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 Publication 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 Publication, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class Publication extends Model |
||
12 | { |
||
13 | |||
14 | public function getSource() |
||
18 | |||
19 | public function initialize() |
||
27 | |||
28 | private $id; |
||
29 | private $type_id; |
||
30 | private $slug; |
||
31 | private $created_at; |
||
32 | private $updated_at; |
||
33 | private $date; |
||
34 | private $preview_src; |
||
35 | private $preview_inner; |
||
36 | |||
37 | protected $title; |
||
38 | protected $text; |
||
39 | protected $meta_title; |
||
40 | protected $meta_description; |
||
41 | protected $meta_keywords; |
||
42 | |||
43 | protected $translateModel = 'Publication\Model\Translate\PublicationTranslate'; // translate |
||
44 | protected $translateFields = [ |
||
45 | 'title', |
||
46 | 'meta_title', |
||
47 | 'meta_description', |
||
48 | 'meta_keywords', |
||
49 | 'text' |
||
50 | ]; |
||
51 | |||
52 | public function beforeCreate() |
||
56 | |||
57 | public function beforeUpdate() |
||
61 | |||
62 | public function afterUpdate() |
||
76 | |||
77 | View Code Duplication | public function validation() |
|
88 | |||
89 | public function afterValidation() |
||
95 | |||
96 | public function updateFields($data) |
||
106 | |||
107 | View Code Duplication | public static function findCachedBySlug($slug) |
|
116 | |||
117 | public static function cacheSlugKey($slug) |
||
122 | |||
123 | public function setCreatedAt($created_at) |
||
128 | |||
129 | public function getCreatedAt() |
||
133 | |||
134 | public function setId($id) |
||
139 | |||
140 | public function getId() |
||
144 | |||
145 | public function setMetaDescription($meta_description) |
||
150 | |||
151 | public function getMetaDescription() |
||
155 | |||
156 | public function setMetaKeywords($meta_keywords) |
||
161 | |||
162 | public function getMetaKeywords() |
||
166 | |||
167 | public function setMetaTitle($meta_title) |
||
172 | |||
173 | public function getMetaTitle() |
||
177 | |||
178 | public function setSlug($slug) |
||
183 | |||
184 | public function getSlug() |
||
188 | |||
189 | public function setText($text) |
||
194 | |||
195 | public function getText() |
||
199 | |||
200 | public function setTitle($title) |
||
205 | |||
206 | public function getTitle() |
||
210 | |||
211 | public function setUpdatedAt($updated_at) |
||
216 | |||
217 | public function getUpdatedAt() |
||
221 | |||
222 | public function setDate($date) |
||
227 | |||
228 | public function getDate($format = 'Y-m-d H:i:s') |
||
238 | |||
239 | public function setTypeId($type_id) |
||
244 | |||
245 | public function getTypeId() |
||
249 | |||
250 | View Code Duplication | public function getTypeTitle() |
|
259 | |||
260 | View Code Duplication | public function getTypeSlug() |
|
269 | |||
270 | View Code Duplication | public function getTypeDisplayDate() |
|
279 | |||
280 | public function setPreviewInner($preview_inner) |
||
285 | |||
286 | public function getPreviewInner() |
||
290 | |||
291 | public function getPreviewSrc() |
||
295 | |||
296 | public function setPreviewSrc($preview_src) |
||
301 | |||
302 | } |
||
303 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.