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:
1 | <?php declare(strict_types=1); |
||
13 | class PropertyGenerator extends AbstractGenerator |
||
14 | { |
||
15 | use VisibilityTrait; |
||
16 | |||
17 | /** @var string Property name */ |
||
18 | protected $name; |
||
19 | |||
20 | /** @var string Property value */ |
||
21 | protected $value; |
||
22 | |||
23 | /** |
||
24 | * PropertyGenerator constructor. |
||
25 | * |
||
26 | * @param string $name Property name |
||
27 | * @param mixed $value Property value |
||
28 | * @param AbstractGenerator|null $parent Parent generator |
||
29 | */ |
||
30 | 4 | public function __construct(string $name, $value = null, AbstractGenerator $parent = null) |
|
37 | |||
38 | /** |
||
39 | * Set property value. |
||
40 | * |
||
41 | * @param mixed $value Value |
||
42 | * @return $this|PropertyGenerator |
||
43 | */ |
||
44 | public function defValue($value) : PropertyGenerator |
||
50 | |||
51 | /** |
||
52 | * {@inheritdoc} |
||
53 | */ |
||
54 | 3 | public function code(): string |
|
72 | } |
||
73 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.