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 $query_attributes = []; |
||
| 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) |
||
| 166 | |||
| 167 | public function processRelationships() |
||
| 185 | |||
| 186 | public function attributeExists($key) |
||
| 190 | |||
| 191 | public function isRelationshipAttribute($key) |
||
| 195 | |||
| 196 | public function unsetAttribute($key) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Dynamically retrieve attributes on the model. |
||
| 203 | * |
||
| 204 | * @param string $key |
||
| 205 | * @return mixed |
||
| 206 | */ |
||
| 207 | public function __get($key) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Dynamically set attributes on the model. |
||
| 214 | * |
||
| 215 | * @param string $key |
||
| 216 | * @param mixed $value |
||
| 217 | * @return void |
||
| 218 | */ |
||
| 219 | public function __set($key, $value) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Determine if an attribute or relation exists on the model. |
||
| 226 | * |
||
| 227 | * @param string $key |
||
| 228 | * @return bool |
||
| 229 | */ |
||
| 230 | public function __isset($key) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Unset an attribute on the model. |
||
| 237 | * |
||
| 238 | * @param string $key |
||
| 239 | * @return void |
||
| 240 | */ |
||
| 241 | public function __unset($key) |
||
| 245 | |||
| 246 | public function make($attributes) |
||
| 253 | |||
| 254 | public function create($attributes) |
||
| 261 | |||
| 262 | public function fill($attributes) |
||
| 270 | |||
| 271 | public function update($attributes) |
||
| 277 | |||
| 278 | public function save() |
||
| 302 | |||
| 303 | public function where($key, $operator, $value = '') |
||
| 315 | |||
| 316 | public function getQueryString() |
||
| 333 | |||
| 334 | public function first() |
||
| 338 | |||
| 339 | View Code Duplication | public function get() |
|
| 350 | |||
| 351 | View Code Duplication | public function all() |
|
| 362 | |||
| 363 | View Code Duplication | public function find($id) |
|
| 374 | |||
| 375 | public function delete($id = '') |
||
| 389 | |||
| 390 | protected function collect($response) |
||
| 399 | } |
||
| 400 |
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.