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 |
||
17 | trait PropertyTrait |
||
18 | { |
||
19 | /** @var array Cached annotations. */ |
||
20 | private static $_annotations; |
||
21 | |||
22 | /** |
||
23 | * {@inheritdoc} |
||
24 | */ |
||
25 | public function __isset($name) |
||
33 | |||
34 | /** |
||
35 | * {@inheritdoc} |
||
36 | */ |
||
37 | 6 | View Code Duplication | public function __get($name) |
55 | |||
56 | /** |
||
57 | * {@inheritdoc} |
||
58 | */ |
||
59 | 7 | View Code Duplication | public function __set($name, $value) |
77 | |||
78 | /** |
||
79 | * Returns array of custom getters. |
||
80 | * |
||
81 | * @return array |
||
82 | */ |
||
83 | 1 | protected function getters(): array |
|
84 | { |
||
85 | 1 | return []; |
|
86 | } |
||
87 | |||
88 | /** |
||
89 | * Returns array of custom setters. |
||
90 | * |
||
91 | * @return array |
||
92 | */ |
||
93 | 1 | protected function setters(): array |
|
94 | { |
||
95 | 1 | return []; |
|
96 | } |
||
97 | |||
98 | /** |
||
99 | * Parses annotations of the class. |
||
100 | */ |
||
101 | 1 | private function parseAnnotations() |
|
115 | } |
||
116 |
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.