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 |
||
19 | class ClassDefinition extends ParentDefinition |
||
|
|||
20 | { |
||
21 | /** |
||
22 | * @var int |
||
23 | */ |
||
24 | protected $type; |
||
25 | |||
26 | /** |
||
27 | * Class methods |
||
28 | * |
||
29 | * @var ClassMethod[] |
||
30 | */ |
||
31 | protected $methods = []; |
||
32 | |||
33 | /** |
||
34 | * Class properties |
||
35 | * |
||
36 | * @var Node\Stmt\PropertyProperty[] |
||
37 | */ |
||
38 | protected $properties = []; |
||
39 | |||
40 | /** |
||
41 | * Property Statements |
||
42 | * |
||
43 | * @var Node\Stmt\Property[] |
||
44 | */ |
||
45 | protected $propertyStatements = []; |
||
46 | |||
47 | /** |
||
48 | * Class constants |
||
49 | * |
||
50 | * @var Node\Stmt\ClassConst[] |
||
51 | */ |
||
52 | protected $constants = []; |
||
53 | |||
54 | /** |
||
55 | * @todo Use Finder |
||
56 | * |
||
57 | * @var string |
||
58 | */ |
||
59 | protected $filepath; |
||
60 | |||
61 | /** |
||
62 | * @var Node\Stmt\Class_|null |
||
63 | */ |
||
64 | protected $statement; |
||
65 | |||
66 | /** |
||
67 | * @var string|null |
||
68 | */ |
||
69 | protected $extendsClass; |
||
70 | |||
71 | /** |
||
72 | * @var ClassDefinition|null |
||
73 | */ |
||
74 | protected $extendsClassDefinition; |
||
75 | |||
76 | /** |
||
77 | * @var array |
||
78 | */ |
||
79 | protected $interfaces = []; |
||
80 | |||
81 | /** |
||
82 | * @param string $name |
||
83 | * @param Node\Stmt\Class_ $statement |
||
84 | * @param integer $type |
||
85 | */ |
||
86 | 58 | public function __construct($name, Node\Stmt\Class_ $statement = null, $type = 0) |
|
87 | { |
||
88 | 58 | $this->name = $name; |
|
89 | 58 | $this->statement = $statement; |
|
90 | 58 | $this->type = $type; |
|
91 | 58 | } |
|
92 | |||
93 | /** |
||
94 | * @param ClassMethod $classMethod |
||
95 | * @param bool $overwrite Should we overwrite method if it already exists |
||
96 | * @return bool Did we overwrite method? |
||
97 | */ |
||
98 | 50 | public function addMethod(ClassMethod $classMethod, $overwrite = true) |
|
99 | { |
||
100 | 50 | if ($overwrite) { |
|
101 | 50 | $this->methods[$classMethod->getName()] = $classMethod; |
|
102 | 50 | } else { |
|
103 | $name = $classMethod->getName(); |
||
104 | if (isset($this->methods[$name])) { |
||
105 | return false; |
||
106 | } else { |
||
107 | $this->methods[$name] = $classMethod; |
||
108 | } |
||
109 | } |
||
110 | |||
111 | 50 | return true; |
|
112 | } |
||
113 | |||
114 | /** |
||
115 | * @param Node\Stmt\Property $property |
||
116 | */ |
||
117 | 7 | public function addProperty(Node\Stmt\Property $property) |
|
118 | { |
||
119 | 7 | foreach ($property->props as $propertyDefinition) { |
|
120 | 7 | $this->properties[$propertyDefinition->name] = $propertyDefinition; |
|
121 | 7 | $this->propertyStatements[$propertyDefinition->name] = $property; |
|
122 | 7 | } |
|
123 | 7 | } |
|
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 | 52 | public function compile(Context $context) |
|
138 | { |
||
139 | 52 | if ($this->compiled) { |
|
140 | return $this; |
||
141 | } |
||
142 | |||
143 | 52 | $this->compiled = true; |
|
144 | 52 | $context->setFilepath($this->filepath); |
|
145 | 52 | $context->setScope($this); |
|
146 | |||
147 | 52 | $context->getEventManager()->fire( |
|
148 | 52 | Event\StatementBeforeCompile::EVENT_NAME, |
|
149 | 52 | new Event\StatementBeforeCompile( |
|
150 | 52 | $this->statement, |
|
151 | $context |
||
152 | 52 | ) |
|
153 | 52 | ); |
|
154 | |||
155 | // Compile event for properties |
||
156 | 52 | foreach ($this->properties as $property) { |
|
157 | 6 | if (!$property->default) { |
|
158 | 2 | continue; |
|
159 | } |
||
160 | |||
161 | // fire expression event for property default |
||
162 | 6 | $context->getEventManager()->fire( |
|
163 | 6 | Event\ExpressionBeforeCompile::EVENT_NAME, |
|
164 | 6 | new Event\ExpressionBeforeCompile( |
|
165 | 6 | $property->default, |
|
166 | $context |
||
167 | 6 | ) |
|
168 | 6 | ); |
|
169 | 52 | } |
|
170 | |||
171 | // Compile event for PropertyProperty |
||
172 | 52 | foreach ($this->properties as $property) { |
|
173 | 6 | $context->getEventManager()->fire( |
|
174 | 6 | Event\StatementBeforeCompile::EVENT_NAME, |
|
175 | 6 | new Event\StatementBeforeCompile( |
|
176 | 6 | $property, |
|
177 | $context |
||
178 | 6 | ) |
|
179 | 6 | ); |
|
180 | 52 | } |
|
181 | |||
182 | // Compile event for constants |
||
183 | 52 | foreach ($this->constants as $const) { |
|
184 | 2 | $context->getEventManager()->fire( |
|
185 | 2 | Event\StatementBeforeCompile::EVENT_NAME, |
|
186 | 2 | new Event\StatementBeforeCompile( |
|
187 | 2 | $const, |
|
188 | $context |
||
189 | 2 | ) |
|
190 | 2 | ); |
|
191 | 52 | } |
|
192 | |||
193 | // Compiler event for property statements |
||
194 | 52 | foreach ($this->propertyStatements as $prop) { |
|
195 | 6 | $context->getEventManager()->fire( |
|
196 | 6 | Event\StatementBeforeCompile::EVENT_NAME, |
|
197 | 6 | new Event\StatementBeforeCompile( |
|
198 | 6 | $prop, |
|
199 | $context |
||
200 | 6 | ) |
|
201 | 6 | ); |
|
202 | 52 | } |
|
203 | |||
204 | // Compile each method |
||
205 | 52 | foreach ($this->methods as $method) { |
|
206 | 49 | $context->clearSymbols(); |
|
207 | |||
208 | 49 | if (!$method->isStatic()) { |
|
209 | 48 | $thisPtr = new Variable('this', $this, CompiledExpression::OBJECT); |
|
210 | 48 | $thisPtr->incGets(); |
|
211 | 48 | $context->addVariable($thisPtr); |
|
212 | 48 | } |
|
213 | |||
214 | 49 | $method->compile($context); |
|
215 | |||
216 | 49 | $symbols = $context->getSymbols(); |
|
217 | 49 | if (count($symbols) > 0) { |
|
218 | 49 | foreach ($symbols as $name => $variable) { |
|
219 | 49 | if (!$variable->isGlobal() && $variable->isUnused()) { |
|
220 | 12 | $context->warning( |
|
221 | 12 | 'unused-' . $variable->getSymbolType(), |
|
222 | 12 | sprintf( |
|
223 | 12 | 'Unused ' . $variable->getSymbolType() . ' $%s in method %s()', |
|
224 | 12 | $variable->getName(), |
|
225 | 12 | $method->getName() |
|
226 | 12 | ) |
|
227 | 12 | ); |
|
228 | 12 | } |
|
229 | 49 | } |
|
230 | 49 | } |
|
231 | 52 | } |
|
232 | |||
233 | 52 | return $this; |
|
234 | } |
||
235 | |||
236 | /** |
||
237 | * @param string $name |
||
238 | * @param boolean|false $inherit |
||
239 | * @return bool |
||
240 | */ |
||
241 | 4 | public function hasMethod($name, $inherit = false) |
|
242 | { |
||
243 | 4 | if (isset($this->methods[$name])) { |
|
244 | 2 | return true; |
|
245 | } |
||
246 | |||
247 | 3 | if ($inherit && $this->extendsClassDefinition && $this->extendsClassDefinition->hasMethod($name, $inherit)) { |
|
248 | 2 | $method = $this->extendsClassDefinition->getMethod($name, $inherit); |
|
249 | 2 | return $method && ($method->isPublic() || $method->isProtected()); |
|
250 | } |
||
251 | |||
252 | 1 | return false; |
|
253 | } |
||
254 | |||
255 | /** |
||
256 | * @param string $name |
||
257 | * @param bool $inherit |
||
258 | * @return bool |
||
259 | */ |
||
260 | 2 | public function hasConst($name, $inherit = false) |
|
268 | |||
269 | /** |
||
270 | * @param $name |
||
271 | * @param boolean|false $inherit |
||
272 | * @return ClassMethod|null |
||
273 | */ |
||
274 | 4 | public function getMethod($name, $inherit = false) |
|
286 | |||
287 | /** |
||
288 | * @param $name |
||
289 | * @param bool $inherit |
||
290 | * @param bool $isParent It's an internal parameter |
||
291 | * @return bool |
||
292 | */ |
||
293 | 3 | public function hasProperty($name, $inherit = false, $isParent = true) |
|
312 | |||
313 | /** |
||
314 | * @param string $name |
||
315 | * @param bool $inherit |
||
316 | * @return Node\Stmt\PropertyProperty|null |
||
317 | */ |
||
318 | public function getProperty($name, $inherit = false) |
||
330 | |||
331 | /** |
||
332 | * @param string $name |
||
333 | * @param bool $inherit |
||
334 | * @return Node\Stmt\Property|null |
||
335 | */ |
||
336 | public function getPropertyStatement($name, $inherit = false) |
||
348 | |||
349 | /** |
||
350 | * @return string |
||
351 | */ |
||
352 | 52 | public function getFilepath() |
|
356 | |||
357 | /** |
||
358 | * @param string $filepath |
||
359 | */ |
||
360 | 52 | public function setFilepath($filepath) |
|
364 | |||
365 | /** |
||
366 | * @return bool |
||
367 | */ |
||
368 | 1 | public function isAbstract() |
|
372 | |||
373 | /** |
||
374 | * @return bool |
||
375 | */ |
||
376 | 3 | public function isFinal() |
|
380 | |||
381 | /** |
||
382 | * @param null|string $extendsClass |
||
383 | */ |
||
384 | 2 | public function setExtendsClass($extendsClass) |
|
388 | |||
389 | /** |
||
390 | * @return null|ClassDefinition |
||
391 | */ |
||
392 | public function getExtendsClassDefinition() |
||
396 | |||
397 | /** |
||
398 | * @param ClassDefinition $extendsClassDefinition |
||
399 | */ |
||
400 | 3 | public function setExtendsClassDefinition(ClassDefinition $extendsClassDefinition) |
|
404 | |||
405 | /** |
||
406 | * @param string $interface |
||
407 | */ |
||
408 | public function addInterface($interface) |
||
412 | |||
413 | /** |
||
414 | * @return null|string |
||
415 | */ |
||
416 | 52 | public function getExtendsClass() |
|
420 | |||
421 | /** |
||
422 | * @param TraitDefinition $definition |
||
423 | * @param Node\Stmt\TraitUseAdaptation\Alias[] $adaptations |
||
424 | */ |
||
425 | public function mergeTrait(TraitDefinition $definition, array $adaptations) |
||
454 | |||
455 | /** |
||
456 | * @return ClassMethod[] |
||
457 | */ |
||
458 | public function getMethods() |
||
462 | } |
||
463 |
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.