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