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 SimpleFactoryTrait |
||
9 | { |
||
10 | /** |
||
11 | * @var mixed[] |
||
12 | */ |
||
13 | protected $params; |
||
14 | |||
15 | /** |
||
16 | * @var callable|null |
||
17 | */ |
||
18 | protected $definition; |
||
19 | |||
20 | /** |
||
21 | * |
||
22 | */ |
||
23 | 46 | public function __construct() |
|
28 | |||
29 | /** |
||
30 | * |
||
31 | */ |
||
32 | 46 | public function __destruct() |
|
37 | |||
38 | /** |
||
39 | * @see SimpleFactoryInterface::bindParam |
||
40 | */ |
||
41 | 14 | public function bindParam($name, $value) |
|
47 | |||
48 | /** |
||
49 | * @see SimpleFactoryInterface::unbindParam |
||
50 | */ |
||
51 | 6 | public function unbindParam($name) |
|
57 | |||
58 | /** |
||
59 | * @see SimpleFactoryInterface::getParam |
||
60 | */ |
||
61 | 6 | View Code Duplication | public function getParam($name) |
70 | |||
71 | /** |
||
72 | * @see SimpleFactoryInterface::hasParam |
||
73 | */ |
||
74 | 10 | public function hasParam($name) |
|
78 | |||
79 | /** |
||
80 | * @see SimpleFactoryInterface::getParams |
||
81 | */ |
||
82 | 4 | public function getParams() |
|
86 | |||
87 | /** |
||
88 | * @see SimpleFactoryInterface::define |
||
89 | */ |
||
90 | 10 | public function define(callable $factoryMethod) |
|
96 | |||
97 | /** |
||
98 | * @see SimpleFactoryInterface::getDefinition |
||
99 | */ |
||
100 | 4 | public function getDefinition() |
|
109 | |||
110 | /** |
||
111 | * @see SimpleFactoryInterface::hasDefinition |
||
112 | */ |
||
113 | 6 | public function hasDefinition() |
|
117 | |||
118 | /** |
||
119 | * @see SimpleFactoryInterface::create |
||
120 | */ |
||
121 | 4 | public function create($args = []) |
|
130 | |||
131 | /** |
||
132 | * @param mixed $value |
||
133 | * @return mixed |
||
134 | */ |
||
135 | 4 | private function invoke($value) |
|
139 | } |
||
140 |
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.