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 | class Cleaner |
||
9 | { |
||
10 | /** |
||
11 | * Properties allowed on the clean object. |
||
12 | * |
||
13 | * @var array |
||
14 | */ |
||
15 | protected $allowed = []; |
||
16 | |||
17 | /** |
||
18 | * Methods to run. Returned value will be stored as a snake case property |
||
19 | * on the clean object. |
||
20 | * |
||
21 | * @var array |
||
22 | */ |
||
23 | protected $methods = []; |
||
24 | |||
25 | /** |
||
26 | * Clean the dirty object. |
||
27 | * |
||
28 | * @param object $dirty |
||
29 | * |
||
30 | * @return this |
||
31 | */ |
||
32 | public function clean($dirty) |
||
40 | |||
41 | /** |
||
42 | * Call snake_cased property names with studlyCased methods. |
||
43 | * |
||
44 | * @param string $name [Name of method called] |
||
45 | * @param array $arguments |
||
46 | * |
||
47 | * @return mixed |
||
48 | */ |
||
49 | public function __call($name, $arguments) |
||
55 | |||
56 | /** |
||
57 | * Set allowed properties on cleaner object. |
||
58 | * |
||
59 | * @param object $dirty |
||
60 | */ |
||
61 | View Code Duplication | protected function setAllowed($dirty) |
|
71 | |||
72 | /** |
||
73 | * Set properties from methods on cleaner object. |
||
74 | * |
||
75 | * @param object $dirty |
||
76 | */ |
||
77 | View Code Duplication | protected function setMethods($dirty) |
|
87 | |||
88 | /** |
||
89 | * Property is a nested property. |
||
90 | * |
||
91 | * @param string $property |
||
92 | * |
||
93 | * @return bool |
||
94 | */ |
||
95 | protected function isNested($property) |
||
99 | |||
100 | /** |
||
101 | * Set the nested property on the cleaner object. |
||
102 | * |
||
103 | * @param string $propString [Property string] |
||
104 | * @param Model $dirty [Original dirty object] |
||
105 | * @param string $type [Type of nested property: 'property', 'method'] |
||
106 | * @param object $object [Object to set property on] |
||
107 | */ |
||
108 | protected function setNestedProperty($propString, Model $dirty, $type, $object = null) |
||
126 | |||
127 | /** |
||
128 | * Create relation on object. |
||
129 | * |
||
130 | * @param string $property |
||
131 | * @param object $object |
||
132 | * |
||
133 | * @return string |
||
134 | */ |
||
135 | protected function setRelation($property, $object) |
||
143 | |||
144 | /** |
||
145 | * Get property name from nested property. |
||
146 | * |
||
147 | * @param string $property |
||
148 | * |
||
149 | * @return string |
||
150 | */ |
||
151 | protected function getProperty($property) |
||
155 | |||
156 | /** |
||
157 | * If relation doesnt not exist on cleaner object, create it. |
||
158 | * |
||
159 | * @param string $relation |
||
160 | * @param object $object [Object to create relation on] |
||
161 | */ |
||
162 | protected function createRelation($relation, $object) |
||
170 | |||
171 | /** |
||
172 | * Set property value on clean object. |
||
173 | * |
||
174 | * @param Model $dirty [Original, dirty object] |
||
175 | * @param string $property [Property name] |
||
176 | * @param Cleaner $object [Clean object] |
||
177 | * @param string $type [Type of property: 'property', 'method'] |
||
178 | * @param string $relation [Relationship name] |
||
179 | */ |
||
180 | protected function setProperty( |
||
208 | |||
209 | /** |
||
210 | * Set property value on clean object. |
||
211 | * |
||
212 | * @param Model $dirty [Original, dirty object] |
||
213 | * @param string $property [Property name] |
||
214 | * @param Cleaner $object [Clean object] |
||
215 | * @param string $type [Type of property: 'property', 'method'] |
||
216 | * @param string $relation [Relationship name] |
||
217 | * @param string $key |
||
218 | */ |
||
219 | protected function setPropertiesOnRelations( |
||
241 | |||
242 | /** |
||
243 | * Get the value from the named property/method off object. |
||
244 | * |
||
245 | * @param Model $object [Object which contains property/method] |
||
246 | * @param string $name [Name of property/method] |
||
247 | * @param string $type ['method' or 'property'] |
||
248 | * |
||
249 | * @return mixed |
||
250 | */ |
||
251 | protected function getValue(Model $object, $name, $type) |
||
263 | } |
||
264 |
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.