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 |
||
| 20 | class FileHandler extends AbstractMediaHandler |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | const TYPE = 'file'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | public $mediaPath; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var Filesystem |
||
| 34 | */ |
||
| 35 | public $fileSystem; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var MimeTypeGuesserInterface |
||
| 39 | */ |
||
| 40 | public $mimeTypeGuesser; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var ExtensionGuesserInterface |
||
| 44 | */ |
||
| 45 | public $extensionGuesser; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Files with a blacklisted extension will be converted to txt |
||
| 49 | * |
||
| 50 | * @var array |
||
| 51 | */ |
||
| 52 | private $blacklistedExtensions = array(); |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var SlugifierInterface |
||
| 56 | */ |
||
| 57 | private $slugifier; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Constructor |
||
| 61 | * |
||
| 62 | * @param int $priority |
||
| 63 | * @param MimeTypeGuesserFactoryInterface $mimeTypeGuesserFactory |
||
| 64 | * @param ExtensionGuesserFactoryInterface $extensionGuesserFactoryInterface |
||
| 65 | */ |
||
| 66 | 5 | public function __construct($priority, MimeTypeGuesserFactoryInterface $mimeTypeGuesserFactory, ExtensionGuesserFactoryInterface $extensionGuesserFactoryInterface) |
|
| 72 | |||
| 73 | /** |
||
| 74 | * @param SlugifierInterface $slugifier |
||
| 75 | */ |
||
| 76 | 1 | public function setSlugifier(SlugifierInterface $slugifier) |
|
| 77 | { |
||
| 78 | 1 | $this->slugifier = $slugifier; |
|
| 79 | 1 | } |
|
| 80 | |||
| 81 | /** |
||
| 82 | * Inject the blacklisted |
||
| 83 | * |
||
| 84 | * @param array $blacklistedExtensions |
||
| 85 | */ |
||
| 86 | public function setBlacklistedExtensions(array $blacklistedExtensions) |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Inject the path used in media urls. |
||
| 93 | * |
||
| 94 | * @param string $mediaPath |
||
| 95 | */ |
||
| 96 | public function setMediaPath($mediaPath) |
||
| 100 | |||
| 101 | public function setFileSystem(Filesystem $fileSystem) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @return string |
||
| 108 | */ |
||
| 109 | public function getName() |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @return string |
||
| 116 | */ |
||
| 117 | public function getType() |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @return string |
||
| 124 | */ |
||
| 125 | public function getFormType() |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @param mixed $object |
||
| 132 | * |
||
| 133 | * @return bool |
||
| 134 | */ |
||
| 135 | 2 | public function canHandle($object) |
|
| 146 | |||
| 147 | /** |
||
| 148 | * @param Media $media |
||
| 149 | * |
||
| 150 | * @return FileHelper |
||
| 151 | */ |
||
| 152 | public function getFormHelper(Media $media) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @param Media $media |
||
| 159 | * |
||
| 160 | * @throws \RuntimeException when the file does not exist |
||
| 161 | */ |
||
| 162 | 1 | public function prepareMedia(Media $media) |
|
| 163 | { |
||
| 164 | 1 | if (null === $media->getUuid()) { |
|
| 165 | 1 | $uuid = uniqid(); |
|
| 166 | 1 | $media->setUuid($uuid); |
|
| 167 | } |
||
| 168 | |||
| 169 | 1 | $content = $media->getContent(); |
|
| 170 | 1 | if (empty($content)) { |
|
| 171 | return; |
||
| 172 | } |
||
| 173 | |||
| 174 | 1 | if (!$content instanceof File) { |
|
| 175 | if (!is_file($content)) { |
||
| 176 | throw new \RuntimeException('Invalid file'); |
||
| 177 | } |
||
| 178 | |||
| 179 | $file = new File($content); |
||
| 180 | $media->setContent($file); |
||
| 181 | } |
||
| 182 | |||
| 183 | 1 | $contentType = $this->mimeTypeGuesser->guess($content->getPathname()); |
|
| 184 | 1 | if ($content instanceof UploadedFile) { |
|
| 185 | $pathInfo = pathinfo($content->getClientOriginalName()); |
||
| 186 | |||
| 187 | if (!\array_key_exists('extension', $pathInfo)) { |
||
| 188 | $pathInfo['extension'] = $this->extensionGuesser->guess($contentType); |
||
| 189 | } |
||
| 190 | |||
| 191 | $media->setOriginalFilename($this->slugifier->slugify($pathInfo['filename']).'.'.$pathInfo['extension']); |
||
| 192 | $name = $media->getName(); |
||
| 193 | |||
| 194 | if (empty($name)) { |
||
| 195 | $media->setName($media->getOriginalFilename()); |
||
| 196 | } |
||
| 197 | } |
||
| 198 | |||
| 199 | 1 | $media->setContentType($contentType); |
|
| 200 | 1 | $media->setFileSize(filesize($media->getContent())); |
|
| 201 | 1 | $media->setUrl($this->mediaPath . $this->getFilePath($media)); |
|
| 202 | 1 | $media->setLocation('local'); |
|
| 203 | 1 | } |
|
| 204 | |||
| 205 | /** |
||
| 206 | * @param Media $media |
||
| 207 | */ |
||
| 208 | public function removeMedia(Media $media) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * {@inheritdoc} |
||
| 234 | */ |
||
| 235 | public function updateMedia(Media $media) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @param Media $media |
||
| 242 | */ |
||
| 243 | public function saveMedia(Media $media) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @param Media $media |
||
| 255 | * |
||
| 256 | * @return \Gaufrette\File |
||
| 257 | */ |
||
| 258 | public function getOriginalFile(Media $media) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @param mixed $data |
||
| 265 | * |
||
| 266 | * @return Media |
||
|
|
|||
| 267 | */ |
||
| 268 | public function createNew($data) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * {@inheritdoc} |
||
| 291 | */ |
||
| 292 | public function getShowTemplate(Media $media) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @return array |
||
| 299 | */ |
||
| 300 | public function getAddFolderActions() |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @param Media $media |
||
| 312 | * |
||
| 313 | * @return string |
||
| 314 | */ |
||
| 315 | 1 | private function getFilePath(Media $media) |
|
| 316 | { |
||
| 317 | 1 | $filename = $media->getOriginalFilename(); |
|
| 318 | 1 | $filename = str_replace(array('/', '\\', '%'), '', $filename); |
|
| 319 | |||
| 320 | 1 | if (!empty($this->blacklistedExtensions)) { |
|
| 321 | $filename = preg_replace('/\.('.implode('|', $this->blacklistedExtensions).')$/', '.txt', $filename); |
||
| 322 | } |
||
| 323 | |||
| 324 | 1 | $parts = pathinfo($filename); |
|
| 325 | 1 | $filename = $this->slugifier->slugify($parts['filename']); |
|
| 326 | 1 | if (\array_key_exists('extension', $parts)) { |
|
| 327 | $filename .= '.'.strtolower($parts['extension']); |
||
| 328 | } |
||
| 329 | |||
| 330 | 1 | return sprintf( |
|
| 331 | 1 | '%s/%s', |
|
| 332 | 1 | $media->getUuid(), |
|
| 333 | $filename |
||
| 334 | ); |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @param Media $media |
||
| 339 | * |
||
| 340 | * @return string |
||
| 341 | */ |
||
| 342 | private function getFileFolderPath(Media $media) |
||
| 346 | } |
||
| 347 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.