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