Complex classes like ClassDescriptor 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 ClassDescriptor, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class ClassDescriptor extends DescriptorAbstract implements Interfaces\ClassInterface |
||
18 | { |
||
19 | /** @var ClassDescriptor|null $extends Reference to an instance of the superclass for this class, if any. */ |
||
20 | protected $parent; |
||
21 | |||
22 | /** @var Collection $implements References to interfaces that are implemented by this class. */ |
||
23 | protected $implements; |
||
24 | |||
25 | /** @var boolean $abstract Whether this is an abstract class. */ |
||
26 | protected $abstract = false; |
||
27 | |||
28 | /** @var boolean $final Whether this class is marked as final and can't be subclassed. */ |
||
29 | protected $final = false; |
||
30 | |||
31 | /** @var Collection $constants References to constants defined in this class. */ |
||
32 | protected $constants; |
||
33 | |||
34 | /** @var Collection $properties References to properties defined in this class. */ |
||
35 | protected $properties; |
||
36 | |||
37 | /** @var Collection $methods References to methods defined in this class. */ |
||
38 | protected $methods; |
||
39 | |||
40 | /** @var Collection $usedTraits References to traits consumed by this class */ |
||
41 | protected $usedTraits = array(); |
||
42 | |||
43 | /** |
||
44 | * Initializes the all properties representing a collection with a new Collection object. |
||
45 | */ |
||
46 | public function __construct() |
||
56 | |||
57 | /** |
||
58 | * {@inheritDoc} |
||
59 | */ |
||
60 | public function setParent($parents) |
||
64 | |||
65 | /** |
||
66 | * {@inheritDoc} |
||
67 | */ |
||
68 | public function getParent() |
||
72 | |||
73 | /** |
||
74 | * {@inheritDoc} |
||
75 | */ |
||
76 | public function setInterfaces(Collection $implements) |
||
80 | |||
81 | /** |
||
82 | * {@inheritDoc} |
||
83 | */ |
||
84 | public function getInterfaces() |
||
88 | |||
89 | /** |
||
90 | * {@inheritDoc} |
||
91 | */ |
||
92 | public function setFinal($final) |
||
96 | |||
97 | /** |
||
98 | * {@inheritDoc} |
||
99 | */ |
||
100 | public function isFinal() |
||
104 | |||
105 | /** |
||
106 | * {@inheritDoc} |
||
107 | */ |
||
108 | public function setAbstract($abstract) |
||
112 | |||
113 | /** |
||
114 | * {@inheritDoc} |
||
115 | */ |
||
116 | public function isAbstract() |
||
120 | |||
121 | /** |
||
122 | * {@inheritDoc} |
||
123 | */ |
||
124 | public function setConstants(Collection $constants) |
||
128 | |||
129 | /** |
||
130 | * {@inheritDoc} |
||
131 | */ |
||
132 | public function getConstants() |
||
136 | |||
137 | /** |
||
138 | * {@inheritDoc} |
||
139 | */ |
||
140 | public function getInheritedConstants() |
||
150 | |||
151 | /** |
||
152 | * {@inheritDoc} |
||
153 | */ |
||
154 | public function setMethods(Collection $methods) |
||
158 | |||
159 | /** |
||
160 | * {@inheritDoc} |
||
161 | */ |
||
162 | public function getMethods() |
||
166 | |||
167 | /** |
||
168 | * {@inheritDoc} |
||
169 | */ |
||
170 | public function getInheritedMethods() |
||
190 | |||
191 | /** |
||
192 | * @return Collection |
||
193 | */ |
||
194 | public function getMagicMethods() |
||
224 | |||
225 | /** |
||
226 | * {@inheritDoc} |
||
227 | */ |
||
228 | public function setProperties(Collection $properties) |
||
232 | |||
233 | /** |
||
234 | * {@inheritDoc} |
||
235 | */ |
||
236 | public function getProperties() |
||
240 | |||
241 | /** |
||
242 | * {@inheritDoc} |
||
243 | */ |
||
244 | public function getInheritedProperties() |
||
264 | |||
265 | /** |
||
266 | * @return Collection |
||
267 | */ |
||
268 | public function getMagicProperties() |
||
294 | |||
295 | /** |
||
296 | * @param string $package |
||
297 | */ |
||
298 | public function setPackage($package) |
||
323 | |||
324 | /** |
||
325 | * Sets a collection of all traits used by this class. |
||
326 | * |
||
327 | * @param Collection $usedTraits |
||
328 | * |
||
329 | * @return void |
||
330 | */ |
||
331 | public function setUsedTraits($usedTraits) |
||
335 | |||
336 | /** |
||
337 | * Returns the traits used by this class. |
||
338 | * |
||
339 | * Returned values may either be a string (when the Trait is not in this project) or a TraitDescriptor. |
||
340 | * |
||
341 | * @return Collection |
||
342 | */ |
||
343 | public function getUsedTraits() |
||
347 | |||
348 | public function getInheritedElement() |
||
352 | } |
||
353 |
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..