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 |
||
8 | abstract class Model |
||
9 | { |
||
10 | protected $attributes = []; |
||
11 | protected $query_attributes = []; |
||
12 | protected $relationships = []; |
||
13 | protected $queries = []; |
||
14 | protected $methods = []; |
||
15 | |||
16 | public $response; |
||
17 | |||
18 | const ENDPOINT = ''; |
||
19 | const NODE_NAME = ''; |
||
20 | const KEY_FIELD = ''; |
||
21 | |||
22 | protected $client; |
||
23 | |||
24 | public function __construct() |
||
28 | |||
29 | /** |
||
30 | * Get the resource uri of the class (Contacts) etc. |
||
31 | * |
||
32 | * @return string |
||
33 | */ |
||
34 | public static function getEndpoint() |
||
38 | |||
39 | /** |
||
40 | * Get the root node name. Just the unqualified classname. |
||
41 | * |
||
42 | * @return string |
||
43 | */ |
||
44 | public static function getRootNodeName() |
||
48 | |||
49 | /** |
||
50 | * Get the unique key field. |
||
51 | * |
||
52 | * @return string |
||
53 | */ |
||
54 | public static function getKey() |
||
58 | |||
59 | /** |
||
60 | * Get the object unique ID. |
||
61 | * |
||
62 | * @return string |
||
63 | */ |
||
64 | public function getID() |
||
70 | |||
71 | public function hasID() |
||
80 | |||
81 | public function getAttributes() |
||
85 | |||
86 | /** |
||
87 | * Get an attribute from the model. |
||
88 | * |
||
89 | * @param string $key |
||
90 | * @return mixed |
||
91 | */ |
||
92 | public function getAttribute($key) |
||
101 | |||
102 | /** |
||
103 | * Get a plain attribute (not a relationship). |
||
104 | * |
||
105 | * @param string $key |
||
106 | * @return mixed |
||
107 | */ |
||
108 | public function getAttributeValue($key) |
||
112 | |||
113 | /** |
||
114 | * Get an attribute from the $attributes array. |
||
115 | * |
||
116 | * @param string $key |
||
117 | * @return mixed |
||
118 | */ |
||
119 | protected function getAttributeFromArray($key) |
||
125 | |||
126 | /** |
||
127 | * Set a given attribute on the model. |
||
128 | * |
||
129 | * @param string $key |
||
130 | * @param mixed $value |
||
131 | * @return mixed |
||
132 | */ |
||
133 | public function setAttribute($key, $value) |
||
141 | |||
142 | public function processRelationships() |
||
160 | |||
161 | public function attributeExists($key) |
||
165 | |||
166 | public function unsetAttribute($key) |
||
170 | |||
171 | /** |
||
172 | * Dynamically retrieve attributes on the model. |
||
173 | * |
||
174 | * @param string $key |
||
175 | * @return mixed |
||
176 | */ |
||
177 | public function __get($key) |
||
181 | |||
182 | /** |
||
183 | * Dynamically set attributes on the model. |
||
184 | * |
||
185 | * @param string $key |
||
186 | * @param mixed $value |
||
187 | * @return void |
||
188 | */ |
||
189 | public function __set($key, $value) |
||
193 | |||
194 | /** |
||
195 | * Determine if an attribute or relation exists on the model. |
||
196 | * |
||
197 | * @param string $key |
||
198 | * @return bool |
||
199 | */ |
||
200 | public function __isset($key) |
||
204 | |||
205 | /** |
||
206 | * Unset an attribute on the model. |
||
207 | * |
||
208 | * @param string $key |
||
209 | * @return void |
||
210 | */ |
||
211 | public function __unset($key) |
||
215 | |||
216 | public static function make($attributes) |
||
225 | |||
226 | public static function create($attributes) |
||
233 | |||
234 | public function fill($attributes) |
||
242 | |||
243 | public function update($attributes) |
||
249 | |||
250 | public function save() |
||
276 | |||
277 | public function where($key, $operator, $value="") |
||
289 | |||
290 | public function getQueryString() |
||
307 | |||
308 | public function first() |
||
312 | |||
313 | View Code Duplication | public function get() |
|
324 | |||
325 | View Code Duplication | public function all() |
|
336 | |||
337 | View Code Duplication | public function find($id) |
|
348 | |||
349 | public function delete($id = "") |
||
363 | |||
364 | protected function collect($response) |
||
373 | } |
||
374 |
This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.
The variable may have been renamed without also renaming all references.