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 Filler 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 Filler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class Filler extends FillerPlugin |
||
25 | { |
||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | const NAME = 'anidb'; |
||
30 | |||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | const TITLE = 'AniDB.net'; |
||
35 | |||
36 | /** |
||
37 | * RegExp for get item id. |
||
38 | * |
||
39 | * @var string |
||
40 | */ |
||
41 | const REG_ITEM_ID = '#/perl\-bin/animedb\.pl\?show=anime&aid=(?<id>\d+)#'; |
||
42 | |||
43 | /** |
||
44 | * @var Browser |
||
45 | */ |
||
46 | protected $browser; |
||
47 | |||
48 | /** |
||
49 | * @var Registry |
||
50 | */ |
||
51 | protected $doctrine; |
||
52 | |||
53 | /** |
||
54 | * @var Downloader |
||
55 | */ |
||
56 | protected $downloader; |
||
57 | |||
58 | /** |
||
59 | * @var SummaryCleaner |
||
60 | */ |
||
61 | protected $cleaner; |
||
62 | |||
63 | /** |
||
64 | * @var string |
||
65 | */ |
||
66 | protected $locale; |
||
67 | |||
68 | /** |
||
69 | * AniDB category to genre. |
||
70 | * |
||
71 | * <code> |
||
72 | * { from: to, ... } |
||
73 | * </code> |
||
74 | * |
||
75 | * @var array |
||
76 | */ |
||
77 | protected $category_to_genre = [ |
||
78 | 'Alternative History' => 'History', |
||
79 | 'Anti-War' => 'War', |
||
80 | 'Badminton' => 'Sport', |
||
81 | 'Bakumatsu - Meiji Period' => 'History', |
||
82 | 'Band' => 'Music', |
||
83 | 'Baseball' => 'Sport', |
||
84 | 'Basketball' => 'Sport', |
||
85 | 'Battle Royale' => 'War', |
||
86 | 'Board Games' => 'Game', |
||
87 | 'Boxing' => 'Sport', |
||
88 | 'Catholic School' => 'School', |
||
89 | 'Chess' => 'Sport', |
||
90 | 'Clubs' => 'School', |
||
91 | 'College' => 'School', |
||
92 | 'Combat' => 'Action', |
||
93 | 'Conspiracy' => 'Thriller', |
||
94 | 'Contemporary Fantasy' => 'Fantasy', |
||
95 | 'Cops' => 'Police', |
||
96 | 'Daily Life' => 'Slice of life', |
||
97 | 'Dark Elf' => 'Fantasy', |
||
98 | 'Dark Fantasy' => 'Fantasy', |
||
99 | 'Dodgeball' => 'Sport', |
||
100 | 'Dragon' => 'Fantasy', |
||
101 | 'Edo Period' => 'Fantasy', |
||
102 | 'Elementary School' => 'School', |
||
103 | 'Elf' => 'Fantasy', |
||
104 | 'Fairies' => 'Fantasy', |
||
105 | 'Fantasy World' => 'Fantasy', |
||
106 | 'Feudal Warfare' => 'War', |
||
107 | 'Football' => 'Sport', |
||
108 | 'Formula Racing' => 'Sport', |
||
109 | 'Ghost' => 'Supernatural', |
||
110 | 'Go' => 'Game', |
||
111 | 'Golf' => 'Sport', |
||
112 | 'Gunfights' => 'War', |
||
113 | 'Gymnastics' => 'Sport', |
||
114 | 'Heian Period' => 'History', |
||
115 | 'High Fantasy' => 'Fantasy', |
||
116 | 'High School' => 'School', |
||
117 | 'Historical' => 'History', |
||
118 | 'Ice Skating' => 'Sport', |
||
119 | 'Inline Skating' => 'Sport', |
||
120 | 'Jousting' => 'Sport', |
||
121 | 'Judo' => 'Sport', |
||
122 | 'Kendo' => 'Sport', |
||
123 | 'Law and Order' => 'Police', |
||
124 | 'Magic Circles' => 'Magic', |
||
125 | 'Mahjong' => 'Game', |
||
126 | 'Mahou Shoujo' => 'Mahoe shoujo', |
||
127 | 'Martial Arts' => 'Martial arts', |
||
128 | 'Military' => 'War', |
||
129 | 'Motorsport' => 'Sport', |
||
130 | 'Muay Thai' => 'Sport', |
||
131 | 'Ninja' => 'Samurai', |
||
132 | 'Pirate' => 'Adventure', |
||
133 | 'Post-apocalypse' => 'Apocalyptic fiction', |
||
134 | 'Post-War' => 'War', |
||
135 | 'Proxy Battles' => 'War', |
||
136 | 'Reverse Harem' => 'Harem', |
||
137 | 'Rugby' => 'Sport', |
||
138 | 'School Dormitory' => 'School', |
||
139 | 'School Excursion' => 'School', |
||
140 | 'School Festival' => 'School', |
||
141 | 'School Life' => 'School', |
||
142 | 'School Sports Festival' => 'School', |
||
143 | 'Sci-Fi' => 'Sci-fi', |
||
144 | 'Sengoku Period' => 'History', |
||
145 | 'Shougi' => 'Game', |
||
146 | 'Shoujo Ai' => 'Shoujo-ai', |
||
147 | 'Shounen Ai' => 'Shounen-ai', |
||
148 | 'Spellcasting' => 'Magic', |
||
149 | 'Sports' => 'Sport', |
||
150 | 'Street Racing' => 'Cars', |
||
151 | 'Swimming' => 'Sport', |
||
152 | 'Swordplay' => 'Sport', |
||
153 | 'Tennis' => 'Sport', |
||
154 | 'Victorian Period' => 'History', |
||
155 | 'Volleyball' => 'Sport', |
||
156 | 'Witch' => 'Magic', |
||
157 | 'World War I' => 'War', |
||
158 | 'World War II' => 'War', |
||
159 | 'Wrestling' => 'Action', |
||
160 | ]; |
||
161 | |||
162 | /** |
||
163 | * @param Browser $browser |
||
164 | * @param Registry $doctrine |
||
165 | * @param Downloader $downloader |
||
166 | * @param SummaryCleaner $cleaner |
||
167 | * @param $locale |
||
168 | */ |
||
169 | public function __construct( |
||
182 | |||
183 | /** |
||
184 | * @return string |
||
185 | */ |
||
186 | public function getName() |
||
190 | |||
191 | /** |
||
192 | * @return string |
||
193 | */ |
||
194 | public function getTitle() |
||
198 | |||
199 | /** |
||
200 | * @return FillerForm |
||
201 | */ |
||
202 | public function getForm() |
||
206 | |||
207 | /** |
||
208 | * Build menu for plugin. |
||
209 | * |
||
210 | * @param ItemInterface $item |
||
211 | * |
||
212 | * @return ItemInterface |
||
213 | */ |
||
214 | public function buildMenu(ItemInterface $item) |
||
219 | |||
220 | /** |
||
221 | * Fill item from source. |
||
222 | * |
||
223 | * @param array $data |
||
224 | * |
||
225 | * @return Item|null |
||
226 | */ |
||
227 | public function fill(array $data) |
||
263 | |||
264 | /** |
||
265 | * @param Item $item |
||
266 | * @param Crawler $body |
||
267 | * |
||
268 | * @return Item |
||
269 | */ |
||
270 | public function setNames(Item $item, Crawler $body) |
||
307 | |||
308 | /** |
||
309 | * @param Item $item |
||
310 | * @param Crawler $body |
||
311 | * @param string $id |
||
312 | * |
||
313 | * @return Item |
||
314 | */ |
||
315 | public function setCover(Item $item, Crawler $body, $id) |
||
332 | |||
333 | /** |
||
334 | * @param Item $item |
||
335 | * @param Crawler $body |
||
336 | * |
||
337 | * @return Item |
||
338 | */ |
||
339 | public function setEpisodes(Item $item, Crawler $body) |
||
350 | |||
351 | /** |
||
352 | * @param Crawler $episode |
||
353 | * |
||
354 | * @return string |
||
355 | */ |
||
356 | protected function getEpisodeTitle(Crawler $episode) |
||
377 | |||
378 | /** |
||
379 | * @param Item $item |
||
380 | * @param Crawler $body |
||
381 | * |
||
382 | * @return Item |
||
383 | */ |
||
384 | public function setType(Item $item, Crawler $body) |
||
401 | |||
402 | /** |
||
403 | * @param Item $item |
||
404 | * @param Crawler $body |
||
405 | * |
||
406 | * @return Item |
||
407 | */ |
||
408 | public function setGenres(Item $item, Crawler $body) |
||
425 | |||
426 | /** |
||
427 | * @param string $url |
||
428 | * @param EntityInterface $entity |
||
429 | * |
||
430 | * @return bool |
||
431 | */ |
||
432 | protected function uploadImageFromUrl($url, EntityInterface $entity) |
||
436 | |||
437 | /** |
||
438 | * @param string $locale |
||
439 | * @param array $names |
||
440 | * |
||
441 | * @return string |
||
442 | */ |
||
443 | View Code Duplication | protected function getNameForLocale($locale, &$names) |
|
457 | |||
458 | /** |
||
459 | * @param string $url |
||
460 | * |
||
461 | * @return bool |
||
462 | */ |
||
463 | public function isSupportedUrl($url) |
||
467 | } |
||
468 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.