Complex classes like DocComment often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DocComment, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
43 | class DocComment |
||
44 | { |
||
45 | const TypeTrait = 'trait'; |
||
46 | const TypeClass = 'class'; |
||
47 | const TypeInterface = 'interface'; |
||
48 | private static $use = []; |
||
49 | private static $useAliases = []; |
||
50 | private static $namespaces = []; |
||
51 | private static $types = []; |
||
52 | private static $classNames = []; |
||
53 | private static $classes = []; |
||
54 | private static $methods = []; |
||
55 | private static $fields = []; |
||
56 | private static $parsedFiles = []; |
||
57 | |||
58 | 1 | public static function clearCache() |
|
68 | |||
69 | 51 | public function get($reflection) |
|
87 | |||
88 | /** |
||
89 | * Get doc comment for file |
||
90 | * If file contains several classes, $className will be returned |
||
91 | * If file name matches class name, this class will be returned |
||
92 | * @param string $name |
||
93 | * @param string $className |
||
94 | * @return array |
||
95 | */ |
||
96 | 6 | public function forFile($name, $className = null) |
|
119 | |||
120 | 53 | public function forClass(Reflector $reflection) |
|
151 | |||
152 | 9 | public function forMethod(Reflector $reflection) |
|
163 | |||
164 | 22 | public function forProperty(Reflector $reflection) |
|
174 | |||
175 | 1 | private function processAnonymous(Reflector $reflection, $fqn) |
|
196 | |||
197 | 64 | private function process($file, $fqn = false) |
|
206 | |||
207 | 53 | protected function parse($file, $fqn = false) |
|
208 | { |
||
209 | 53 | $use = []; |
|
210 | 53 | $aliases = []; |
|
211 | 53 | $namespace = '\\'; |
|
212 | 53 | $tokens = $this->getTokens($file); |
|
213 | 53 | $class = false; |
|
214 | 53 | $isTrait = $isClass = $isInterface = false; |
|
215 | 53 | $comment = null; |
|
216 | 53 | $max = count($tokens); |
|
217 | 53 | $i = 0; |
|
218 | 53 | while ($i < $max) |
|
219 | { |
||
220 | 53 | $token = $tokens[$i]; |
|
221 | 53 | if (is_array($token)) |
|
222 | { |
||
223 | 53 | [$code, $value] = $token; |
|
224 | |||
225 | // $tokenType = token_name($code); |
||
226 | // |
||
227 | // // Seems doc comment before some entity |
||
228 | // if(strpos($value, '@') !== false) |
||
229 | // { |
||
230 | //// echo $value; |
||
231 | // } |
||
232 | // |
||
233 | // codecept_debug("$tokenType: " . str_replace("\n", '\n', str_replace("\t", '\t', $value))); |
||
234 | |||
235 | switch ($code) |
||
236 | { |
||
237 | 53 | case T_DOC_COMMENT: |
|
238 | 53 | $comment = $value; |
|
239 | 53 | break; |
|
240 | |||
241 | 53 | case T_NAMESPACE: |
|
242 | 53 | $comment = false; |
|
243 | 53 | $tokensCount = count($tokens); |
|
244 | 53 | for ($j = $i + 1; $j < $tokensCount; $j++) |
|
245 | { |
||
246 | 53 | if ($tokens[$j][0] === T_STRING) |
|
247 | { |
||
248 | 53 | $namespace .= '\\' . $tokens[$j][1]; |
|
249 | } |
||
250 | 53 | elseif ($tokens[$j] === '{' || $tokens[$j] === ';') |
|
251 | { |
||
252 | 53 | break; |
|
253 | } |
||
254 | } |
||
255 | |||
256 | 53 | $namespace = preg_replace('~^\\\\+~', '', $namespace); |
|
257 | 53 | break; |
|
258 | 53 | case T_USE: |
|
259 | // After class declaration, this should ignore `use` trait |
||
260 | 49 | if ($class) |
|
261 | { |
||
262 | 4 | break; |
|
263 | } |
||
264 | 49 | $comment = false; |
|
265 | 49 | $useNs = ''; |
|
266 | 49 | $tokensCount = count($tokens); |
|
267 | 49 | $as = false; |
|
268 | 49 | for ($j = $i + 1; $j < $tokensCount; $j++) |
|
269 | { |
||
270 | 49 | $tokenName = $tokens[$j][0]; |
|
271 | 49 | if ($tokenName === T_STRING && !$as) |
|
272 | { |
||
273 | 49 | $useNs .= '\\' . $tokens[$j][1]; |
|
274 | } |
||
275 | 49 | if ($tokenName === T_STRING && $as) |
|
276 | { |
||
277 | 2 | $alias = $tokens[$j][1]; |
|
278 | 2 | break; |
|
279 | } |
||
280 | 49 | if ($tokenName === T_AS) |
|
281 | { |
||
282 | 2 | $as = true; |
|
283 | } |
||
284 | 49 | if ($tokens[$j] === '{' || $tokens[$j] === ';') |
|
285 | { |
||
286 | 49 | break; |
|
287 | } |
||
288 | } |
||
289 | 49 | $use[] = preg_replace('~^\\\\+~', '', $useNs); |
|
290 | 49 | if ($as) |
|
291 | { |
||
292 | 2 | assert(!empty($alias)); |
|
293 | 2 | $aliases[$useNs] = $alias; |
|
294 | } |
||
295 | 49 | break; |
|
296 | 53 | case T_TRAIT: |
|
297 | 53 | case T_CLASS: |
|
298 | 53 | case T_INTERFACE: |
|
299 | // Ignore magic constant `::class` |
||
300 | 53 | if ($tokens[$i - 1][0] == T_DOUBLE_COLON) |
|
301 | { |
||
302 | 4 | break; |
|
303 | } |
||
304 | 53 | $class = $this->getString($tokens, $i, $max); |
|
305 | 53 | if (!$fqn) |
|
306 | { |
||
307 | 4 | $fqn = sprintf('%s\%s', $namespace, $class); |
|
308 | } |
||
309 | 53 | if ($comment !== false) |
|
310 | { |
||
311 | 45 | self::$classes[$fqn] = $comment; |
|
312 | 45 | $comment = false; |
|
313 | } |
||
314 | 53 | self::$namespaces[$fqn] = $namespace; |
|
315 | 53 | self::$classNames[$fqn] = $class; |
|
316 | 53 | self::$use[$fqn] = $use; |
|
317 | 53 | self::$useAliases[$fqn] = $aliases; |
|
318 | |||
319 | 53 | if($code === T_TRAIT) |
|
320 | { |
||
321 | 7 | self::$types[$fqn] = self::TypeTrait; |
|
322 | } |
||
323 | 49 | elseif($code === T_CLASS) |
|
324 | { |
||
325 | 47 | self::$types[$fqn] = self::TypeClass; |
|
326 | } |
||
327 | 2 | elseif($code === T_INTERFACE) |
|
328 | { |
||
329 | 2 | self::$types[$fqn] = self::TypeInterface; |
|
330 | } |
||
331 | |||
332 | 53 | break; |
|
333 | |||
334 | 53 | case T_VARIABLE: |
|
335 | 39 | if ($comment !== false && $class) |
|
336 | { |
||
337 | 33 | $field = substr($token[1], 1); |
|
338 | 33 | self::$fields[$fqn][$field] = $comment; |
|
339 | 33 | $comment = false; |
|
340 | } |
||
341 | 39 | break; |
|
342 | |||
343 | 53 | case T_FUNCTION: |
|
344 | 20 | if ($comment !== false && $class) |
|
345 | { |
||
346 | 12 | $function = $this->getString($tokens, $i, $max); |
|
347 | 12 | self::$methods[$fqn][$function] = $comment; |
|
348 | 12 | $comment = false; |
|
349 | } |
||
350 | |||
351 | 20 | break; |
|
352 | |||
353 | // ignore |
||
354 | 53 | case T_WHITESPACE: |
|
355 | 53 | case T_PUBLIC: |
|
356 | 53 | case T_PROTECTED: |
|
357 | 53 | case T_PRIVATE: |
|
358 | 53 | case T_ABSTRACT: |
|
359 | 53 | case T_FINAL: |
|
360 | 53 | case T_VAR: |
|
361 | 53 | break; |
|
362 | |||
363 | // Might be scalar typed property, eg: |
||
364 | // `public string $title;` |
||
365 | 53 | case T_STRING: |
|
366 | 53 | case T_ARRAY: |
|
367 | // NOTE: Maybe it will better to check ahead, ie if T_STRING, then check it next value is variable |
||
368 | |||
369 | // Skip whitespace and check token before T_STRING and T_WHITESPACE |
||
370 | 53 | if(isset($tokens[$i - 2]) && is_array($tokens[$i - 2])) |
|
371 | { |
||
372 | 53 | $prevType = $tokens[$i - 2][0]; |
|
373 | 53 | $prevVal = $tokens[$i - 2][1]; |
|
374 | switch ($prevType) |
||
375 | { |
||
376 | 53 | case T_PUBLIC: |
|
377 | 53 | case T_PROTECTED: |
|
378 | 53 | case T_PRIVATE: |
|
379 | 53 | case T_ABSTRACT: |
|
380 | break 2; |
||
381 | } |
||
382 | |||
383 | } |
||
384 | // Optional typed parameter, the `?` is present as a *string* token, |
||
385 | // ie contains only `?` string, not an array |
||
386 | // Skip whitespace and check token before T_STRING and T_WHITESPACE |
||
387 | 53 | if(isset($tokens[$i - 3]) && is_array($tokens[$i - 3])) |
|
388 | { |
||
389 | 53 | $prevType = $tokens[$i - 3][0]; |
|
390 | 53 | $prevVal = $tokens[$i - 3][1]; |
|
391 | switch ($prevType) |
||
392 | { |
||
393 | 53 | case T_PUBLIC: |
|
394 | 53 | case T_PROTECTED: |
|
395 | 53 | case T_PRIVATE: |
|
396 | 53 | case T_ABSTRACT: |
|
397 | break 2; |
||
398 | } |
||
399 | |||
400 | } |
||
401 | 53 | break; |
|
402 | default: |
||
403 | 53 | $comment = false; |
|
404 | 53 | break; |
|
405 | } |
||
406 | } |
||
407 | else |
||
408 | { |
||
409 | switch ($token) |
||
410 | { |
||
411 | // Don't reset comment tag on `?` token, as it *probably* means optional val |
||
412 | 53 | case '?': |
|
413 | break; |
||
414 | default: |
||
415 | 53 | $comment = false; |
|
416 | } |
||
417 | } |
||
418 | 53 | $i++; |
|
419 | } |
||
420 | 53 | return $fqn; |
|
421 | } |
||
422 | |||
423 | 53 | private function getString($tokens, &$i, $max) |
|
448 | |||
449 | 53 | private function getTokens($file) |
|
453 | |||
454 | } |
||
455 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: