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 | * |
||
41 | * @var MimeTypeGuesser |
||
42 | */ |
||
43 | public $mimeTypeGuesser; |
||
44 | |||
45 | /** |
||
46 | * @deprecated This property is deprecated since KunstmaanMediaBundle 5.7 and will be removed in KunstmaanMediaBundle 6.0. Use the `$mimeTypes` property instead. |
||
47 | * |
||
48 | * @var ExtensionGuesser |
||
49 | */ |
||
50 | public $extensionGuesser; |
||
51 | |||
52 | /** @var MimeTypesInterface */ |
||
53 | private $mimeTypes; |
||
54 | |||
55 | /** |
||
56 | * Files with a blacklisted extension will be converted to txt |
||
57 | * |
||
58 | * @var array |
||
59 | */ |
||
60 | private $blacklistedExtensions = []; |
||
61 | |||
62 | /** |
||
63 | * @var SlugifierInterface |
||
64 | */ |
||
65 | private $slugifier; |
||
66 | |||
67 | /** |
||
68 | * Constructor |
||
69 | * |
||
70 | * @param int $priority |
||
71 | * @param MimeTypeGuesserFactoryInterface|MimeTypesInterface $mimeTypes |
||
72 | * @param ExtensionGuesserFactoryInterface $extensionGuesserFactoryInterface |
||
|
|||
73 | */ |
||
74 | 5 | public function __construct($priority, /*MimeTypesInterface*/ $mimeTypes, ExtensionGuesserFactoryInterface $extensionGuesserFactoryInterface = null) |
|
99 | |||
100 | /** |
||
101 | * @param SlugifierInterface $slugifier |
||
102 | */ |
||
103 | 1 | public function setSlugifier(SlugifierInterface $slugifier) |
|
107 | |||
108 | /** |
||
109 | * Inject the blacklisted |
||
110 | * |
||
111 | * @param array $blacklistedExtensions |
||
112 | */ |
||
113 | public function setBlacklistedExtensions(array $blacklistedExtensions) |
||
117 | |||
118 | /** |
||
119 | * Inject the path used in media urls. |
||
120 | * |
||
121 | * @param string $mediaPath |
||
122 | */ |
||
123 | public function setMediaPath($mediaPath) |
||
127 | |||
128 | public function setFileSystem(Filesystem $fileSystem) |
||
132 | |||
133 | /** |
||
134 | * @return string |
||
135 | */ |
||
136 | public function getName() |
||
140 | |||
141 | /** |
||
142 | * @return string |
||
143 | */ |
||
144 | public function getType() |
||
148 | |||
149 | /** |
||
150 | * @return string |
||
151 | */ |
||
152 | public function getFormType() |
||
156 | |||
157 | /** |
||
158 | * @param mixed $object |
||
159 | * |
||
160 | * @return bool |
||
161 | */ |
||
162 | 2 | public function canHandle($object) |
|
173 | |||
174 | /** |
||
175 | * @param Media $media |
||
176 | * |
||
177 | * @return FileHelper |
||
178 | */ |
||
179 | public function getFormHelper(Media $media) |
||
183 | |||
184 | /** |
||
185 | * @param Media $media |
||
186 | * |
||
187 | * @throws \RuntimeException when the file does not exist |
||
188 | */ |
||
189 | 1 | public function prepareMedia(Media $media) |
|
231 | |||
232 | /** |
||
233 | * @param Media $media |
||
234 | */ |
||
235 | public function removeMedia(Media $media) |
||
258 | |||
259 | /** |
||
260 | * {@inheritdoc} |
||
261 | */ |
||
262 | public function updateMedia(Media $media) |
||
266 | |||
267 | /** |
||
268 | * @param Media $media |
||
269 | */ |
||
270 | public function saveMedia(Media $media) |
||
279 | |||
280 | /** |
||
281 | * @param Media $media |
||
282 | * |
||
283 | * @return \Gaufrette\File |
||
284 | */ |
||
285 | public function getOriginalFile(Media $media) |
||
289 | |||
290 | /** |
||
291 | * @param mixed $data |
||
292 | * |
||
293 | * @return Media |
||
294 | */ |
||
295 | public function createNew($data) |
||
315 | |||
316 | /** |
||
317 | * {@inheritdoc} |
||
318 | */ |
||
319 | public function getShowTemplate(Media $media) |
||
323 | |||
324 | /** |
||
325 | * @return array |
||
326 | */ |
||
327 | public function getAddFolderActions() |
||
336 | |||
337 | /** |
||
338 | * @param Media $media |
||
339 | * |
||
340 | * @return string |
||
341 | */ |
||
342 | 1 | private function getFilePath(Media $media) |
|
363 | |||
364 | /** |
||
365 | * @param Media $media |
||
366 | * |
||
367 | * @return string |
||
368 | */ |
||
369 | private function getFileFolderPath(Media $media) |
||
373 | |||
374 | 1 | private function guessMimeType($pathName) |
|
383 | |||
384 | View Code Duplication | private function getExtensions($mimeType) |
|
393 | } |
||
394 |
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.