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 |
||
25 | class ContainerAccessors extends ContainerMagic { |
||
26 | /** |
||
27 | * Array of accessors - getters/setters/etc |
||
28 | * |
||
29 | * @var callable[][] |
||
30 | */ |
||
31 | protected $accessors = array(); |
||
32 | |||
33 | /** |
||
34 | * @param array $array |
||
35 | */ |
||
36 | 1 | public function setAccessors($array) { |
|
39 | |||
40 | /** |
||
41 | * Direct access to parent class setter |
||
42 | * |
||
43 | * @param string $name |
||
44 | * @param mixed $value |
||
45 | * |
||
46 | * @return mixed |
||
47 | */ |
||
48 | 1 | public function setDirect($name, $value) { |
|
51 | |||
52 | /** |
||
53 | * Direct access to parent class getter |
||
54 | * |
||
55 | * @param string $name |
||
56 | * |
||
57 | * @return mixed |
||
58 | */ |
||
59 | 1 | public function getDirect($name) { |
|
62 | |||
63 | /** |
||
64 | * Direct access to parent class unsetter |
||
65 | * |
||
66 | * @param string $name |
||
67 | */ |
||
68 | 1 | public function unsetDirect($name) { |
|
71 | |||
72 | /** |
||
73 | * Assign accessor to a named variable |
||
74 | * |
||
75 | * Different accessors have different signatures - you should look carefully before assigning accessor |
||
76 | * |
||
77 | * @param string $varName |
||
78 | * @param string $type - getter/setter/importer/exporter/etc |
||
79 | * @param callable $callable |
||
80 | * |
||
81 | * @throws Exception |
||
82 | */ |
||
83 | 1 | View Code Duplication | public function assignAccessor($varName, $type, $callable) { |
94 | |||
95 | /** |
||
96 | * Performs $processor operation on property with specified name |
||
97 | * |
||
98 | * @param string $processor |
||
99 | * @param string $name |
||
100 | * @param null|mixed $value |
||
101 | * |
||
102 | * @return mixed |
||
103 | */ |
||
104 | 1 | protected function performMagic($processor, $name, $value = null) { |
|
115 | |||
116 | 1 | public function __set($name, $value) { |
|
123 | |||
124 | 1 | public function __get($name) { |
|
127 | |||
128 | 1 | public function __unset($name) { |
|
131 | |||
132 | 1 | public function __isset($name) { |
|
138 | |||
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.