3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types=1 );
6
7
7
8
namespace Magento \Setup \Module \Di \Code \Reader ;
8
9
9
10
/**
10
11
* Class FileClassScanner
11
- *
12
- * @package Magento\Setup\Module\Di\Code\Reader
13
12
*/
14
13
class FileClassScanner
15
14
{
@@ -20,9 +19,10 @@ class FileClassScanner
20
19
];
21
20
22
21
private const ALLOWED_OPEN_BRACES_TOKENS = [
23
- T_CURLY_OPEN => true ,
22
+ T_CURLY_OPEN => true ,
24
23
T_DOLLAR_OPEN_CURLY_BRACES => true ,
25
- T_STRING_VARNAME => true ];
24
+ T_STRING_VARNAME => true
25
+ ];
26
26
27
27
/**
28
28
* The filename of the file to introspect
@@ -32,11 +32,11 @@ class FileClassScanner
32
32
private $ filename ;
33
33
34
34
/**
35
- * The list of classes found in the file.
35
+ * The class name found in the file.
36
36
*
37
37
* @var bool
38
38
*/
39
- private $ classNames = false ;
39
+ private $ className = false ;
40
40
41
41
/**
42
42
* @var array
@@ -75,6 +75,19 @@ public function getFileContents()
75
75
return file_get_contents ($ this ->filename );
76
76
}
77
77
78
+ /**
79
+ * Retrieves the first class found in a class file.
80
+ *
81
+ * @return string
82
+ */
83
+ public function getClassName (): string
84
+ {
85
+ if ($ this ->className === false ) {
86
+ $ this ->className = $ this ->extract ();
87
+ }
88
+ return $ this ->className ;
89
+ }
90
+
78
91
/**
79
92
* Extracts the fully qualified class name from a file.
80
93
*
@@ -85,11 +98,10 @@ public function getFileContents()
85
98
*
86
99
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
87
100
* @SuppressWarnings(PHPMD.NPathComplexity)
88
- * @return array
101
+ * @return string
89
102
*/
90
- private function extract ()
103
+ private function extract (): string
91
104
{
92
- $ classes = [];
93
105
$ namespaceParts = [];
94
106
$ class = '' ;
95
107
$ triggerClass = false ;
@@ -117,6 +129,9 @@ private function extract()
117
129
}
118
130
$ namespaceParts [] = $ token [1 ];
119
131
132
+ // `class` token is not used with a valid class name
133
+ } elseif ($ triggerClass && !$ tokenIsArray ) {
134
+ $ triggerClass = false ;
120
135
// The class keyword was found in the last loop
121
136
} elseif ($ triggerClass && $ token [0 ] === T_STRING ) {
122
137
$ triggerClass = false ;
@@ -125,27 +140,26 @@ private function extract()
125
140
126
141
switch ($ token [0 ]) {
127
142
case T_NAMESPACE :
128
- // Current loop contains the namespace keyword. Between this and the semicolon is the namespace
143
+ // Current loop contains the namespace keyword. Between this and the semicolon is the namespace
129
144
$ triggerNamespace = true ;
130
145
$ namespaceParts = [];
131
146
$ bracedNamespace = $ this ->isBracedNamespace ($ index );
132
147
break ;
133
148
case T_CLASS :
134
- // Current loop contains the class keyword. Next loop will have the class name itself.
149
+ // Current loop contains the class keyword. Next loop will have the class name itself.
135
150
if ($ braceLevel == 0 || ($ bracedNamespace && $ braceLevel == 1 )) {
136
151
$ triggerClass = true ;
137
152
}
138
153
break ;
139
154
}
140
155
141
- // We have a class name, let's concatenate and store it!
156
+ // We have a class name, let's concatenate and return it!
142
157
if ($ class !== '' ) {
143
158
$ fqClassName = trim (join ('' , $ namespaceParts )) . trim ($ class );
144
- $ classes [] = $ fqClassName ;
145
- $ class = '' ;
159
+ return $ fqClassName ;
146
160
}
147
161
}
148
- return $ classes ;
162
+ return $ class ;
149
163
}
150
164
151
165
/**
@@ -173,19 +187,4 @@ private function isBracedNamespace($index)
173
187
}
174
188
throw new InvalidFileException ('Could not find namespace termination ' );
175
189
}
176
-
177
- /**
178
- * Retrieves the first class found in a class file.
179
- *
180
- * The return value is in an array format so it retains the same usage as the FileScanner.
181
- *
182
- * @return array
183
- */
184
- public function getClassNames ()
185
- {
186
- if ($ this ->classNames === false ) {
187
- $ this ->classNames = $ this ->extract ();
188
- }
189
- return $ this ->classNames ;
190
- }
191
190
}
0 commit comments