MediaWiki master
ClassCollector.php
Go to the documentation of this file.
1<?php
25
29 protected $namespace = '';
30
34 protected $classes;
35
39 protected $startToken;
40
44 protected $tokens;
45
49 protected $alias;
50
55 public function getClasses( $code ) {
56 $this->namespace = '';
57 $this->classes = [];
58 $this->startToken = null;
59 $this->alias = null;
60 $this->tokens = [];
61
62 // HACK: The PHP tokenizer is slow (T225730).
63 // Speed it up by reducing the input to the three kinds of statement we care about:
64 // - namespace X;
65 // - [final] [abstract] class X … {}
66 // - class_alias( … );
67 $lines = [];
68 $matches = null;
69 preg_match_all(
70 // phpcs:ignore Generic.Files.LineLength.TooLong
71 '#^\t*(?:namespace |(final )?(abstract )?(class|interface|trait) |class_alias\‍()[^;{]+[;{]\s*\}?#m',
72 $code,
74 );
75 if ( isset( $matches[0][0] ) ) {
76 foreach ( $matches[0] as $match ) {
77 $match = trim( $match );
78 if ( str_ends_with( $match, '{' ) ) {
79 // Keep it balanced
80 $match .= '}';
81 }
82 $lines[] = $match;
83 }
84 }
85 $code = '<?php ' . implode( "\n", $lines ) . "\n";
86
87 foreach ( token_get_all( $code ) as $token ) {
88 if ( $this->startToken === null ) {
89 $this->tryBeginExpect( $token );
90 } else {
91 $this->tryEndExpect( $token );
92 }
93 }
94
95 return $this->classes;
96 }
97
103 protected function tryBeginExpect( $token ) {
104 if ( is_string( $token ) ) {
105 return;
106 }
107 // Note: When changing class name discovery logic,
108 // AutoLoaderStructureTest.php may also need to be updated.
109 switch ( $token[0] ) {
110 case T_NAMESPACE:
111 case T_CLASS:
112 case T_INTERFACE:
113 case T_TRAIT:
114 case T_DOUBLE_COLON:
115 case T_NEW:
116 $this->startToken = $token;
117 break;
118 case T_STRING:
119 if ( $token[1] === 'class_alias' ) {
120 $this->startToken = $token;
121 $this->alias = [];
122 }
123 }
124 }
125
131 protected function tryEndExpect( $token ) {
132 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable
133 switch ( $this->startToken[0] ) {
134 case T_DOUBLE_COLON:
135 // Skip over T_CLASS after T_DOUBLE_COLON because this is something like
136 // "ClassName::class" that evaluates to a fully qualified class name. It
137 // doesn't define a new class.
138 $this->startToken = null;
139 break;
140 case T_NEW:
141 // Skip over T_CLASS after T_NEW because this is an anonymous class.
142 if ( !is_array( $token ) || $token[0] !== T_WHITESPACE ) {
143 $this->startToken = null;
144 }
145 break;
146 case T_NAMESPACE:
147 if ( $token === ';' || $token === '{' ) {
148 $this->namespace = $this->implodeTokens() . '\\';
149 } else {
150 $this->tokens[] = $token;
151 }
152 break;
153
154 case T_STRING:
155 if ( $this->alias !== null ) {
156 // Flow 1 - Two string literals:
157 // - T_STRING class_alias
158 // - '('
159 // - T_CONSTANT_ENCAPSED_STRING 'TargetClass'
160 // - ','
161 // - T_WHITESPACE
162 // - T_CONSTANT_ENCAPSED_STRING 'AliasName'
163 // - ')'
164 // Flow 2 - Use of ::class syntax for first parameter
165 // - T_STRING class_alias
166 // - '('
167 // - T_STRING TargetClass
168 // - T_DOUBLE_COLON ::
169 // - T_CLASS class
170 // - ','
171 // - T_WHITESPACE
172 // - T_CONSTANT_ENCAPSED_STRING 'AliasName'
173 // - ')'
174 if ( $token === '(' ) {
175 // Start of a function call to class_alias()
176 $this->alias = [ 'target' => false, 'name' => false ];
177 } elseif ( $token === ',' ) {
178 // Record that we're past the first parameter
179 if ( $this->alias['target'] === false ) {
180 $this->alias['target'] = true;
181 }
182 } elseif ( is_array( $token ) && $token[0] === T_CONSTANT_ENCAPSED_STRING ) {
183 if ( $this->alias['target'] === true ) {
184 // We already saw a first argument, this must be the second.
185 // Strip quotes from the string literal.
186 $this->alias['name'] = self::stripQuotes( $token[1] );
187 }
188 } elseif ( $token === ')' ) {
189 // End of function call
190 $this->classes[] = $this->alias['name'];
191 $this->alias = null;
192 $this->startToken = null;
193 } elseif ( !is_array( $token ) || (
194 $token[0] !== T_STRING &&
195 $token[0] !== T_DOUBLE_COLON &&
196 $token[0] !== T_CLASS &&
197 $token[0] !== T_WHITESPACE
198 ) ) {
199 // Ignore this call to class_alias() - compat/Timestamp.php
200 $this->alias = null;
201 $this->startToken = null;
202 }
203 }
204 break;
205
206 case T_CLASS:
207 case T_INTERFACE:
208 case T_TRAIT:
209 $this->tokens[] = $token;
210 if ( is_array( $token ) && $token[0] === T_STRING ) {
211 $this->classes[] = $this->namespace . $this->implodeTokens();
212 }
213 }
214 }
215
225 private static function stripQuotes( $str ) {
226 return str_replace( '\\\\', '\\', substr( $str, 1, -1 ) );
227 }
228
235 protected function implodeTokens() {
236 $content = [];
237 foreach ( $this->tokens as $token ) {
238 $content[] = is_string( $token ) ? $token : $token[1];
239 }
240
241 $this->tokens = [];
242 $this->startToken = null;
243
244 return trim( implode( '', $content ), " \n\t" );
245 }
246}
implodeTokens()
Returns the string representation of the tokens within the current expect sequence and resets the seq...
Reads PHP code and returns the FQCN of every class defined within it.
string $namespace
Current namespace.
array $classes
List of FQCN detected in this pass.
array null $alias
Class alias with target/name fields.
array null $startToken
Token from token_get_all() that started an expect sequence.
tryEndExpect( $token)
Accepts the next token in an expect sequence.
array[] string[] $tokens
List of tokens that are members of the current expect sequence.
tryBeginExpect( $token)
Determine if $token begins the next expect sequence.
if(!file_exists( $CREDITS)) $lines