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 Type 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 Type, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class Type extends Model |
||
| 16 | { |
||
| 17 | |||
| 18 | public function getSource() |
||
| 22 | |||
| 23 | protected $translateModel = 'Publication\Model\Translate\TypeTranslate'; |
||
| 24 | |||
| 25 | public $id; |
||
| 26 | public $title; // translate |
||
| 27 | public $slug; |
||
| 28 | public $limit = 10; |
||
| 29 | public $display_date; |
||
| 30 | public $format = 'list'; |
||
| 31 | public $head_title; // translate |
||
| 32 | public $meta_description; // translate |
||
| 33 | public $meta_keywords; // translate |
||
| 34 | public $seo_text; // translate |
||
| 35 | |||
| 36 | public static $formats = [ |
||
| 37 | 'list' => 'List', |
||
| 38 | 'grid' => 'Grid', |
||
| 39 | ]; |
||
| 40 | |||
| 41 | public function initialize() |
||
| 49 | |||
| 50 | View Code Duplication | public function validation() |
|
| 61 | |||
| 62 | public function afterUpdate() |
||
| 69 | |||
| 70 | public function afterSave() |
||
| 74 | |||
| 75 | public function afterDelete() |
||
| 79 | |||
| 80 | private function buildCmsTypesCache() |
||
| 92 | |||
| 93 | public function updateFields($data) |
||
| 110 | |||
| 111 | public static function types() |
||
| 115 | |||
| 116 | public static function cachedListArray($params = []) |
||
| 141 | |||
| 142 | View Code Duplication | public static function getCachedBySlug($slug) |
|
| 157 | |||
| 158 | public static function cacheSlugKey($slug) |
||
| 162 | |||
| 163 | public static function cacheListKey($params) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @param mixed $title |
||
| 170 | */ |
||
| 171 | public function setTitle($title) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @return mixed |
||
| 179 | */ |
||
| 180 | public function getTitle() |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @param string $format |
||
| 187 | */ |
||
| 188 | public function setFormat($format) |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @return string |
||
| 196 | */ |
||
| 197 | public function getFormat() |
||
| 201 | |||
| 202 | public function getFormatTitle() |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @param mixed $head_title |
||
| 211 | */ |
||
| 212 | public function setHeadTitle($head_title) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @return mixed |
||
| 220 | */ |
||
| 221 | public function getHeadTitle() |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @param mixed $id |
||
| 228 | */ |
||
| 229 | public function setId($id) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @return mixed |
||
| 237 | */ |
||
| 238 | public function getId() |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @param mixed $limit |
||
| 245 | */ |
||
| 246 | public function setLimit($limit) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @return mixed |
||
| 254 | */ |
||
| 255 | public function getLimit() |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @param mixed $meta_description |
||
| 262 | */ |
||
| 263 | public function setMetaDescription($meta_description) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @return mixed |
||
| 271 | */ |
||
| 272 | public function getMetaDescription() |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @param mixed $meta_keywords |
||
| 279 | */ |
||
| 280 | public function setMetaKeywords($meta_keywords) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @return mixed |
||
| 288 | */ |
||
| 289 | public function getMetaKeywords() |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @param mixed $seo_text |
||
| 296 | */ |
||
| 297 | public function setSeoText($seo_text) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @return mixed |
||
| 305 | */ |
||
| 306 | public function getSeoText() |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @param mixed $slug |
||
| 313 | */ |
||
| 314 | public function setSlug($slug) |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @return mixed |
||
| 322 | */ |
||
| 323 | public function getSlug() |
||
| 327 | |||
| 328 | /** |
||
| 329 | * @param mixed $display_date |
||
| 330 | */ |
||
| 331 | public function setDisplayDate($display_date) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @return mixed |
||
| 339 | */ |
||
| 340 | public function getDisplayDate() |
||
| 344 | |||
| 345 | |||
| 346 | } |
||
| 347 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.