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 |
||
8 | trait FactoryTrait |
||
9 | { |
||
10 | /** |
||
11 | * @var mixed[] |
||
12 | */ |
||
13 | protected $params; |
||
14 | |||
15 | /** |
||
16 | * @var callable[] |
||
17 | */ |
||
18 | protected $definitions; |
||
19 | |||
20 | /** |
||
21 | * |
||
22 | */ |
||
23 | 56 | public function __construct() |
|
28 | |||
29 | /** |
||
30 | * |
||
31 | */ |
||
32 | 56 | public function __destruct() |
|
37 | |||
38 | /** |
||
39 | * @see FactoryInterface::bindParam |
||
40 | */ |
||
41 | 14 | public function bindParam($name, $value) |
|
47 | |||
48 | /** |
||
49 | * @see FactoryInterface::unbindParam |
||
50 | */ |
||
51 | 6 | public function unbindParam($name) |
|
57 | |||
58 | /** |
||
59 | * @see FactoryInterface::getParam |
||
60 | */ |
||
61 | 6 | View Code Duplication | public function getParam($name) |
70 | |||
71 | /** |
||
72 | * @see FactoryInterface::hasParam |
||
73 | */ |
||
74 | 10 | public function hasParam($param) |
|
78 | |||
79 | /** |
||
80 | * @see FactoryInterface::getParams |
||
81 | */ |
||
82 | 4 | public function getParams() |
|
86 | |||
87 | /** |
||
88 | * @see FactoryInterface::define |
||
89 | */ |
||
90 | 14 | public function define($name, callable $factoryMethod) |
|
96 | |||
97 | /** |
||
98 | * @see FactoryInterface::remove |
||
99 | */ |
||
100 | 6 | public function remove($name) |
|
106 | |||
107 | /** |
||
108 | * @see FactoryInterface::getDefinition |
||
109 | */ |
||
110 | 4 | public function getDefinition($name) |
|
119 | |||
120 | /** |
||
121 | * @see FactoryInterface::hasDefinition |
||
122 | */ |
||
123 | 10 | public function hasDefinition($name) |
|
127 | |||
128 | /** |
||
129 | * @see FactoryInterface::getDefinitions |
||
130 | */ |
||
131 | 4 | public function getDefinitions() |
|
135 | |||
136 | /** |
||
137 | * @see FactoryInterface::create |
||
138 | */ |
||
139 | 4 | public function create($name, $args = []) |
|
148 | |||
149 | /** |
||
150 | * @param mixed $value |
||
151 | * @return mixed |
||
152 | */ |
||
153 | 4 | private function invoke($value) |
|
157 | } |
||
158 |
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.