Complex classes like ClassDefinition 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 ClassDefinition, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class ClassDefinition extends ParentDefinition |
||
|
|||
19 | { |
||
20 | /** |
||
21 | * @var int |
||
22 | */ |
||
23 | protected $type; |
||
24 | |||
25 | /** |
||
26 | * Class methods |
||
27 | * |
||
28 | * @var ClassMethod[] |
||
29 | */ |
||
30 | protected $methods = array(); |
||
31 | |||
32 | /** |
||
33 | * Class properties |
||
34 | * |
||
35 | * @var Node\Stmt\PropertyProperty[] |
||
36 | */ |
||
37 | protected $properties = array(); |
||
38 | |||
39 | /** |
||
40 | * Property Statements |
||
41 | * |
||
42 | * @var Node\Stmt\Property[] |
||
43 | */ |
||
44 | protected $propertyStatements = array(); |
||
45 | |||
46 | /** |
||
47 | * Class constants |
||
48 | * |
||
49 | * @var Node\Stmt\Const_[] |
||
50 | */ |
||
51 | protected $constants = array(); |
||
52 | |||
53 | /** |
||
54 | * @todo Use Finder |
||
55 | * |
||
56 | * @var string |
||
57 | */ |
||
58 | protected $filepath; |
||
59 | |||
60 | /** |
||
61 | * @var Node\Stmt\Class_|null |
||
62 | */ |
||
63 | protected $statement; |
||
64 | |||
65 | /** |
||
66 | * @var string|null |
||
67 | */ |
||
68 | protected $extendsClass; |
||
69 | |||
70 | /** |
||
71 | * @var ClassDefinition|null |
||
72 | */ |
||
73 | protected $extendsClassDefinition; |
||
74 | |||
75 | /** |
||
76 | * @var array |
||
77 | */ |
||
78 | protected $interfaces = array(); |
||
79 | |||
80 | /** |
||
81 | * @param string $name |
||
82 | * @param Node\Stmt\Class_ $statement |
||
83 | * @param integer $type |
||
84 | */ |
||
85 | 874 | public function __construct($name, Node\Stmt\Class_ $statement = null, $type = 0) |
|
91 | |||
92 | /** |
||
93 | * @param ClassMethod $classMethod |
||
94 | * @param bool $overwrite Should we overwrite method if it already exists |
||
95 | * @return bool Did we overwrite method? |
||
96 | */ |
||
97 | 40 | public function addMethod(ClassMethod $classMethod, $overwrite = true) |
|
98 | { |
||
99 | 40 | if ($overwrite) { |
|
100 | 40 | $this->methods[$classMethod->getName()] = $classMethod; |
|
101 | 40 | } else { |
|
102 | 1 | $name = $classMethod->getName(); |
|
103 | if (isset($this->methods[$name])) { |
||
104 | return false; |
||
105 | } else { |
||
106 | $this->methods[$name] = $classMethod; |
||
107 | } |
||
108 | } |
||
109 | |||
110 | 40 | return true; |
|
111 | } |
||
112 | |||
113 | /** |
||
114 | * @param Node\Stmt\Property $property |
||
115 | */ |
||
116 | 7 | public function addProperty(Node\Stmt\Property $property) |
|
124 | |||
125 | /** |
||
126 | * @param Node\Stmt\ClassConst $const |
||
127 | */ |
||
128 | 4 | public function addConst(Node\Stmt\ClassConst $const) |
|
132 | |||
133 | /** |
||
134 | * @param Context $context |
||
135 | * @return $this |
||
136 | */ |
||
137 | 41 | public function compile(Context $context) |
|
138 | { |
||
139 | 41 | if ($this->compiled) { |
|
140 | return $this; |
||
141 | } |
||
142 | |||
143 | 41 | $context->getEventManager()->fire( |
|
144 | 41 | Event\StatementBeforeCompile::EVENT_NAME, |
|
145 | 41 | new Event\StatementBeforeCompile( |
|
146 | 41 | $this->statement, |
|
147 | $context |
||
148 | 41 | ) |
|
149 | 41 | ); |
|
150 | |||
151 | 41 | $this->compiled = true; |
|
152 | 41 | $context->setFilepath($this->filepath); |
|
153 | 41 | $context->setScope($this); |
|
154 | |||
155 | // Compile event for properties |
||
156 | 41 | foreach ($this->properties as $property) { |
|
157 | 6 | if (!$property->default) { |
|
158 | 2 | continue; |
|
159 | } |
||
160 | |||
161 | // fire expression event for property default |
||
162 | 5 | $context->getEventManager()->fire( |
|
163 | 5 | Event\ExpressionBeforeCompile::EVENT_NAME, |
|
164 | 5 | new Event\ExpressionBeforeCompile( |
|
165 | 5 | $property->default, |
|
166 | $context |
||
167 | 5 | ) |
|
168 | 5 | ); |
|
169 | 41 | } |
|
170 | |||
171 | // Compile event for constants |
||
172 | 41 | foreach ($this->constants as $const) { |
|
173 | 2 | $context->getEventManager()->fire( |
|
174 | 2 | Event\StatementBeforeCompile::EVENT_NAME, |
|
175 | 2 | new Event\StatementBeforeCompile( |
|
176 | 2 | $const, |
|
177 | $context |
||
178 | 2 | ) |
|
179 | 2 | ); |
|
180 | 41 | } |
|
181 | |||
182 | // Compiler event for property statements |
||
183 | 41 | foreach ($this->propertyStatements as $prop) { |
|
184 | 6 | $context->getEventManager()->fire( |
|
185 | 6 | Event\StatementBeforeCompile::EVENT_NAME, |
|
186 | 6 | new Event\StatementBeforeCompile( |
|
187 | 6 | $prop, |
|
188 | $context |
||
189 | 6 | ) |
|
190 | 6 | ); |
|
191 | 41 | } |
|
192 | |||
193 | // Compile each method |
||
194 | 41 | foreach ($this->methods as $method) { |
|
195 | 39 | $context->clearSymbols(); |
|
196 | |||
197 | 39 | if (!$method->isStatic()) { |
|
198 | 38 | $thisPtr = new Variable('this', $this, CompiledExpression::OBJECT); |
|
199 | 38 | $thisPtr->incGets(); |
|
200 | 38 | $context->addVariable($thisPtr); |
|
201 | 38 | } |
|
202 | |||
203 | 39 | $method->compile($context); |
|
204 | |||
205 | 39 | foreach ($context->getSymbols() as $name => $variable) { |
|
206 | 38 | if (!$variable->isUnused()) { |
|
207 | 38 | continue; |
|
208 | } |
||
209 | |||
210 | 9 | $context->notice( |
|
211 | 9 | 'unused-' . $variable->getSymbolType(), |
|
212 | 9 | sprintf( |
|
213 | 9 | 'Unused ' . $variable->getSymbolType() . ' $%s in method %s()', |
|
214 | 9 | $variable->getName(), |
|
215 | 9 | $method->getName() |
|
216 | 9 | ), |
|
217 | 9 | $variable->getDeclarationStatement() |
|
218 | 9 | ); |
|
219 | 39 | } |
|
220 | 41 | } |
|
221 | |||
222 | 41 | return $this; |
|
223 | } |
||
224 | |||
225 | /** |
||
226 | * @param string $name |
||
227 | * @param boolean|false $inherit |
||
228 | * @return bool |
||
229 | */ |
||
230 | 2 | public function hasMethod($name, $inherit = false) |
|
243 | |||
244 | /** |
||
245 | * @param string $name |
||
246 | * @param bool $inherit |
||
247 | * @return bool |
||
248 | */ |
||
249 | 2 | public function hasConst($name, $inherit = false) |
|
257 | |||
258 | /** |
||
259 | * @param $name |
||
260 | * @param boolean|false $inherit |
||
261 | * @return ClassMethod|null |
||
262 | */ |
||
263 | 2 | public function getMethod($name, $inherit = false) |
|
275 | |||
276 | /** |
||
277 | * @param $name |
||
278 | * @param bool $inherit |
||
279 | * @return bool |
||
280 | */ |
||
281 | 3 | public function hasProperty($name, $inherit = false) |
|
289 | |||
290 | /** |
||
291 | * @param string $name |
||
292 | * @param bool $inherit |
||
293 | * @return Node\Stmt\PropertyProperty |
||
294 | */ |
||
295 | public function getProperty($name, $inherit = false) |
||
309 | |||
310 | /** |
||
311 | * @param string $name |
||
312 | * @param bool $inherit |
||
313 | * @return Node\Stmt\Property |
||
314 | */ |
||
315 | public function getPropertyStatement($name, $inherit = false) |
||
316 | { |
||
317 | if (isset($this->propertyStatements[$name])) { |
||
318 | return $this->propertyStatements[$name]; |
||
319 | } |
||
320 | |||
321 | if ($inherit && $this->extendsClassDefinition) { |
||
322 | return $this->extendsClassDefinition->getPropertyStatement($name, true); |
||
323 | } |
||
324 | |||
325 | return null; |
||
326 | } |
||
327 | |||
328 | /** |
||
329 | * @return string |
||
330 | */ |
||
331 | 41 | public function getFilepath() |
|
332 | { |
||
333 | 41 | return $this->filepath; |
|
334 | } |
||
335 | |||
336 | /** |
||
337 | * @param string $filepath |
||
338 | */ |
||
339 | 41 | public function setFilepath($filepath) |
|
340 | { |
||
341 | 41 | $this->filepath = $filepath; |
|
342 | 41 | } |
|
343 | |||
344 | /** |
||
345 | * @return bool |
||
346 | */ |
||
347 | public function isAbstract() |
||
351 | |||
352 | /** |
||
353 | * @return bool |
||
354 | */ |
||
355 | 1 | public function isFinal() |
|
356 | { |
||
357 | 1 | return (bool) ($this->type & Node\Stmt\Class_::MODIFIER_FINAL); |
|
358 | } |
||
359 | |||
360 | /** |
||
361 | * @param null|string $extendsClass |
||
362 | */ |
||
363 | public function setExtendsClass($extendsClass) |
||
364 | { |
||
365 | $this->extendsClass = $extendsClass; |
||
366 | } |
||
367 | |||
368 | /** |
||
369 | * @return null|ClassDefinition |
||
370 | */ |
||
371 | public function getExtendsClassDefinition() |
||
375 | |||
376 | /** |
||
377 | * @param ClassDefinition $extendsClassDefinition |
||
378 | */ |
||
379 | 1 | public function setExtendsClassDefinition(ClassDefinition $extendsClassDefinition) |
|
380 | { |
||
381 | 1 | $this->extendsClassDefinition = $extendsClassDefinition; |
|
382 | 1 | } |
|
383 | |||
384 | /** |
||
385 | * @param string $interface |
||
386 | */ |
||
387 | public function addInterface($interface) |
||
391 | |||
392 | /** |
||
393 | * @return null|string |
||
394 | */ |
||
395 | 41 | public function getExtendsClass() |
|
396 | { |
||
397 | 41 | return $this->extendsClass; |
|
399 | |||
400 | /** |
||
401 | * @param TraitDefinition $definition |
||
402 | * @param Node\Stmt\TraitUseAdaptation\Alias[] $adaptations |
||
403 | */ |
||
404 | public function mergeTrait(TraitDefinition $definition, array $adaptations) |
||
433 | } |
||
434 |
The class complexity is the sum of the complexity of all methods. A very high value is usually an indication that your class does not follow the single reponsibility principle and does more than one job.
Some resources for further reading:
You can also find more detailed suggestions for refactoring in the “Code” section of your repository.