Complex classes like Album 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 Album, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class Album implements AlbumInterface |
||
10 | { |
||
11 | /** |
||
12 | * @var string |
||
13 | */ |
||
14 | protected $albumId; |
||
15 | |||
16 | /** |
||
17 | * @var string |
||
18 | */ |
||
19 | protected $title; |
||
20 | |||
21 | /** |
||
22 | * @var string |
||
23 | */ |
||
24 | protected $description; |
||
25 | |||
26 | /** |
||
27 | * @var \DateTime |
||
28 | */ |
||
29 | protected $insertedIntoGallery; |
||
30 | |||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $cover; |
||
35 | |||
36 | /** |
||
37 | * @var int |
||
38 | */ |
||
39 | protected $coverWidth; |
||
40 | |||
41 | /** |
||
42 | * @var int |
||
43 | */ |
||
44 | protected $coverHeight; |
||
45 | |||
46 | /** |
||
47 | * @var string |
||
48 | */ |
||
49 | protected $accountUsername; |
||
50 | |||
51 | /** |
||
52 | * @var string |
||
53 | */ |
||
54 | protected $accountId; |
||
55 | |||
56 | /** |
||
57 | * @var string |
||
58 | */ |
||
59 | protected $privacy; |
||
60 | |||
61 | /** |
||
62 | * @var string |
||
63 | */ |
||
64 | protected $layout; |
||
65 | |||
66 | /** |
||
67 | * @var int |
||
68 | */ |
||
69 | protected $views; |
||
70 | |||
71 | /** |
||
72 | * @var string |
||
73 | */ |
||
74 | protected $link; |
||
75 | |||
76 | /** |
||
77 | * @var bool |
||
78 | */ |
||
79 | protected $favorite; |
||
80 | |||
81 | /** |
||
82 | * @var bool |
||
83 | */ |
||
84 | protected $nsfw; |
||
85 | |||
86 | /** |
||
87 | * @var string |
||
88 | */ |
||
89 | protected $section; |
||
90 | |||
91 | /** |
||
92 | * @var int |
||
93 | */ |
||
94 | protected $order; |
||
95 | |||
96 | /** |
||
97 | * @var string |
||
98 | */ |
||
99 | protected $deleteHash; |
||
100 | |||
101 | /** |
||
102 | * @var int |
||
103 | */ |
||
104 | protected $imagesCount; |
||
105 | |||
106 | /** |
||
107 | * @var ImageInterface[] |
||
108 | */ |
||
109 | protected $images; |
||
110 | |||
111 | /** |
||
112 | * @param string $id |
||
113 | * @return AlbumInterface |
||
114 | */ |
||
115 | 1 | public function setAlbumId($id) |
|
121 | |||
122 | /** |
||
123 | * @return string |
||
124 | */ |
||
125 | 1 | public function getAlbumId() |
|
129 | |||
130 | /** |
||
131 | * @param string $title |
||
132 | * @return AlbumInterface |
||
133 | */ |
||
134 | 1 | public function setTitle($title) |
|
140 | |||
141 | /** |
||
142 | * @return string |
||
143 | */ |
||
144 | 1 | public function getTitle() |
|
148 | |||
149 | /** |
||
150 | * @param string $description |
||
151 | * @return AlbumInterface |
||
152 | */ |
||
153 | 1 | public function setDescription($description) |
|
159 | |||
160 | /** |
||
161 | * @return string |
||
162 | */ |
||
163 | 1 | public function getDescription() |
|
167 | |||
168 | /** |
||
169 | * @param \DateTime $dateTime |
||
170 | * @return AlbumInterface |
||
171 | */ |
||
172 | 1 | public function setInsertedIntoGallery($dateTime) |
|
178 | |||
179 | /** |
||
180 | * @return \DateTime |
||
181 | */ |
||
182 | 1 | public function getInsertedIntoGallery() |
|
186 | |||
187 | /** |
||
188 | * @param string $cover |
||
189 | * @return AlbumInterface |
||
190 | */ |
||
191 | 1 | public function setCover($cover) |
|
197 | |||
198 | /** |
||
199 | * @return string |
||
200 | */ |
||
201 | 1 | public function getCover() |
|
205 | |||
206 | /** |
||
207 | * @param int $coverWidth |
||
208 | * @return AlbumInterface |
||
209 | */ |
||
210 | 1 | public function setCoverWidth($coverWidth) |
|
216 | |||
217 | /** |
||
218 | * @return int |
||
219 | */ |
||
220 | 1 | public function getCoverWidth() |
|
224 | |||
225 | /** |
||
226 | * @param int $coverHeight |
||
227 | * @return AlbumInterface |
||
228 | */ |
||
229 | 1 | public function setCoverHeight($coverHeight) |
|
235 | |||
236 | /** |
||
237 | * @return int |
||
238 | */ |
||
239 | 1 | public function getCoverHeight() |
|
243 | |||
244 | /** |
||
245 | * @param string $accountUsername |
||
246 | * @return AlbumInterface |
||
247 | */ |
||
248 | 1 | public function setAccountUsername($accountUsername) |
|
254 | |||
255 | /** |
||
256 | * @return string |
||
257 | */ |
||
258 | 1 | public function getAccountUsername() |
|
262 | |||
263 | /** |
||
264 | * @param integer $accountId |
||
265 | * @return AlbumInterface |
||
266 | */ |
||
267 | 1 | public function setAccountId($accountId) |
|
273 | |||
274 | /** |
||
275 | * @return string |
||
276 | */ |
||
277 | 1 | public function getAccountId() |
|
281 | |||
282 | /** |
||
283 | * @param string $privacy |
||
284 | * @return AlbumInterface |
||
285 | */ |
||
286 | 1 | public function setPrivacy($privacy) |
|
292 | |||
293 | /** |
||
294 | * @return string |
||
295 | */ |
||
296 | 1 | public function getPrivacy() |
|
300 | |||
301 | /** |
||
302 | * @param string $layout |
||
303 | * @return AlbumInterface |
||
304 | */ |
||
305 | 1 | public function setLayout($layout) |
|
311 | |||
312 | /** |
||
313 | * @return string |
||
314 | */ |
||
315 | 1 | public function getLayout() |
|
319 | |||
320 | /** |
||
321 | * @param int $views |
||
322 | * @return AlbumInterface |
||
323 | */ |
||
324 | 1 | public function setViews($views) |
|
330 | |||
331 | /** |
||
332 | * @return int |
||
333 | */ |
||
334 | 1 | public function getViews() |
|
338 | |||
339 | /** |
||
340 | * @param string $link |
||
341 | * @return AlbumInterface |
||
342 | */ |
||
343 | 1 | public function setLink($link) |
|
349 | |||
350 | /** |
||
351 | * @return string |
||
352 | */ |
||
353 | 1 | public function getLink() |
|
357 | |||
358 | /** |
||
359 | * @param bool $favorite |
||
360 | * @return AlbumInterface |
||
361 | */ |
||
362 | 1 | public function setFavorite($favorite) |
|
368 | |||
369 | /** |
||
370 | * @return bool |
||
371 | */ |
||
372 | 1 | public function isFavorite() |
|
376 | |||
377 | /** |
||
378 | * @param bool $nsfw |
||
379 | * @return AlbumInterface |
||
380 | */ |
||
381 | 1 | public function setNsfw($nsfw) |
|
387 | |||
388 | /** |
||
389 | * @return bool |
||
390 | */ |
||
391 | 1 | public function isNsfw() |
|
395 | |||
396 | /** |
||
397 | * @param string $section |
||
398 | * @return AlbumInterface |
||
399 | */ |
||
400 | 1 | public function setSection($section) |
|
406 | |||
407 | /** |
||
408 | * @return mixed |
||
409 | */ |
||
410 | 1 | public function getSection() |
|
414 | |||
415 | /** |
||
416 | * @param int $order |
||
417 | * @return AlbumInterface |
||
418 | */ |
||
419 | 1 | public function setOrder($order) |
|
425 | |||
426 | /** |
||
427 | * @return int |
||
428 | */ |
||
429 | 1 | public function getOrder() |
|
433 | |||
434 | /** |
||
435 | * @param string $deleteHash |
||
436 | * @return AlbumInterface |
||
437 | */ |
||
438 | 1 | public function setDeleteHash($deleteHash) |
|
444 | |||
445 | /** |
||
446 | * @return string |
||
447 | */ |
||
448 | 1 | public function getDeleteHash() |
|
452 | |||
453 | /** |
||
454 | * @param int $imagesCount |
||
455 | * @return AlbumInterface |
||
456 | */ |
||
457 | 1 | public function setImagesCount($imagesCount) |
|
463 | |||
464 | /** |
||
465 | * @return int |
||
466 | */ |
||
467 | 1 | public function getImagesCount() |
|
471 | |||
472 | /** |
||
473 | * @param ImageInterface[] $images |
||
474 | * @return AlbumInterface |
||
475 | */ |
||
476 | 1 | public function setImages($images) |
|
482 | |||
483 | /** |
||
484 | * @return ImageInterface[] |
||
485 | */ |
||
486 | public function getImages() |
||
490 | } |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.