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 anme |
||
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 | /** |
||
55 | * (PHP 5)<br/> |
||
56 | * Constructs a ReflectionClass from file |
||
57 | * @link http://php.net/manual/en/reflectionclass.construct.php |
||
58 | * @param string $file <p> |
||
59 | * Either a string containing the name of the class to |
||
60 | * reflect, or an object. |
||
61 | * </p> |
||
62 | */ |
||
63 | 1 | public function __construct($file) |
|
83 | |||
84 | final private function __clone() |
||
88 | |||
89 | public function __toString() |
||
93 | |||
94 | /** |
||
95 | * (PHP 5)<br/> |
||
96 | * Exports a class |
||
97 | * @link http://php.net/manual/en/reflectionclass.export.php |
||
98 | * @param mixed $argument <p> |
||
99 | * The reflection to export. |
||
100 | * </p> |
||
101 | * @param bool $return [optional] <p> |
||
102 | * Setting to <b>TRUE</b> will return the export, |
||
103 | * as opposed to emitting it. Setting to <b>FALSE</b> (the default) will do the opposite. |
||
104 | * </p> |
||
105 | * @return string If the <i>return</i> parameter |
||
106 | * is set to <b>TRUE</b>, then the export is returned as a string, |
||
107 | * otherwise <b>NULL</b> is returned. |
||
108 | */ |
||
109 | public static function export() |
||
113 | |||
114 | /** |
||
115 | * (PHP 5)<br/> |
||
116 | * Gets class name |
||
117 | * @link http://php.net/manual/en/reflectionclass.getname.php |
||
118 | * @return string The class name. |
||
119 | */ |
||
120 | public function getName() |
||
124 | |||
125 | /** |
||
126 | * (PHP 5)<br/> |
||
127 | * Checks if class is defined internally by an extension, or the core |
||
128 | * @link http://php.net/manual/en/reflectionclass.isinternal.php |
||
129 | * @return bool <b>TRUE</b> on success or <b>FALSE</b> on failure. |
||
130 | */ |
||
131 | public function isInternal() |
||
135 | |||
136 | /** |
||
137 | * (PHP 5)<br/> |
||
138 | * Checks if user defined |
||
139 | * @link http://php.net/manual/en/reflectionclass.isuserdefined.php |
||
140 | * @return bool <b>TRUE</b> on success or <b>FALSE</b> on failure. |
||
141 | */ |
||
142 | public function isUserDefined() |
||
146 | |||
147 | /** |
||
148 | * (PHP 5)<br/> |
||
149 | * Checks if the class is instantiable |
||
150 | * @link http://php.net/manual/en/reflectionclass.isinstantiable.php |
||
151 | * @return bool <b>TRUE</b> on success or <b>FALSE</b> on failure. |
||
152 | */ |
||
153 | public function isInstantiable() |
||
157 | |||
158 | /** |
||
159 | * (PHP >= 5.4.0)<br/> |
||
160 | * Returns whether this class is cloneable |
||
161 | * @link http://php.net/manual/en/reflectionclass.iscloneable.php |
||
162 | * @return bool <b>TRUE</b> if the class is cloneable, <b>FALSE</b> otherwise. |
||
163 | */ |
||
164 | public function isCloneable() |
||
168 | |||
169 | /** |
||
170 | * (PHP 5)<br/> |
||
171 | * Gets the filename of the file in which the class has been defined |
||
172 | * @link http://php.net/manual/en/reflectionclass.getfilename.php |
||
173 | * @return string the filename of the file in which the class has been defined. |
||
174 | * If the class is defined in the PHP core or in a PHP extension, <b>FALSE</b> |
||
175 | * is returned. |
||
176 | */ |
||
177 | public function getFileName() |
||
181 | |||
182 | /** |
||
183 | * (PHP 5)<br/> |
||
184 | * Gets starting line number |
||
185 | * @link http://php.net/manual/en/reflectionclass.getstartline.php |
||
186 | * @return int The starting line number, as an integer. |
||
187 | */ |
||
188 | public function getStartLine() |
||
192 | |||
193 | /** |
||
194 | * (PHP 5)<br/> |
||
195 | * Gets end line |
||
196 | * @link http://php.net/manual/en/reflectionclass.getendline.php |
||
197 | * @return int The ending line number of the user defined class, or <b>FALSE</b> if unknown. |
||
198 | */ |
||
199 | public function getEndLine() |
||
203 | |||
204 | /** |
||
205 | * (PHP 5 >= 5.1.0)<br/> |
||
206 | * Gets doc comments |
||
207 | * @link http://php.net/manual/en/reflectionclass.getdoccomment.php |
||
208 | * @return string The doc comment if it exists, otherwise <b>FALSE</b> |
||
209 | */ |
||
210 | public function getDocComment() |
||
214 | |||
215 | /** |
||
216 | * (PHP 5)<br/> |
||
217 | * Gets the constructor of the class |
||
218 | * @link http://php.net/manual/en/reflectionclass.getconstructor.php |
||
219 | * @return ReflectionMethod A <b>ReflectionMethod</b> object reflecting the class' constructor, or <b>NULL</b> if the class |
||
220 | * has no constructor. |
||
221 | */ |
||
222 | public function getConstructor() |
||
226 | |||
227 | /** |
||
228 | * (PHP 5 >= 5.1.0)<br/> |
||
229 | * Checks if method is defined |
||
230 | * @link http://php.net/manual/en/reflectionclass.hasmethod.php |
||
231 | * @param string $name <p> |
||
232 | * Name of the method being checked for. |
||
233 | * </p> |
||
234 | * @return bool <b>TRUE</b> if it has the method, otherwise <b>FALSE</b> |
||
235 | */ |
||
236 | public function hasMethod($name) |
||
240 | |||
241 | /** |
||
242 | * (PHP 5)<br/> |
||
243 | * Gets a <b>ReflectionMethod</b> for a class method. |
||
244 | * @link http://php.net/manual/en/reflectionclass.getmethod.php |
||
245 | * @param string $name <p> |
||
246 | * The method name to reflect. |
||
247 | * </p> |
||
248 | * @return ReflectionMethod A <b>ReflectionMethod</b>. |
||
249 | */ |
||
250 | public function getMethod($name) |
||
254 | |||
255 | /** |
||
256 | * (PHP 5)<br/> |
||
257 | * Gets an array of methods |
||
258 | * @link http://php.net/manual/en/reflectionclass.getmethods.php |
||
259 | * @param int $filter [optional] <p> |
||
260 | * Filter the results to include only methods with certain attributes. Defaults |
||
261 | * to no filtering. |
||
262 | * </p> |
||
263 | * <p> |
||
264 | * Any combination of <b>ReflectionMethod::IS_STATIC</b>, |
||
265 | * <b>ReflectionMethod::IS_PUBLIC</b>, |
||
266 | * <b>ReflectionMethod::IS_PROTECTED</b>, |
||
267 | * <b>ReflectionMethod::IS_PRIVATE</b>, |
||
268 | * <b>ReflectionMethod::IS_ABSTRACT</b>, |
||
269 | * <b>ReflectionMethod::IS_FINAL</b>. |
||
270 | * </p> |
||
271 | * @return array An array of <b>ReflectionMethod</b> objects |
||
272 | * reflecting each method. |
||
273 | */ |
||
274 | public function getMethods($filter = null) |
||
278 | |||
279 | /** |
||
280 | * (PHP 5 >= 5.1.0)<br/> |
||
281 | * Checks if property is defined |
||
282 | * @link http://php.net/manual/en/reflectionclass.hasproperty.php |
||
283 | * @param string $name <p> |
||
284 | * Name of the property being checked for. |
||
285 | * </p> |
||
286 | * @return bool <b>TRUE</b> if it has the property, otherwise <b>FALSE</b> |
||
287 | */ |
||
288 | public function hasProperty($name) |
||
292 | |||
293 | /** |
||
294 | * (PHP 5)<br/> |
||
295 | * Gets a <b>ReflectionProperty</b> for a class's property |
||
296 | * @link http://php.net/manual/en/reflectionclass.getproperty.php |
||
297 | * @param string $name <p> |
||
298 | * The property name. |
||
299 | * </p> |
||
300 | * @return ReflectionProperty A <b>ReflectionProperty</b>. |
||
301 | */ |
||
302 | public function getProperty($name) |
||
306 | |||
307 | /** |
||
308 | * (PHP 5)<br/> |
||
309 | * Gets properties |
||
310 | * @link http://php.net/manual/en/reflectionclass.getproperties.php |
||
311 | * @param int $filter [optional] <p> |
||
312 | * The optional filter, for filtering desired property types. It's configured using |
||
313 | * the ReflectionProperty constants, |
||
314 | * and defaults to all property types. |
||
315 | * </p> |
||
316 | * @return array An array of <b>ReflectionProperty</b> objects. |
||
317 | */ |
||
318 | public function getProperties($filter = null) |
||
322 | |||
323 | /** |
||
324 | * (PHP 5 >= 5.1.0)<br/> |
||
325 | * Checks if constant is defined |
||
326 | * @link http://php.net/manual/en/reflectionclass.hasconstant.php |
||
327 | * @param string $name <p> |
||
328 | * The name of the constant being checked for. |
||
329 | * </p> |
||
330 | * @return bool <b>TRUE</b> if the constant is defined, otherwise <b>FALSE</b>. |
||
331 | */ |
||
332 | public function hasConstant($name) |
||
336 | |||
337 | /** |
||
338 | * (PHP 5)<br/> |
||
339 | * Gets constants |
||
340 | * @link http://php.net/manual/en/reflectionclass.getconstants.php |
||
341 | * @return array An array of constants. |
||
342 | * Constant name in key, constant value in value. |
||
343 | */ |
||
344 | public function getConstants() |
||
348 | |||
349 | /** |
||
350 | * (PHP 5)<br/> |
||
351 | * Gets defined constant |
||
352 | * @link http://php.net/manual/en/reflectionclass.getconstant.php |
||
353 | * @param string $name <p> |
||
354 | * Name of the constant. |
||
355 | * </p> |
||
356 | * @return mixed Value of the constant. |
||
357 | */ |
||
358 | public function getConstant($name) |
||
362 | |||
363 | /** |
||
364 | * (PHP 5)<br/> |
||
365 | * Gets the interfaces |
||
366 | * @link http://php.net/manual/en/reflectionclass.getinterfaces.php |
||
367 | * @return array An associative array of interfaces, with keys as interface |
||
368 | * names and the array values as <b>ReflectionClass</b> objects. |
||
369 | */ |
||
370 | public function getInterfaces() |
||
374 | |||
375 | /** |
||
376 | * (PHP 5 >= 5.2.0)<br/> |
||
377 | * Gets the interface names |
||
378 | * @link http://php.net/manual/en/reflectionclass.getinterfacenames.php |
||
379 | * @return array A numerical array with interface names as the values. |
||
380 | */ |
||
381 | public function getInterfaceNames() |
||
385 | |||
386 | /** |
||
387 | * (PHP 5)<br/> |
||
388 | * Checks if the class is an interface |
||
389 | * @link http://php.net/manual/en/reflectionclass.isinterface.php |
||
390 | * @return bool <b>TRUE</b> on success or <b>FALSE</b> on failure. |
||
391 | */ |
||
392 | public function isInterface() |
||
396 | |||
397 | /** |
||
398 | * (PHP >= 5.4.0)<br/> |
||
399 | * Returns an array of traits used by this class |
||
400 | * @link http://php.net/manual/en/reflectionclass.gettraits.php |
||
401 | * @return array an array with trait names in keys and instances of trait's |
||
402 | * <b>ReflectionClass</b> in values. |
||
403 | * Returns <b>NULL</b> in case of an error. |
||
404 | */ |
||
405 | public function getTraits() |
||
409 | |||
410 | /** |
||
411 | * (PHP >= 5.4.0)<br/> |
||
412 | * Returns an array of names of traits used by this class |
||
413 | * @link http://php.net/manual/en/reflectionclass.gettraitnames.php |
||
414 | * @return array an array with trait names in values. |
||
415 | * Returns <b>NULL</b> in case of an error. |
||
416 | */ |
||
417 | public function getTraitNames() |
||
421 | |||
422 | /** |
||
423 | * (PHP >= 5.4.0)<br/> |
||
424 | * Returns an array of trait aliases |
||
425 | * @link http://php.net/manual/en/reflectionclass.gettraitaliases.php |
||
426 | * @return array an array with new method names in keys and original names (in the |
||
427 | * format "TraitName::original") in values. |
||
428 | * Returns <b>NULL</b> in case of an error. |
||
429 | */ |
||
430 | public function getTraitAliases() |
||
434 | |||
435 | /** |
||
436 | * (PHP >= 5.4.0)<br/> |
||
437 | * Returns whether this is a trait |
||
438 | * @link http://php.net/manual/en/reflectionclass.istrait.php |
||
439 | * @return bool <b>TRUE</b> if this is a trait, <b>FALSE</b> otherwise. |
||
440 | * Returns <b>NULL</b> in case of an error. |
||
441 | */ |
||
442 | public function isTrait() |
||
446 | |||
447 | /** |
||
448 | * (PHP 5)<br/> |
||
449 | * Checks if class is abstract |
||
450 | * @link http://php.net/manual/en/reflectionclass.isabstract.php |
||
451 | * @return bool <b>TRUE</b> on success or <b>FALSE</b> on failure. |
||
452 | */ |
||
453 | public function isAbstract() |
||
457 | |||
458 | /** |
||
459 | * (PHP 5)<br/> |
||
460 | * Checks if class is final |
||
461 | * @link http://php.net/manual/en/reflectionclass.isfinal.php |
||
462 | * @return bool <b>TRUE</b> on success or <b>FALSE</b> on failure. |
||
463 | */ |
||
464 | public function isFinal() |
||
468 | |||
469 | /** |
||
470 | * (PHP 5)<br/> |
||
471 | * Gets modifiers |
||
472 | * @link http://php.net/manual/en/reflectionclass.getmodifiers.php |
||
473 | * @return int bitmask of |
||
474 | * modifier constants. |
||
475 | */ |
||
476 | public function getModifiers() |
||
480 | |||
481 | /** |
||
482 | * (PHP 5)<br/> |
||
483 | * Checks class for instance |
||
484 | * @link http://php.net/manual/en/reflectionclass.isinstance.php |
||
485 | * @param object $object <p> |
||
486 | * The object being compared to. |
||
487 | * </p> |
||
488 | * @return bool <b>TRUE</b> on success or <b>FALSE</b> on failure. |
||
489 | */ |
||
490 | public function isInstance($object) |
||
494 | |||
495 | /** |
||
496 | * (PHP 5)<br/> |
||
497 | * Creates a new class instance from given arguments. |
||
498 | * @link http://php.net/manual/en/reflectionclass.newinstance.php |
||
499 | * @param mixed $args <p> |
||
500 | * Accepts a variable number of arguments which are passed to the class |
||
501 | * constructor, much like <b>call_user_func</b>. |
||
502 | * </p> |
||
503 | * @param mixed $_ [optional] |
||
504 | * @return object |
||
505 | */ |
||
506 | public function newInstance($args, $_ = null) |
||
510 | |||
511 | /** |
||
512 | * (PHP >= 5.4.0)<br/> |
||
513 | * Creates a new class instance without invoking the constructor. |
||
514 | * @link http://php.net/manual/en/reflectionclass.newinstancewithoutconstructor.php |
||
515 | * @return object |
||
516 | */ |
||
517 | public function newInstanceWithoutConstructor() |
||
521 | |||
522 | /** |
||
523 | * (PHP 5 >= 5.1.3)<br/> |
||
524 | * Creates a new class instance from given arguments. |
||
525 | * @link http://php.net/manual/en/reflectionclass.newinstanceargs.php |
||
526 | * @param array $args [optional] <p> |
||
527 | * The parameters to be passed to the class constructor as an array. |
||
528 | * </p> |
||
529 | * @return object a new instance of the class. |
||
530 | */ |
||
531 | public function newInstanceArgs(array $args = null) |
||
535 | |||
536 | /** |
||
537 | * (PHP 5)<br/> |
||
538 | * Gets parent class |
||
539 | * @link http://php.net/manual/en/reflectionclass.getparentclass.php |
||
540 | * @return object A <b>ReflectionClass</b>. |
||
541 | */ |
||
542 | public function getParentClass() |
||
546 | |||
547 | /** |
||
548 | * (PHP 5)<br/> |
||
549 | * Checks if a subclass |
||
550 | * @link http://php.net/manual/en/reflectionclass.issubclassof.php |
||
551 | * @param string $class <p> |
||
552 | * The class name being checked against. |
||
553 | * </p> |
||
554 | * @return bool <b>TRUE</b> on success or <b>FALSE</b> on failure. |
||
555 | */ |
||
556 | public function isSubclassOf($class) |
||
560 | |||
561 | /** |
||
562 | * (PHP 5)<br/> |
||
563 | * Gets static properties |
||
564 | * @link http://php.net/manual/en/reflectionclass.getstaticproperties.php |
||
565 | * @return array The static properties, as an array. |
||
566 | */ |
||
567 | public function getStaticProperties() |
||
571 | |||
572 | /** |
||
573 | * (PHP 5 >= 5.1.0)<br/> |
||
574 | * Gets static property value |
||
575 | * @link http://php.net/manual/en/reflectionclass.getstaticpropertyvalue.php |
||
576 | * @param string $name <p> |
||
577 | * The name of the static property for which to return a value. |
||
578 | * </p> |
||
579 | * @param mixed $def_value [optional] <p> |
||
580 | * </p> |
||
581 | * @return mixed The value of the static property. |
||
582 | */ |
||
583 | public function getStaticPropertyValue($name, &$def_value = null) |
||
587 | |||
588 | /** |
||
589 | * (PHP 5 >= 5.1.0)<br/> |
||
590 | * Sets static property value |
||
591 | * @link http://php.net/manual/en/reflectionclass.setstaticpropertyvalue.php |
||
592 | * @param string $name <p> |
||
593 | * Property name. |
||
594 | * </p> |
||
595 | * @param string $value <p> |
||
596 | * New property value. |
||
597 | * </p> |
||
598 | * @return void No value is returned. |
||
599 | */ |
||
600 | public function setStaticPropertyValue($name, $value) |
||
604 | |||
605 | /** |
||
606 | * (PHP 5)<br/> |
||
607 | * Gets default properties |
||
608 | * @link http://php.net/manual/en/reflectionclass.getdefaultproperties.php |
||
609 | * @return array An array of default properties, with the key being the name of |
||
610 | * the property and the value being the default value of the property or <b>NULL</b> |
||
611 | * if the property doesn't have a default value. The function does not distinguish |
||
612 | * between static and non static properties and does not take visibility modifiers |
||
613 | * into account. |
||
614 | */ |
||
615 | public function getDefaultProperties() |
||
619 | |||
620 | /** |
||
621 | * (PHP 5)<br/> |
||
622 | * Checks if iterateable |
||
623 | * @link http://php.net/manual/en/reflectionclass.isiterateable.php |
||
624 | * @return bool <b>TRUE</b> on success or <b>FALSE</b> on failure. |
||
625 | */ |
||
626 | public function isIterateable() |
||
630 | |||
631 | /** |
||
632 | * (PHP 5)<br/> |
||
633 | * Implements interface |
||
634 | * @link http://php.net/manual/en/reflectionclass.implementsinterface.php |
||
635 | * @param string $interface <p> |
||
636 | * The interface name. |
||
637 | * </p> |
||
638 | * @return bool <b>TRUE</b> on success or <b>FALSE</b> on failure. |
||
639 | */ |
||
640 | public function implementsInterface($interface) |
||
644 | |||
645 | /** |
||
646 | * (PHP 5)<br/> |
||
647 | * Gets a <b>ReflectionExtension</b> object for the extension which defined the class |
||
648 | * @link http://php.net/manual/en/reflectionclass.getextension.php |
||
649 | * @return ReflectionExtension A <b>ReflectionExtension</b> object representing the extension which defined the class, |
||
650 | * or <b>NULL</b> for user-defined classes. |
||
651 | */ |
||
652 | public function getExtension() |
||
656 | |||
657 | /** |
||
658 | * (PHP 5)<br/> |
||
659 | * Gets the name of the extension which defined the class |
||
660 | * @link http://php.net/manual/en/reflectionclass.getextensionname.php |
||
661 | * @return string The name of the extension which defined the class, or <b>FALSE</b> for user-defined classes. |
||
662 | */ |
||
663 | public function getExtensionName() |
||
667 | |||
668 | /** |
||
669 | * (PHP 5 >= 5.3.0)<br/> |
||
670 | * Checks if in namespace |
||
671 | * @link http://php.net/manual/en/reflectionclass.innamespace.php |
||
672 | * @return bool <b>TRUE</b> on success or <b>FALSE</b> on failure. |
||
673 | */ |
||
674 | public function inNamespace() |
||
678 | |||
679 | /** |
||
680 | * (PHP 5 >= 5.3.0)<br/> |
||
681 | * Gets namespace name |
||
682 | * @link http://php.net/manual/en/reflectionclass.getnamespacename.php |
||
683 | * @return string The namespace name. |
||
684 | */ |
||
685 | public function getNamespaceName() |
||
689 | |||
690 | /** |
||
691 | * (PHP 5 >= 5.3.0)<br/> |
||
692 | * Gets short name |
||
693 | * @link http://php.net/manual/en/reflectionclass.getshortname.php |
||
694 | * @return string The class short name. |
||
695 | */ |
||
696 | public function getShortName() |
||
700 | |||
701 | } |
||
702 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..