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:
Complex classes like Model often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Model, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | abstract class Model |
||
20 | { |
||
21 | protected $mappingClasses = []; |
||
22 | |||
23 | /** |
||
24 | * Contains property name mappings. |
||
25 | * |
||
26 | * [ |
||
27 | * 'data_array_property1' => 'objectProperty1', |
||
28 | * 'data_array_property2' => 'objectProperty2', |
||
29 | * ] |
||
30 | * |
||
31 | * Data array property uses as keys |
||
32 | * because there is can be more then one rule per object property |
||
33 | * |
||
34 | * f.g. $data['nmodels'] and ['modelsnum'] should map in modelsCount property. |
||
35 | * Otherwise not unique array keys cause remapping of properties. |
||
36 | * |
||
37 | * @var array |
||
38 | */ |
||
39 | protected $propNameMap = []; |
||
40 | |||
41 | /** |
||
42 | * Constructor |
||
43 | * |
||
44 | * @param array $data |
||
45 | 152 | */ |
|
46 | public function __construct($data) |
||
50 | |||
51 | /** |
||
52 | * Set from XML |
||
53 | * |
||
54 | * @param \SimpleXMLIterator $data |
||
55 | * @return $this |
||
56 | 150 | */ |
|
57 | public function fromXml(\SimpleXMLIterator $data) |
||
120 | |||
121 | /** |
||
122 | * Set from array |
||
123 | * |
||
124 | * @param array $data |
||
125 | * @return $this |
||
126 | */ |
||
127 | 40 | public function fromArray($data) |
|
160 | |||
161 | 18 | /** |
|
162 | * Set from json |
||
163 | * |
||
164 | * @param string $json |
||
165 | * @return $this |
||
166 | */ |
||
167 | public function fromJson($json) |
||
173 | |||
174 | /** |
||
175 | * Get array from object |
||
176 | * |
||
177 | * @return array |
||
178 | */ |
||
179 | public function toArray() |
||
183 | |||
184 | /** |
||
185 | * Get array from object |
||
186 | * |
||
187 | * @return string |
||
188 | */ |
||
189 | public function toJson() |
||
193 | |||
194 | /** |
||
195 | * Get array from object |
||
196 | * |
||
197 | * @param array|object $data |
||
198 | * @return array |
||
199 | */ |
||
200 | protected function toArrayRecursive($data) |
||
238 | } |
||
239 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: