Complex classes like ReflectionFile 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 ReflectionFile, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | class ReflectionFile implements Reflector |
||
31 | { |
||
32 | |||
33 | const IS_IMPLICIT_ABSTRACT = 16; |
||
34 | const IS_EXPLICIT_ABSTRACT = 32; |
||
35 | const IS_FINAL = 64; |
||
36 | |||
37 | /** |
||
38 | * Class name |
||
39 | * @var string |
||
40 | */ |
||
41 | public $name = ''; |
||
42 | |||
43 | /** |
||
44 | * Extracted docs |
||
45 | * @var mixed[] |
||
46 | */ |
||
47 | private $_docs = []; |
||
48 | private $namespace; |
||
49 | private $shortName; |
||
50 | private $file; |
||
51 | private $methods; |
||
52 | private $fields; |
||
53 | |||
54 | private $type; |
||
55 | |||
56 | /** |
||
57 | * (PHP 5)<br/> |
||
58 | * Constructs a ReflectionClass from file |
||
59 | * @link http://php.net/manual/en/reflectionclass.construct.php |
||
60 | * @param string $file <p> |
||
61 | * Either a string containing the name of the class to |
||
62 | * reflect, or an object. |
||
63 | * </p> |
||
64 | */ |
||
65 | 5 | public function __construct($file) |
|
85 | |||
86 | final private function __clone() |
||
90 | |||
91 | public function __toString() |
||
95 | |||
96 | /** |
||
97 | * (PHP 5)<br/> |
||
98 | * Exports a class |
||
99 | * @link http://php.net/manual/en/reflectionclass.export.php |
||
100 | * @param mixed $argument <p> |
||
|
|||
101 | * The reflection to export. |
||
102 | * </p> |
||
103 | * @param bool $return [optional] <p> |
||
104 | * Setting to <b>TRUE</b> will return the export, |
||
105 | * as opposed to emitting it. Setting to <b>FALSE</b> (the default) will do the opposite. |
||
106 | * </p> |
||
107 | * @return string If the <i>return</i> parameter |
||
108 | * is set to <b>TRUE</b>, then the export is returned as a string, |
||
109 | * otherwise <b>NULL</b> is returned. |
||
110 | */ |
||
111 | public static function export() |
||
115 | |||
116 | /** |
||
117 | * (PHP 5)<br/> |
||
118 | * Gets class name |
||
119 | * @link http://php.net/manual/en/reflectionclass.getname.php |
||
120 | * @return string The class name. |
||
121 | */ |
||
122 | public function getName() |
||
126 | |||
127 | /** |
||
128 | * (PHP 5)<br/> |
||
129 | * Checks if class is defined internally by an extension, or the core |
||
130 | * @link http://php.net/manual/en/reflectionclass.isinternal.php |
||
131 | * @return bool <b>TRUE</b> on success or <b>FALSE</b> on failure. |
||
132 | */ |
||
133 | public function isInternal() |
||
137 | |||
138 | /** |
||
139 | * (PHP 5)<br/> |
||
140 | * Checks if user defined |
||
141 | * @link http://php.net/manual/en/reflectionclass.isuserdefined.php |
||
142 | * @return bool <b>TRUE</b> on success or <b>FALSE</b> on failure. |
||
143 | */ |
||
144 | public function isUserDefined() |
||
148 | |||
149 | /** |
||
150 | * (PHP 5)<br/> |
||
151 | * Checks if the class is instantiable |
||
152 | * @link http://php.net/manual/en/reflectionclass.isinstantiable.php |
||
153 | * @return bool <b>TRUE</b> on success or <b>FALSE</b> on failure. |
||
154 | */ |
||
155 | public function isInstantiable() |
||
159 | |||
160 | /** |
||
161 | * (PHP >= 5.4.0)<br/> |
||
162 | * Returns whether this class is cloneable |
||
163 | * @link http://php.net/manual/en/reflectionclass.iscloneable.php |
||
164 | * @return bool <b>TRUE</b> if the class is cloneable, <b>FALSE</b> otherwise. |
||
165 | */ |
||
166 | public function isCloneable() |
||
170 | |||
171 | /** |
||
172 | * (PHP 5)<br/> |
||
173 | * Gets the filename of the file in which the class has been defined |
||
174 | * @link http://php.net/manual/en/reflectionclass.getfilename.php |
||
175 | * @return string the filename of the file in which the class has been defined. |
||
176 | * If the class is defined in the PHP core or in a PHP extension, <b>FALSE</b> |
||
177 | * is returned. |
||
178 | */ |
||
179 | public function getFileName() |
||
183 | |||
184 | /** |
||
185 | * (PHP 5)<br/> |
||
186 | * Gets starting line number |
||
187 | * @link http://php.net/manual/en/reflectionclass.getstartline.php |
||
188 | * @return int The starting line number, as an integer. |
||
189 | */ |
||
190 | public function getStartLine() |
||
194 | |||
195 | /** |
||
196 | * (PHP 5)<br/> |
||
197 | * Gets end line |
||
198 | * @link http://php.net/manual/en/reflectionclass.getendline.php |
||
199 | * @return int The ending line number of the user defined class, or <b>FALSE</b> if unknown. |
||
200 | */ |
||
201 | public function getEndLine() |
||
205 | |||
206 | /** |
||
207 | * (PHP 5 >= 5.1.0)<br/> |
||
208 | * Gets doc comments |
||
209 | * @link http://php.net/manual/en/reflectionclass.getdoccomment.php |
||
210 | * @return string The doc comment if it exists, otherwise <b>FALSE</b> |
||
211 | */ |
||
212 | public function getDocComment() |
||
216 | |||
217 | /** |
||
218 | * (PHP 5)<br/> |
||
219 | * Gets the constructor of the class |
||
220 | * @link http://php.net/manual/en/reflectionclass.getconstructor.php |
||
221 | * @return ReflectionMethod A <b>ReflectionMethod</b> object reflecting the class' constructor, or <b>NULL</b> if the class |
||
222 | * has no constructor. |
||
223 | */ |
||
224 | public function getConstructor() |
||
228 | |||
229 | /** |
||
230 | * (PHP 5 >= 5.1.0)<br/> |
||
231 | * Checks if method is defined |
||
232 | * @link http://php.net/manual/en/reflectionclass.hasmethod.php |
||
233 | * @param string $name <p> |
||
234 | * Name of the method being checked for. |
||
235 | * </p> |
||
236 | * @return bool <b>TRUE</b> if it has the method, otherwise <b>FALSE</b> |
||
237 | */ |
||
238 | public function hasMethod($name) |
||
242 | |||
243 | /** |
||
244 | * (PHP 5)<br/> |
||
245 | * Gets a <b>ReflectionMethod</b> for a class method. |
||
246 | * @link http://php.net/manual/en/reflectionclass.getmethod.php |
||
247 | * @param string $name <p> |
||
248 | * The method name to reflect. |
||
249 | * </p> |
||
250 | * @return ReflectionMethod A <b>ReflectionMethod</b>. |
||
251 | */ |
||
252 | public function getMethod($name) |
||
256 | |||
257 | /** |
||
258 | * (PHP 5)<br/> |
||
259 | * Gets an array of methods |
||
260 | * @link http://php.net/manual/en/reflectionclass.getmethods.php |
||
261 | * @param int $filter [optional] <p> |
||
262 | * Filter the results to include only methods with certain attributes. Defaults |
||
263 | * to no filtering. |
||
264 | * </p> |
||
265 | * <p> |
||
266 | * Any combination of <b>ReflectionMethod::IS_STATIC</b>, |
||
267 | * <b>ReflectionMethod::IS_PUBLIC</b>, |
||
268 | * <b>ReflectionMethod::IS_PROTECTED</b>, |
||
269 | * <b>ReflectionMethod::IS_PRIVATE</b>, |
||
270 | * <b>ReflectionMethod::IS_ABSTRACT</b>, |
||
271 | * <b>ReflectionMethod::IS_FINAL</b>. |
||
272 | * </p> |
||
273 | * @return array An array of <b>ReflectionMethod</b> objects |
||
274 | * reflecting each method. |
||
275 | */ |
||
276 | public function getMethods($filter = null) |
||
280 | |||
281 | /** |
||
282 | * (PHP 5 >= 5.1.0)<br/> |
||
283 | * Checks if property is defined |
||
284 | * @link http://php.net/manual/en/reflectionclass.hasproperty.php |
||
285 | * @param string $name <p> |
||
286 | * Name of the property being checked for. |
||
287 | * </p> |
||
288 | * @return bool <b>TRUE</b> if it has the property, otherwise <b>FALSE</b> |
||
289 | */ |
||
290 | public function hasProperty($name) |
||
294 | |||
295 | /** |
||
296 | * (PHP 5)<br/> |
||
297 | * Gets a <b>ReflectionProperty</b> for a class's property |
||
298 | * @link http://php.net/manual/en/reflectionclass.getproperty.php |
||
299 | * @param string $name <p> |
||
300 | * The property name. |
||
301 | * </p> |
||
302 | * @return ReflectionProperty A <b>ReflectionProperty</b>. |
||
303 | */ |
||
304 | public function getProperty($name) |
||
308 | |||
309 | /** |
||
310 | * (PHP 5)<br/> |
||
311 | * Gets properties |
||
312 | * @link http://php.net/manual/en/reflectionclass.getproperties.php |
||
313 | * @param int $filter [optional] <p> |
||
314 | * The optional filter, for filtering desired property types. It's configured using |
||
315 | * the ReflectionProperty constants, |
||
316 | * and defaults to all property types. |
||
317 | * </p> |
||
318 | * @return array An array of <b>ReflectionProperty</b> objects. |
||
319 | */ |
||
320 | public function getProperties($filter = null) |
||
324 | |||
325 | /** |
||
326 | * (PHP 5 >= 5.1.0)<br/> |
||
327 | * Checks if constant is defined |
||
328 | * @link http://php.net/manual/en/reflectionclass.hasconstant.php |
||
329 | * @param string $name <p> |
||
330 | * The name of the constant being checked for. |
||
331 | * </p> |
||
332 | * @return bool <b>TRUE</b> if the constant is defined, otherwise <b>FALSE</b>. |
||
333 | */ |
||
334 | public function hasConstant($name) |
||
338 | |||
339 | /** |
||
340 | * (PHP 5)<br/> |
||
341 | * Gets constants |
||
342 | * @link http://php.net/manual/en/reflectionclass.getconstants.php |
||
343 | * @return array An array of constants. |
||
344 | * Constant name in key, constant value in value. |
||
345 | */ |
||
346 | public function getConstants() |
||
350 | |||
351 | /** |
||
352 | * (PHP 5)<br/> |
||
353 | * Gets defined constant |
||
354 | * @link http://php.net/manual/en/reflectionclass.getconstant.php |
||
355 | * @param string $name <p> |
||
356 | * Name of the constant. |
||
357 | * </p> |
||
358 | * @return mixed Value of the constant. |
||
359 | */ |
||
360 | public function getConstant($name) |
||
364 | |||
365 | /** |
||
366 | * (PHP 5)<br/> |
||
367 | * Gets the interfaces |
||
368 | * @link http://php.net/manual/en/reflectionclass.getinterfaces.php |
||
369 | * @return array An associative array of interfaces, with keys as interface |
||
370 | * names and the array values as <b>ReflectionClass</b> objects. |
||
371 | */ |
||
372 | public function getInterfaces() |
||
376 | |||
377 | /** |
||
378 | * (PHP 5 >= 5.2.0)<br/> |
||
379 | * Gets the interface names |
||
380 | * @link http://php.net/manual/en/reflectionclass.getinterfacenames.php |
||
381 | * @return array A numerical array with interface names as the values. |
||
382 | */ |
||
383 | public function getInterfaceNames() |
||
387 | |||
388 | /** |
||
389 | * (PHP 5)<br/> |
||
390 | * Checks if the class is an interface |
||
391 | * @link http://php.net/manual/en/reflectionclass.isinterface.php |
||
392 | * @return bool <b>TRUE</b> on success or <b>FALSE</b> on failure. |
||
393 | */ |
||
394 | 2 | public function isInterface() |
|
398 | |||
399 | /** |
||
400 | * (PHP >= 5.4.0)<br/> |
||
401 | * Returns an array of traits used by this class |
||
402 | * @link http://php.net/manual/en/reflectionclass.gettraits.php |
||
403 | * @return array an array with trait names in keys and instances of trait's |
||
404 | * <b>ReflectionClass</b> in values. |
||
405 | * Returns <b>NULL</b> in case of an error. |
||
406 | */ |
||
407 | public function getTraits() |
||
411 | |||
412 | /** |
||
413 | * (PHP >= 5.4.0)<br/> |
||
414 | * Returns an array of names of traits used by this class |
||
415 | * @link http://php.net/manual/en/reflectionclass.gettraitnames.php |
||
416 | * @return array an array with trait names in values. |
||
417 | * Returns <b>NULL</b> in case of an error. |
||
418 | */ |
||
419 | public function getTraitNames() |
||
423 | |||
424 | /** |
||
425 | * (PHP >= 5.4.0)<br/> |
||
426 | * Returns an array of trait aliases |
||
427 | * @link http://php.net/manual/en/reflectionclass.gettraitaliases.php |
||
428 | * @return array an array with new method names in keys and original names (in the |
||
429 | * format "TraitName::original") in values. |
||
430 | * Returns <b>NULL</b> in case of an error. |
||
431 | */ |
||
432 | public function getTraitAliases() |
||
436 | |||
437 | /** |
||
438 | * (PHP >= 5.4.0)<br/> |
||
439 | * Returns whether this is a trait |
||
440 | * @link http://php.net/manual/en/reflectionclass.istrait.php |
||
441 | * @return bool <b>TRUE</b> if this is a trait, <b>FALSE</b> otherwise. |
||
442 | * Returns <b>NULL</b> in case of an error. |
||
443 | */ |
||
444 | 2 | public function isTrait() |
|
448 | |||
449 | /** |
||
450 | * (PHP 5)<br/> |
||
451 | * Checks if class is abstract |
||
452 | * @link http://php.net/manual/en/reflectionclass.isabstract.php |
||
453 | * @return bool <b>TRUE</b> on success or <b>FALSE</b> on failure. |
||
454 | */ |
||
455 | public function isAbstract() |
||
459 | |||
460 | /** |
||
461 | * (PHP 5)<br/> |
||
462 | * Checks if class is final |
||
463 | * @link http://php.net/manual/en/reflectionclass.isfinal.php |
||
464 | * @return bool <b>TRUE</b> on success or <b>FALSE</b> on failure. |
||
465 | */ |
||
466 | public function isFinal() |
||
470 | |||
471 | /** |
||
472 | * (PHP 5)<br/> |
||
473 | * Gets modifiers |
||
474 | * @link http://php.net/manual/en/reflectionclass.getmodifiers.php |
||
475 | * @return int bitmask of |
||
476 | * modifier constants. |
||
477 | */ |
||
478 | public function getModifiers() |
||
482 | |||
483 | /** |
||
484 | * (PHP 5)<br/> |
||
485 | * Checks class for instance |
||
486 | * @link http://php.net/manual/en/reflectionclass.isinstance.php |
||
487 | * @param object $object <p> |
||
488 | * The object being compared to. |
||
489 | * </p> |
||
490 | * @return bool <b>TRUE</b> on success or <b>FALSE</b> on failure. |
||
491 | */ |
||
492 | public function isInstance($object) |
||
496 | |||
497 | /** |
||
498 | * (PHP 5)<br/> |
||
499 | * Creates a new class instance from given arguments. |
||
500 | * @link http://php.net/manual/en/reflectionclass.newinstance.php |
||
501 | * @param mixed $args <p> |
||
502 | * Accepts a variable number of arguments which are passed to the class |
||
503 | * constructor, much like <b>call_user_func</b>. |
||
504 | * </p> |
||
505 | * @param mixed $_ [optional] |
||
506 | * @return object |
||
507 | */ |
||
508 | public function newInstance($args, $_ = null) |
||
512 | |||
513 | /** |
||
514 | * (PHP >= 5.4.0)<br/> |
||
515 | * Creates a new class instance without invoking the constructor. |
||
516 | * @link http://php.net/manual/en/reflectionclass.newinstancewithoutconstructor.php |
||
517 | * @return object |
||
518 | */ |
||
519 | public function newInstanceWithoutConstructor() |
||
523 | |||
524 | /** |
||
525 | * (PHP 5 >= 5.1.3)<br/> |
||
526 | * Creates a new class instance from given arguments. |
||
527 | * @link http://php.net/manual/en/reflectionclass.newinstanceargs.php |
||
528 | * @param array $args [optional] <p> |
||
529 | * The parameters to be passed to the class constructor as an array. |
||
530 | * </p> |
||
531 | * @return object a new instance of the class. |
||
532 | */ |
||
533 | public function newInstanceArgs(array $args = null) |
||
537 | |||
538 | /** |
||
539 | * (PHP 5)<br/> |
||
540 | * Gets parent class |
||
541 | * @link http://php.net/manual/en/reflectionclass.getparentclass.php |
||
542 | * @return object A <b>ReflectionClass</b>. |
||
543 | */ |
||
544 | public function getParentClass() |
||
548 | |||
549 | /** |
||
550 | * (PHP 5)<br/> |
||
551 | * Checks if a subclass |
||
552 | * @link http://php.net/manual/en/reflectionclass.issubclassof.php |
||
553 | * @param string $class <p> |
||
554 | * The class name being checked against. |
||
555 | * </p> |
||
556 | * @return bool <b>TRUE</b> on success or <b>FALSE</b> on failure. |
||
557 | */ |
||
558 | public function isSubclassOf($class) |
||
562 | |||
563 | /** |
||
564 | * (PHP 5)<br/> |
||
565 | * Gets static properties |
||
566 | * @link http://php.net/manual/en/reflectionclass.getstaticproperties.php |
||
567 | * @return array The static properties, as an array. |
||
568 | */ |
||
569 | public function getStaticProperties() |
||
573 | |||
574 | /** |
||
575 | * (PHP 5 >= 5.1.0)<br/> |
||
576 | * Gets static property value |
||
577 | * @link http://php.net/manual/en/reflectionclass.getstaticpropertyvalue.php |
||
578 | * @param string $name <p> |
||
579 | * The name of the static property for which to return a value. |
||
580 | * </p> |
||
581 | * @param mixed $def_value [optional] <p> |
||
582 | * </p> |
||
583 | * @return mixed The value of the static property. |
||
584 | */ |
||
585 | public function getStaticPropertyValue($name, &$def_value = null) |
||
589 | |||
590 | /** |
||
591 | * (PHP 5 >= 5.1.0)<br/> |
||
592 | * Sets static property value |
||
593 | * @link http://php.net/manual/en/reflectionclass.setstaticpropertyvalue.php |
||
594 | * @param string $name <p> |
||
595 | * Property name. |
||
596 | * </p> |
||
597 | * @param string $value <p> |
||
598 | * New property value. |
||
599 | * </p> |
||
600 | * @return void No value is returned. |
||
601 | */ |
||
602 | public function setStaticPropertyValue($name, $value) |
||
606 | |||
607 | /** |
||
608 | * (PHP 5)<br/> |
||
609 | * Gets default properties |
||
610 | * @link http://php.net/manual/en/reflectionclass.getdefaultproperties.php |
||
611 | * @return array An array of default properties, with the key being the name of |
||
612 | * the property and the value being the default value of the property or <b>NULL</b> |
||
613 | * if the property doesn't have a default value. The function does not distinguish |
||
614 | * between static and non static properties and does not take visibility modifiers |
||
615 | * into account. |
||
616 | */ |
||
617 | public function getDefaultProperties() |
||
621 | |||
622 | /** |
||
623 | * (PHP 5)<br/> |
||
624 | * Checks if iterateable |
||
625 | * @link http://php.net/manual/en/reflectionclass.isiterateable.php |
||
626 | * @return bool <b>TRUE</b> on success or <b>FALSE</b> on failure. |
||
627 | */ |
||
628 | public function isIterateable() |
||
632 | |||
633 | /** |
||
634 | * (PHP 5)<br/> |
||
635 | * Implements interface |
||
636 | * @link http://php.net/manual/en/reflectionclass.implementsinterface.php |
||
637 | * @param string $interface <p> |
||
638 | * The interface name. |
||
639 | * </p> |
||
640 | * @return bool <b>TRUE</b> on success or <b>FALSE</b> on failure. |
||
641 | */ |
||
642 | public function implementsInterface($interface) |
||
646 | |||
647 | /** |
||
648 | * (PHP 5)<br/> |
||
649 | * Gets a <b>ReflectionExtension</b> object for the extension which defined the class |
||
650 | * @link http://php.net/manual/en/reflectionclass.getextension.php |
||
651 | * @return ReflectionExtension A <b>ReflectionExtension</b> object representing the extension which defined the class, |
||
652 | * or <b>NULL</b> for user-defined classes. |
||
653 | */ |
||
654 | public function getExtension() |
||
658 | |||
659 | /** |
||
660 | * (PHP 5)<br/> |
||
661 | * Gets the name of the extension which defined the class |
||
662 | * @link http://php.net/manual/en/reflectionclass.getextensionname.php |
||
663 | * @return string The name of the extension which defined the class, or <b>FALSE</b> for user-defined classes. |
||
664 | */ |
||
665 | public function getExtensionName() |
||
669 | |||
670 | /** |
||
671 | * (PHP 5 >= 5.3.0)<br/> |
||
672 | * Checks if in namespace |
||
673 | * @link http://php.net/manual/en/reflectionclass.innamespace.php |
||
674 | * @return bool <b>TRUE</b> on success or <b>FALSE</b> on failure. |
||
675 | */ |
||
676 | public function inNamespace() |
||
680 | |||
681 | /** |
||
682 | * (PHP 5 >= 5.3.0)<br/> |
||
683 | * Gets namespace name |
||
684 | * @link http://php.net/manual/en/reflectionclass.getnamespacename.php |
||
685 | * @return string The namespace name. |
||
686 | */ |
||
687 | public function getNamespaceName() |
||
691 | |||
692 | /** |
||
693 | * (PHP 5 >= 5.3.0)<br/> |
||
694 | * Gets short name |
||
695 | * @link http://php.net/manual/en/reflectionclass.getshortname.php |
||
696 | * @return string The class short name. |
||
697 | */ |
||
698 | public function getShortName() |
||
702 | |||
703 | } |
||
704 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.