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 |
||
9 | abstract class Model |
||
10 | { |
||
11 | protected $attributes = []; |
||
12 | protected $queryAttributes = []; |
||
13 | protected $relationships = []; |
||
14 | protected $queries = []; |
||
15 | protected $methods = []; |
||
16 | |||
17 | public $response; |
||
18 | |||
19 | const ENDPOINT = ''; |
||
20 | const NODE_NAME = ''; |
||
21 | const KEY_FIELD = ''; |
||
22 | |||
23 | protected $client; |
||
24 | |||
25 | public function __construct() |
||
29 | |||
30 | /** |
||
31 | * Get the resource uri of the class (Contacts) etc. |
||
32 | * |
||
33 | * @return string |
||
34 | */ |
||
35 | public static function getEndpoint() |
||
39 | |||
40 | /** |
||
41 | * Get the root node name. Just the unqualified classname. |
||
42 | * |
||
43 | * @return string |
||
44 | */ |
||
45 | public static function getRootNodeName() |
||
49 | |||
50 | /** |
||
51 | * Get the unique key field. |
||
52 | * |
||
53 | * @return string |
||
54 | */ |
||
55 | public static function getKey() |
||
59 | |||
60 | /** |
||
61 | * Get the response model. |
||
62 | * |
||
63 | * @return response object |
||
64 | */ |
||
65 | public function getResponse() |
||
69 | |||
70 | /** |
||
71 | * Get the object unique ID. |
||
72 | * |
||
73 | * @return string |
||
74 | */ |
||
75 | public function getID() |
||
81 | |||
82 | public function hasID() |
||
91 | |||
92 | public function getAttributes() |
||
96 | |||
97 | /** |
||
98 | * Get an attribute from the model. |
||
99 | * |
||
100 | * @param string $key |
||
101 | * @return mixed |
||
102 | */ |
||
103 | public function getAttribute($key) |
||
112 | |||
113 | /** |
||
114 | * Get a plain attribute (not a relationship). |
||
115 | * |
||
116 | * @param string $key |
||
117 | * @return mixed |
||
118 | */ |
||
119 | public function getAttributeValue($key) |
||
123 | |||
124 | /** |
||
125 | * Get an attribute from the $attributes array. |
||
126 | * |
||
127 | * @param string $key |
||
128 | * @return mixed |
||
129 | */ |
||
130 | protected function getAttributeFromArray($key) |
||
136 | |||
137 | /** |
||
138 | * Set a given attribute on the model. |
||
139 | * |
||
140 | * @param string $key |
||
141 | * @param mixed $value |
||
142 | * @return mixed |
||
143 | */ |
||
144 | public function setAttribute($key, $value) |
||
169 | |||
170 | public function processRelationships() |
||
188 | |||
189 | public function attributeExists($key) |
||
193 | |||
194 | public function isRelationshipAttribute($key) |
||
198 | |||
199 | public function unsetAttribute($key) |
||
203 | |||
204 | /** |
||
205 | * Dynamically retrieve attributes on the model. |
||
206 | * |
||
207 | * @param string $key |
||
208 | * @return mixed |
||
209 | */ |
||
210 | public function __get($key) |
||
214 | |||
215 | /** |
||
216 | * Dynamically set attributes on the model. |
||
217 | * |
||
218 | * @param string $key |
||
219 | * @param mixed $value |
||
220 | * @return void |
||
221 | */ |
||
222 | public function __set($key, $value) |
||
226 | |||
227 | /** |
||
228 | * Determine if an attribute or relation exists on the model. |
||
229 | * |
||
230 | * @param string $key |
||
231 | * @return bool |
||
232 | */ |
||
233 | public function __isset($key) |
||
237 | |||
238 | /** |
||
239 | * Unset an attribute on the model. |
||
240 | * |
||
241 | * @param string $key |
||
242 | * @return void |
||
243 | */ |
||
244 | public function __unset($key) |
||
248 | |||
249 | public function make($attributes) |
||
256 | |||
257 | public function create($attributes) |
||
264 | |||
265 | public function fill($attributes) |
||
273 | |||
274 | public function update($attributes) |
||
280 | |||
281 | public function save() |
||
305 | |||
306 | public function where($key, $operator, $value = '') |
||
318 | |||
319 | public function getQueryString() |
||
337 | |||
338 | public function first() |
||
342 | |||
343 | View Code Duplication | public function get() |
|
354 | |||
355 | View Code Duplication | public function all() |
|
366 | |||
367 | View Code Duplication | public function find($id) |
|
378 | |||
379 | public function delete($id = '') |
||
393 | |||
394 | protected function collect($response) |
||
403 | } |
||
404 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.