Complex classes like Language 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 Language, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class Language extends Model |
||
17 | { |
||
18 | |||
19 | public function getSource() |
||
23 | |||
24 | public $id; |
||
25 | public $iso; |
||
26 | public $locale; |
||
27 | public $name; |
||
28 | public $short_name; |
||
29 | public $url; |
||
30 | public $sortorder; |
||
31 | public $primary; |
||
32 | |||
33 | public function validation() |
||
34 | { |
||
35 | $validator = new Validation(); |
||
36 | |||
37 | /** |
||
38 | * ISO |
||
39 | */ |
||
40 | $validator->add('iso', new Uniqueness([ |
||
41 | 'model' => $this, |
||
42 | "message" => "The inputted ISO language is existing" |
||
43 | ])); |
||
44 | $validator->add('iso', new PresenceOf([ |
||
45 | 'model' => $this, |
||
46 | 'message' => 'ISO is required' |
||
47 | ])); |
||
48 | |||
49 | /** |
||
50 | * Name |
||
51 | */ |
||
52 | $validator->add('name', new Uniqueness([ |
||
53 | 'model' => $this, |
||
54 | "message" => "The inputted name is existing" |
||
55 | ])); |
||
56 | $validator->add('name', new PresenceOf([ |
||
57 | 'model' => $this, |
||
58 | 'message' => 'Name is required' |
||
59 | ])); |
||
60 | |||
61 | /** |
||
62 | * URL |
||
63 | */ |
||
64 | $validator->add('url', new Uniqueness([ |
||
65 | 'model' => $this, |
||
66 | "message" => "The inputted URL is existing" |
||
67 | ] |
||
68 | )); |
||
69 | |||
70 | if ($this->primary == 0) { |
||
71 | $validator->add('url', new PresenceOf([ |
||
72 | 'model' => $this, |
||
73 | 'message' => 'URL is required' |
||
74 | ])); |
||
75 | } |
||
76 | |||
77 | return $this->validationHasFailed() != true; |
||
78 | } |
||
79 | |||
80 | public function afterCreate() |
||
85 | |||
86 | public function afterUpdate() |
||
91 | |||
92 | public function afterSave() |
||
97 | |||
98 | public function afterDelete() |
||
103 | |||
104 | private function buildCmsLanguagesCache() |
||
128 | |||
129 | public function afterValidation() |
||
136 | |||
137 | public function afterValidationOnCreate() |
||
142 | |||
143 | public static function findCachedLanguages() |
||
147 | |||
148 | public static function findCachedLanguagesIso() |
||
159 | |||
160 | public static function findCachedByIso($iso) |
||
169 | |||
170 | public static function cacheKey() |
||
174 | |||
175 | public function getUpperSortorder() |
||
181 | |||
182 | public function setOnlyOnePrimary() |
||
201 | |||
202 | /** |
||
203 | * @param mixed $id |
||
204 | */ |
||
205 | public function setId($id) |
||
209 | |||
210 | /** |
||
211 | * @return mixed |
||
212 | */ |
||
213 | public function getId() |
||
217 | |||
218 | /** |
||
219 | * @param mixed $iso |
||
220 | */ |
||
221 | public function setIso($iso) |
||
225 | |||
226 | /** |
||
227 | * @return mixed |
||
228 | */ |
||
229 | public function getIso() |
||
233 | |||
234 | /** |
||
235 | * @param mixed $name |
||
236 | */ |
||
237 | public function setName($name) |
||
241 | |||
242 | /** |
||
243 | * @return mixed |
||
244 | */ |
||
245 | public function getName() |
||
249 | |||
250 | /** |
||
251 | * @param mixed $sortorder |
||
252 | */ |
||
253 | public function setSortorder($sortorder) |
||
257 | |||
258 | /** |
||
259 | * @return mixed |
||
260 | */ |
||
261 | public function getSortorder() |
||
265 | |||
266 | /** |
||
267 | * @param mixed $url |
||
268 | */ |
||
269 | public function setUrl($url) |
||
273 | |||
274 | /** |
||
275 | * @return mixed |
||
276 | */ |
||
277 | public function getUrl() |
||
281 | |||
282 | /** |
||
283 | * @param mixed $primary |
||
284 | */ |
||
285 | public function setPrimary($primary) |
||
289 | |||
290 | /** |
||
291 | * @return mixed |
||
292 | */ |
||
293 | public function getPrimary() |
||
297 | |||
298 | /** |
||
299 | * @param mixed $short_name |
||
300 | */ |
||
301 | public function setShort_name($short_name) |
||
305 | |||
306 | /** |
||
307 | * @return mixed |
||
308 | */ |
||
309 | public function getShort_name() |
||
313 | |||
314 | /** |
||
315 | * @param mixed $locale |
||
316 | */ |
||
317 | public function setLocale($locale) |
||
321 | |||
322 | /** |
||
323 | * @return mixed |
||
324 | */ |
||
325 | public function getLocale() |
||
329 | |||
330 | } |
||
331 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.