Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like ClassGenerator 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 ClassGenerator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class ClassGenerator |
||
16 | { |
||
17 | /** |
||
18 | * @var ClassType |
||
19 | */ |
||
20 | protected $class; |
||
21 | |||
22 | /** |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $ownerCanonicalClassName; |
||
26 | |||
27 | /** |
||
28 | * @param string $value |
||
29 | * @return string |
||
30 | */ |
||
31 | protected static function camelCase(string $value): string |
||
35 | |||
36 | /** |
||
37 | * @param string $type |
||
38 | * @return string |
||
39 | */ |
||
40 | protected function getTypeHint(string $type) |
||
52 | |||
53 | /** |
||
54 | * @param string $type |
||
55 | * @return string |
||
56 | */ |
||
57 | protected function getCommentTypeHint(string $type) |
||
73 | |||
74 | /** |
||
75 | * @return string |
||
76 | */ |
||
77 | protected function getCanonicalClassName(): string |
||
81 | |||
82 | /** |
||
83 | * ClassGenerator constructor. |
||
84 | * |
||
85 | * @param Config $annotation |
||
86 | * @param string $className |
||
87 | * @param string $classNamespace |
||
88 | * @param string $ownerCanonicalClassName |
||
89 | */ |
||
90 | public function __construct( |
||
99 | |||
100 | /** |
||
101 | * @inheritDoc |
||
102 | */ |
||
103 | public function __toString() |
||
130 | |||
131 | /** |
||
132 | * @param string $name |
||
133 | * @param string $type |
||
134 | * @param null $default |
||
135 | * @return ClassGenerator |
||
136 | */ |
||
137 | public function generateProperty(string $name, string $type, $default = null): ClassGenerator |
||
146 | |||
147 | /** |
||
148 | * @param string $name |
||
149 | * @param string $type |
||
150 | * @return ClassGenerator |
||
151 | */ |
||
152 | View Code Duplication | public function generateGet(string $name, string $type): ClassGenerator |
|
168 | |||
169 | /** |
||
170 | * @param string $name |
||
171 | * @param string $type |
||
172 | * @return ClassGenerator |
||
173 | */ |
||
174 | public function generateSet(string $name, string $type): ClassGenerator |
||
189 | |||
190 | /** |
||
191 | * @param string $name |
||
192 | * @return ClassGenerator |
||
193 | */ |
||
194 | View Code Duplication | public function generateIsset(string $name): ClassGenerator |
|
206 | |||
207 | /** |
||
208 | * @param string $name |
||
209 | * @return ClassGenerator |
||
210 | */ |
||
211 | View Code Duplication | public function generateUnset(string $name): ClassGenerator |
|
224 | |||
225 | /** |
||
226 | * @param string $name |
||
227 | * @param string $type |
||
228 | * @return ClassGenerator |
||
229 | */ |
||
230 | public function generateListSet(string $name, string $type): ClassGenerator |
||
231 | { |
||
232 | $this->class |
||
233 | ->addMethod(static::camelCase('set_' . $name)) |
||
234 | ->addComment( |
||
235 | '@param ' . $this->getCommentTypeHint($type) . ' $values' . PHP_EOL . |
||
236 | '@return ' . $this->class->getName() |
||
237 | )->setReturnType($this->getCanonicalClassName()) |
||
238 | ->setBody( |
||
239 | '$this->' . static::camelCase('clear_' . $name) . '();' . PHP_EOL . |
||
240 | 'foreach ($values as $value) {' . PHP_EOL . |
||
241 | ' $this->' . static::camelCase('push_' . $name) . '($value);' . PHP_EOL . |
||
242 | '}' . PHP_EOL . |
||
243 | 'return $this;' |
||
244 | )->addParameter('values') |
||
245 | ->setTypeHint($this->getTypeHint($type)); |
||
246 | return $this; |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * @param string $name |
||
251 | * @param string $type |
||
252 | * @return ClassGenerator |
||
253 | */ |
||
254 | public function generateListGetAt(string $name, string $type): ClassGenerator |
||
255 | { |
||
256 | $this->class |
||
257 | ->addMethod(static::camelCase('get_' . $name . '_at')) |
||
258 | ->addComment( |
||
259 | '@param int $index' . PHP_EOL . |
||
260 | '@return ' . $this->getCommentTypeHint($type) |
||
261 | )->setReturnType($this->getTypeHint($type)) |
||
262 | ->setReturnNullable(true) |
||
263 | ->setBody( |
||
264 | 'if (isset($this->__' . $name . '__[0]) && array_key_exists($index, $this->__' . $name . '__[0])) {' . |
||
265 | PHP_EOL . ' return $this->__' . $name . '__[0][$index];' . PHP_EOL . |
||
266 | '}' . PHP_EOL . |
||
267 | 'return null;' |
||
268 | )->addParameter('index') |
||
269 | ->setTypeHint('int'); |
||
270 | return $this; |
||
271 | } |
||
272 | |||
273 | /** |
||
274 | * @param string $name |
||
275 | * @param string $type |
||
276 | * @return ClassGenerator |
||
277 | */ |
||
278 | public function generateListSetAt(string $name, string $type): ClassGenerator |
||
279 | { |
||
280 | $method = $this->class |
||
281 | ->addMethod(static::camelCase('set_' . $name . '_at')) |
||
282 | ->addComment( |
||
283 | '@param int $index' . PHP_EOL . |
||
284 | '@param ' . $this->getCommentTypeHint($type) . ' $value' . PHP_EOL . |
||
285 | '@return ' . $this->class->getName() |
||
286 | )->setReturnType($this->getCanonicalClassName()) |
||
287 | ->setBody( |
||
288 | 'if (0 > $index || (0 < $index && (!isset($this->__' . $name . '__[0]) ||' . PHP_EOL . |
||
289 | ' empty($this->__' . $name . '__[0])) || $index > count($this->__' . $name . '__[0]))) {' . PHP_EOL . |
||
290 | ' return $this;' . PHP_EOL . |
||
291 | '}' . PHP_EOL . PHP_EOL . |
||
292 | 'if (!isset($this->__' . $name . '__[0])) {' . PHP_EOL . |
||
293 | ' $this->__' . $name . '__[0] = [];' . PHP_EOL . |
||
294 | '}' . PHP_EOL . PHP_EOL . |
||
295 | '$this->__' . $name . '__[0][$index] = $value;' . PHP_EOL . |
||
296 | 'return $this;' |
||
297 | ); |
||
298 | |||
299 | $method->addParameter('index')->setTypeHint('int'); |
||
300 | $method->addParameter('value')->setTypeHint($this->getTypeHint($type)); |
||
301 | |||
302 | return $this; |
||
303 | } |
||
304 | |||
305 | /** |
||
306 | * @param string $name |
||
307 | * @param string $type |
||
308 | * @return ClassGenerator |
||
309 | */ |
||
310 | View Code Duplication | public function generateListPush(string $name, string $type): ClassGenerator |
|
311 | { |
||
312 | $this->class |
||
313 | ->addMethod(static::camelCase('push_' . $name)) |
||
314 | ->addComment( |
||
315 | '@param ' . $this->getCommentTypeHint($type) . ' $value' . PHP_EOL . |
||
316 | '@return ' . $this->class->getName() |
||
317 | )->setReturnType($this->getCanonicalClassName()) |
||
318 | ->setBody( |
||
319 | 'if (!isset($this->__' . $name . '__[0])) {' . PHP_EOL . |
||
320 | ' $this->__' . $name . '__[0] = [];' . PHP_EOL . |
||
321 | '}' . PHP_EOL . |
||
322 | 'array_push($this->__' . $name . '__[0], $value);' . PHP_EOL . |
||
323 | 'return $this;' |
||
324 | )->addParameter('value') |
||
325 | ->setTypeHint($this->getTypeHint($type)); |
||
326 | return $this; |
||
327 | } |
||
328 | |||
329 | /** |
||
330 | * @param string $name |
||
331 | * @param string $type |
||
332 | * @return ClassGenerator |
||
333 | */ |
||
334 | View Code Duplication | public function generateListUnshift(string $name, string $type): ClassGenerator |
|
335 | { |
||
336 | $this->class |
||
337 | ->addMethod(static::camelCase('unshift_' . $name)) |
||
338 | ->addComment( |
||
339 | '@param ' . $this->getCommentTypeHint($type) . ' $value' . PHP_EOL . |
||
340 | '@return ' . $this->class->getName() |
||
341 | )->setReturnType($this->getCanonicalClassName()) |
||
342 | ->setBody( |
||
343 | 'if (!isset($this->__' . $name . '__[0])) {' . PHP_EOL . |
||
344 | ' $this->__' . $name . '__[0] = [];' . PHP_EOL . |
||
345 | '}' . PHP_EOL . |
||
346 | 'array_unshift($this->__' . $name . '__[0], $value);' . PHP_EOL . |
||
347 | 'return $this;' |
||
348 | )->addParameter('value') |
||
349 | ->setTypeHint($this->getTypeHint($type)); |
||
350 | return $this; |
||
351 | } |
||
352 | |||
353 | /** |
||
354 | * @param string $name |
||
355 | * @param string $type |
||
356 | * @return ClassGenerator |
||
357 | */ |
||
358 | View Code Duplication | public function generateArrayPop(string $name, string $type): ClassGenerator |
|
359 | { |
||
360 | $this->class |
||
361 | ->addMethod(static::camelCase('pop_' . $name)) |
||
362 | ->addComment( |
||
363 | '@return null|' . $this->getCommentTypeHint($type) |
||
364 | )->setReturnType($this->getTypeHint($type)) |
||
365 | ->setReturnNullable(true) |
||
366 | ->setBody( |
||
367 | 'if (!isset($this->__' . $name . '__[0])) {' . PHP_EOL . |
||
368 | ' return null;' . PHP_EOL . |
||
369 | '}' . PHP_EOL . |
||
370 | 'return array_pop($this->__' . $name . '__[0]);' |
||
371 | ); |
||
372 | return $this; |
||
373 | } |
||
374 | |||
375 | /** |
||
376 | * @param string $name |
||
377 | * @param string $type |
||
378 | * @return ClassGenerator |
||
379 | */ |
||
380 | View Code Duplication | public function generateArrayShift(string $name, string $type): ClassGenerator |
|
381 | { |
||
382 | $this->class |
||
383 | ->addMethod(static::camelCase('shift_' . $name)) |
||
384 | ->addComment( |
||
385 | '@return null|' . $this->getCommentTypeHint($type) |
||
386 | )->setReturnType($this->getTypeHint($type)) |
||
387 | ->setReturnNullable(true) |
||
388 | ->setBody( |
||
389 | 'if (!isset($this->__' . $name . '__[0])) {' . PHP_EOL . |
||
390 | ' return null;' . PHP_EOL . |
||
391 | '}' . PHP_EOL . |
||
392 | 'return array_shift($this->__' . $name . '__[0]);' |
||
393 | ); |
||
394 | return $this; |
||
395 | } |
||
396 | |||
397 | /** |
||
398 | * @param string $name |
||
399 | * @return ClassGenerator |
||
400 | */ |
||
401 | View Code Duplication | public function generateArrayClear(string $name): ClassGenerator |
|
402 | { |
||
403 | $this->class |
||
404 | ->addMethod(static::camelCase('clear_' . $name)) |
||
405 | ->addComment( |
||
406 | '@return ' . $this->class->getName() |
||
407 | )->setReturnType($this->getCanonicalClassName()) |
||
408 | ->setBody( |
||
409 | 'unset($this->__' . $name . '__[0]);' . PHP_EOL . |
||
410 | 'return $this;' |
||
411 | ); |
||
412 | return $this; |
||
413 | } |
||
414 | |||
415 | /** |
||
416 | * @param string $name |
||
417 | * @param string $type |
||
418 | * @return ClassGenerator |
||
419 | */ |
||
420 | public function generateConfigGet(string $name, string $type): ClassGenerator |
||
436 | |||
437 | /** |
||
438 | * @param string $name |
||
439 | * @return ClassGenerator |
||
440 | */ |
||
441 | public function generateConfigSet(string $name): ClassGenerator |
||
454 | |||
455 | /** |
||
456 | * @param string $name |
||
457 | * @return ClassGenerator |
||
458 | */ |
||
459 | View Code Duplication | public function generateConfigIsset(string $name): ClassGenerator |
|
472 | |||
473 | /** |
||
474 | * @param string $name |
||
475 | * @return ClassGenerator |
||
476 | */ |
||
477 | public function generateConfigUnset(string $name): ClassGenerator |
||
490 | |||
491 | /** |
||
492 | * @return ClassGenerator |
||
493 | */ |
||
494 | View Code Duplication | public function generateMagicGet(): ClassGenerator |
|
526 | |||
527 | /** |
||
528 | * @return ClassGenerator |
||
529 | */ |
||
530 | public function generateMagicSet(): ClassGenerator |
||
567 | |||
568 | /** |
||
569 | * @return ClassGenerator |
||
570 | */ |
||
571 | View Code Duplication | public function generateMagicIsset(): ClassGenerator |
|
603 | |||
604 | /** |
||
605 | * @return ClassGenerator |
||
606 | */ |
||
607 | View Code Duplication | public function generateMagicUnset(): ClassGenerator |
|
639 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.