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 FileHandler 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 FileHandler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class FileHandler extends AbstractMediaHandler |
||
22 | { |
||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | const TYPE = 'file'; |
||
27 | |||
28 | /** |
||
29 | * @var string |
||
30 | */ |
||
31 | public $mediaPath; |
||
32 | |||
33 | /** |
||
34 | * @var Filesystem |
||
35 | */ |
||
36 | public $fileSystem; |
||
37 | |||
38 | /** |
||
39 | * @deprecated This property is deprecated since KunstmaanMediaBundle 5.7 and will be removed in KunstmaanMediaBundle 6.0. Use the `$mimeTypes` property instead. |
||
40 | * @var MimeTypeGuesser |
||
41 | */ |
||
42 | public $mimeTypeGuesser; |
||
43 | |||
44 | /** |
||
45 | * @deprecated This property is deprecated since KunstmaanMediaBundle 5.7 and will be removed in KunstmaanMediaBundle 6.0. Use the `$mimeTypes` property instead. |
||
46 | * @var ExtensionGuesser |
||
47 | */ |
||
48 | public $extensionGuesser; |
||
49 | |||
50 | /** @var MimeTypesInterface */ |
||
51 | private $mimeTypes; |
||
52 | |||
53 | /** |
||
54 | * Files with a blacklisted extension will be converted to txt |
||
55 | * |
||
56 | * @var array |
||
57 | */ |
||
58 | private $blacklistedExtensions = []; |
||
59 | |||
60 | /** |
||
61 | * @var SlugifierInterface |
||
62 | */ |
||
63 | private $slugifier; |
||
64 | |||
65 | /** |
||
66 | * Constructor |
||
67 | * |
||
68 | * @param int $priority |
||
69 | * @param MimeTypeGuesserFactoryInterface|MimeTypesInterface $mimeTypes |
||
70 | * @param ExtensionGuesserFactoryInterface $extensionGuesserFactoryInterface |
||
|
|||
71 | */ |
||
72 | 5 | public function __construct($priority, /*MimeTypesInterface*/ $mimeTypes, ExtensionGuesserFactoryInterface $extensionGuesserFactoryInterface = null) |
|
97 | |||
98 | /** |
||
99 | * @param SlugifierInterface $slugifier |
||
100 | */ |
||
101 | 1 | public function setSlugifier(SlugifierInterface $slugifier) |
|
105 | |||
106 | /** |
||
107 | * Inject the blacklisted |
||
108 | * |
||
109 | * @param array $blacklistedExtensions |
||
110 | */ |
||
111 | public function setBlacklistedExtensions(array $blacklistedExtensions) |
||
115 | |||
116 | /** |
||
117 | * Inject the path used in media urls. |
||
118 | * |
||
119 | * @param string $mediaPath |
||
120 | */ |
||
121 | public function setMediaPath($mediaPath) |
||
125 | |||
126 | public function setFileSystem(Filesystem $fileSystem) |
||
130 | |||
131 | /** |
||
132 | * @return string |
||
133 | */ |
||
134 | public function getName() |
||
138 | |||
139 | /** |
||
140 | * @return string |
||
141 | */ |
||
142 | public function getType() |
||
146 | |||
147 | /** |
||
148 | * @return string |
||
149 | */ |
||
150 | public function getFormType() |
||
154 | |||
155 | /** |
||
156 | * @param mixed $object |
||
157 | * |
||
158 | * @return bool |
||
159 | */ |
||
160 | 2 | public function canHandle($object) |
|
171 | |||
172 | /** |
||
173 | * @param Media $media |
||
174 | * |
||
175 | * @return FileHelper |
||
176 | */ |
||
177 | public function getFormHelper(Media $media) |
||
181 | |||
182 | /** |
||
183 | * @param Media $media |
||
184 | * |
||
185 | * @throws \RuntimeException when the file does not exist |
||
186 | */ |
||
187 | 1 | public function prepareMedia(Media $media) |
|
229 | |||
230 | /** |
||
231 | * @param Media $media |
||
232 | */ |
||
233 | public function removeMedia(Media $media) |
||
256 | |||
257 | /** |
||
258 | * {@inheritdoc} |
||
259 | */ |
||
260 | public function updateMedia(Media $media) |
||
264 | |||
265 | /** |
||
266 | * @param Media $media |
||
267 | */ |
||
268 | public function saveMedia(Media $media) |
||
277 | |||
278 | /** |
||
279 | * @param Media $media |
||
280 | * |
||
281 | * @return \Gaufrette\File |
||
282 | */ |
||
283 | public function getOriginalFile(Media $media) |
||
287 | |||
288 | /** |
||
289 | * @param mixed $data |
||
290 | * |
||
291 | * @return Media |
||
292 | */ |
||
293 | public function createNew($data) |
||
313 | |||
314 | /** |
||
315 | * {@inheritdoc} |
||
316 | */ |
||
317 | public function getShowTemplate(Media $media) |
||
321 | |||
322 | /** |
||
323 | * @return array |
||
324 | */ |
||
325 | public function getAddFolderActions() |
||
334 | |||
335 | /** |
||
336 | * @param Media $media |
||
337 | * |
||
338 | * @return string |
||
339 | */ |
||
340 | 1 | private function getFilePath(Media $media) |
|
361 | |||
362 | /** |
||
363 | * @param Media $media |
||
364 | * |
||
365 | * @return string |
||
366 | */ |
||
367 | private function getFileFolderPath(Media $media) |
||
371 | |||
372 | 1 | private function guessMimeType($pathName) |
|
381 | |||
382 | View Code Duplication | private function getExtensions($mimeType) |
|
391 | } |
||
392 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.