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 |
||
6 | class V2PropertyContainer implements IMagicProperties, IPropertyContainer { |
||
7 | |||
8 | /** |
||
9 | * Property descriptions |
||
10 | * |
||
11 | * @var array[] |
||
12 | */ |
||
13 | protected $properties = array(); |
||
14 | |||
15 | /** |
||
16 | * Property values |
||
17 | * |
||
18 | * @var array |
||
19 | */ |
||
20 | protected $values = array(); |
||
21 | /** |
||
22 | * Array of getters |
||
23 | * |
||
24 | * Getter is a callable like |
||
25 | * function ($value) use ($that) {} |
||
26 | * |
||
27 | * @var callable[] |
||
28 | */ |
||
29 | protected $setters = array(); |
||
30 | /** |
||
31 | * Array of setters |
||
32 | * |
||
33 | * Setter is a callable like |
||
34 | * function () use ($that) {} |
||
35 | * |
||
36 | * @var callable[] |
||
37 | */ |
||
38 | protected $getters = array(); |
||
39 | /** |
||
40 | * Array of importers |
||
41 | * |
||
42 | * Importer is a callable like |
||
43 | * function (&$row) use ($this) {} |
||
44 | * |
||
45 | * @var callable[] |
||
46 | */ |
||
47 | protected $importers; |
||
48 | /** |
||
49 | * Array of exporters |
||
50 | * |
||
51 | * Exporter is a callable like |
||
52 | * function (&$row) use ($this) {} |
||
53 | * |
||
54 | * @var callable[] |
||
55 | */ |
||
56 | protected $exporters = array(); |
||
57 | |||
58 | /** |
||
59 | * Magic checker for property set |
||
60 | * |
||
61 | * @param string $name |
||
62 | * |
||
63 | * @return boolean |
||
64 | */ |
||
65 | public function __isset($name) { |
||
68 | |||
69 | /** |
||
70 | * Magic un-setter |
||
71 | * |
||
72 | * @param string $name |
||
73 | */ |
||
74 | public function __unset($name) { |
||
77 | |||
78 | public function __set($name, $value) { |
||
85 | |||
86 | public function __get($name) { |
||
93 | |||
94 | View Code Duplication | public function importRow($row) { |
|
104 | |||
105 | View Code Duplication | public function exportRow() { |
|
119 | |||
120 | public function setProperties($properties) { |
||
123 | |||
124 | public function assignAccessor($varName, $type, $callable) { |
||
135 | |||
136 | |||
137 | /** |
||
138 | * Is container contains no data |
||
139 | * |
||
140 | * @return bool |
||
141 | */ |
||
142 | public function isEmpty() { |
||
145 | |||
146 | } |
||
147 |
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.