| Conditions | 18 |
| Paths | 483 |
| Total Lines | 103 |
| Code Lines | 61 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 58 | public function upload(File $file, $dest_folder = '', \DateTime $lifetime = null, $uploadConfiguration = null) |
||
| 59 | { |
||
| 60 | if ($file instanceof UploadedFile) { |
||
| 61 | if ($file->getError() !== null && $file->getError() !== 0) { |
||
| 62 | throw new UploadException($file->getErrorMessage()); |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | //preparing dir name |
||
| 67 | $dest_folder = date('Ymd').'/'.date('G').'/'.$dest_folder; |
||
| 68 | |||
| 69 | //checking mimetypes |
||
| 70 | if ($uploadConfiguration !== null) { |
||
| 71 | $allowedMimeTypes = $this->uploadConfiguration[$uploadConfiguration]['allowed_mimetypes']; |
||
| 72 | |||
| 73 | $mimeTypePassed = false; |
||
| 74 | foreach ($allowedMimeTypes as $mimeType) { |
||
| 75 | if (preg_match('@'.$mimeType.'@', $file->getMimeType())) { |
||
| 76 | $mimeTypePassed = true; |
||
| 77 | } |
||
| 78 | } |
||
| 79 | |||
| 80 | if (!$mimeTypePassed) { |
||
| 81 | throw new InvalidMimeTypeException( |
||
| 82 | 'Only following filetypes are allowed : '.implode(', ', $allowedMimeTypes) |
||
| 83 | ); |
||
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 87 | $fs = new Filesystem(); |
||
| 88 | if (!$fs->exists($this->uploadDir.$dest_folder)) { |
||
| 89 | $fs->mkdir($this->uploadDir.$dest_folder); |
||
| 90 | } |
||
| 91 | |||
| 92 | $em = $this->entityManager; |
||
| 93 | $media = new Media(); |
||
| 94 | $media->setMime($file->getMimeType()); |
||
| 95 | |||
| 96 | // If there's one, we try to generate a new name |
||
| 97 | $extension = $file->getExtension(); |
||
| 98 | |||
| 99 | // Sanitizing the filename |
||
| 100 | $slugify = new Slugify(); |
||
| 101 | if ($file instanceof UploadedFile) { |
||
| 102 | if (empty($extension)) { |
||
| 103 | $extension = $file->getClientOriginalExtension(); |
||
| 104 | if (empty($extension)) { |
||
| 105 | $extension = $file->guessClientExtension(); |
||
| 106 | } |
||
| 107 | } |
||
| 108 | $filename = $slugify->slugify(basename($file->getClientOriginalName(), $extension)).'.'.$extension; |
||
| 109 | } else { |
||
| 110 | if (empty($extension)) { |
||
| 111 | $extension = $file->guessClientExtension(); |
||
|
|
|||
| 112 | } |
||
| 113 | $filename = $slugify->slugify(basename($file->getFilename(), $extension)).'.'.$extension; |
||
| 114 | } |
||
| 115 | |||
| 116 | // A media can have a lifetime and will be deleted with the cleanup function |
||
| 117 | if (!empty($lifetime)) { |
||
| 118 | $media->setLifetime($lifetime); |
||
| 119 | } |
||
| 120 | |||
| 121 | // Checking for a media with the same name |
||
| 122 | $mediaExists = $this->entityManager->getRepository('AlpixelMediaBundle:Media')->findOneByUri( |
||
| 123 | $dest_folder.$filename |
||
| 124 | ); |
||
| 125 | $mediaExists = (count($mediaExists) > 0); |
||
| 126 | if ($mediaExists === false) { |
||
| 127 | $mediaExists = $fs->exists($this->uploadDir.$dest_folder.$filename); |
||
| 128 | } |
||
| 129 | |||
| 130 | if ($mediaExists === true) { |
||
| 131 | $filename = basename($filename, '.'.$extension); |
||
| 132 | $i = 1; |
||
| 133 | do { |
||
| 134 | $media->setName($filename.'-'.$i++.'.'.$extension); |
||
| 135 | $media->setUri($dest_folder.$media->getName()); |
||
| 136 | $mediaExists = $this->entityManager->getRepository('AlpixelMediaBundle:Media')->findOneByUri( |
||
| 137 | $media->getUri() |
||
| 138 | ); |
||
| 139 | $mediaExists = (count($mediaExists) > 0); |
||
| 140 | if ($mediaExists === false) { |
||
| 141 | $mediaExists = $fs->exists($this->uploadDir.$dest_folder.$filename); |
||
| 142 | } |
||
| 143 | } while ($mediaExists === true); |
||
| 144 | } else { |
||
| 145 | $media->setName($filename); |
||
| 146 | $media->setUri($dest_folder.$media->getName()); |
||
| 147 | } |
||
| 148 | |||
| 149 | $file->move($this->uploadDir.$dest_folder, $media->getName()); |
||
| 150 | chmod($this->uploadDir.$dest_folder.$media->getName(), 0664); |
||
| 151 | |||
| 152 | // Getting the salt defined in parameters.yml |
||
| 153 | $secret = $this->container->getParameter('secret'); |
||
| 154 | $media->setSecretKey(hash('sha256', $secret.$media->getName().$media->getUri())); |
||
| 155 | |||
| 156 | $em->persist($media); |
||
| 157 | $em->flush(); |
||
| 158 | |||
| 159 | return $media; |
||
| 160 | } |
||
| 161 | |||
| 309 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: