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 | * @var MimeTypeGuesser |
||
40 | */ |
||
41 | public $mimeTypeGuesser; |
||
42 | |||
43 | /** |
||
44 | * @var ExtensionGuesser |
||
45 | */ |
||
46 | public $extensionGuesser; |
||
47 | |||
48 | /** |
||
49 | * Files with a blacklisted extension will be converted to txt |
||
50 | * |
||
51 | * @var array |
||
52 | */ |
||
53 | private $blacklistedExtensions = []; |
||
54 | |||
55 | /** |
||
56 | * @var SlugifierInterface |
||
57 | */ |
||
58 | private $slugifier; |
||
59 | |||
60 | /** |
||
61 | * Constructor |
||
62 | * |
||
63 | * @param int $priority |
||
64 | * @param MimeTypeGuesserFactoryInterface $mimeTypeGuesserFactory |
||
65 | * @param ExtensionGuesserFactoryInterface|null $extensionGuesserFactoryInterface |
||
66 | */ |
||
67 | 5 | public function __construct($priority, $mimeTypeGuesserFactory, $extensionGuesserFactoryInterface, MimeTypes $mimeTypes = null) |
|
73 | |||
74 | /** |
||
75 | * @param SlugifierInterface $slugifier |
||
76 | */ |
||
77 | 1 | public function setSlugifier(SlugifierInterface $slugifier) |
|
81 | |||
82 | /** |
||
83 | * Inject the blacklisted |
||
84 | * |
||
85 | * @param array $blacklistedExtensions |
||
86 | */ |
||
87 | public function setBlacklistedExtensions(array $blacklistedExtensions) |
||
91 | |||
92 | /** |
||
93 | * Inject the path used in media urls. |
||
94 | * |
||
95 | * @param string $mediaPath |
||
96 | */ |
||
97 | public function setMediaPath($mediaPath) |
||
101 | |||
102 | public function setFileSystem(Filesystem $fileSystem) |
||
106 | |||
107 | /** |
||
108 | * @return string |
||
109 | */ |
||
110 | public function getName() |
||
114 | |||
115 | /** |
||
116 | * @return string |
||
117 | */ |
||
118 | public function getType() |
||
122 | |||
123 | /** |
||
124 | * @return string |
||
125 | */ |
||
126 | public function getFormType() |
||
130 | |||
131 | /** |
||
132 | * @param mixed $object |
||
133 | * |
||
134 | * @return bool |
||
135 | */ |
||
136 | public function canHandle($object) |
||
147 | |||
148 | /** |
||
149 | * @param Media $media |
||
150 | * |
||
151 | * @return FileHelper |
||
152 | */ |
||
153 | public function getFormHelper(Media $media) |
||
157 | |||
158 | /** |
||
159 | * @param Media $media |
||
160 | * |
||
161 | * @throws \RuntimeException when the file does not exist |
||
162 | */ |
||
163 | 1 | public function prepareMedia(Media $media) |
|
205 | |||
206 | /** |
||
207 | * @param Media $media |
||
208 | */ |
||
209 | public function removeMedia(Media $media) |
||
232 | |||
233 | /** |
||
234 | * {@inheritdoc} |
||
235 | */ |
||
236 | public function updateMedia(Media $media) |
||
240 | |||
241 | /** |
||
242 | * @param Media $media |
||
243 | */ |
||
244 | public function saveMedia(Media $media) |
||
253 | |||
254 | /** |
||
255 | * @param Media $media |
||
256 | * |
||
257 | * @return \Gaufrette\File |
||
258 | */ |
||
259 | public function getOriginalFile(Media $media) |
||
263 | |||
264 | /** |
||
265 | * @param mixed $data |
||
266 | * |
||
267 | * @return Media |
||
268 | */ |
||
269 | public function createNew($data) |
||
289 | |||
290 | /** |
||
291 | * {@inheritdoc} |
||
292 | */ |
||
293 | public function getShowTemplate(Media $media) |
||
297 | |||
298 | /** |
||
299 | * @return array |
||
300 | */ |
||
301 | public function getAddFolderActions() |
||
310 | |||
311 | /** |
||
312 | * @param Media $media |
||
313 | * |
||
314 | * @return string |
||
315 | */ |
||
316 | 1 | private function getFilePath(Media $media) |
|
337 | |||
338 | /** |
||
339 | * @param Media $media |
||
340 | * |
||
341 | * @return string |
||
342 | */ |
||
343 | private function getFileFolderPath(Media $media) |
||
347 | |||
348 | 1 | private function guessMimeType($pathName) |
|
356 | |||
357 | View Code Duplication | private function getExtensions($mimeType) |
|
365 | } |
||
366 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: