Conditions | 17 |
Paths | 727 |
Total Lines | 93 |
Code Lines | 56 |
Lines | 0 |
Ratio | 0 % |
Changes | 17 | ||
Bugs | 4 | Features | 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 |
||
49 | public function upload(File $file, $dest_folder = '', \DateTime $lifetime = null) |
||
50 | { |
||
51 | if ($file instanceof UploadedFile) { |
||
52 | if ($file->getError() !== null && $file->getError() !== 0) { |
||
53 | throw new UploadException($file->getErrorMessage()); |
||
54 | } |
||
55 | } |
||
56 | |||
57 | //preparing dir name |
||
58 | $dest_folder = date('Ymd') . '/' . date('G') . '/' . $dest_folder; |
||
59 | |||
60 | //checking mimetypes |
||
61 | $mimeTypePassed = false; |
||
62 | foreach ($this->allowedMimetypes as $mimeType) { |
||
63 | if (preg_match('@' . $mimeType . '@', $file->getMimeType())) { |
||
64 | $mimeTypePassed = true; |
||
65 | } |
||
66 | } |
||
67 | |||
68 | if (!$mimeTypePassed) { |
||
69 | throw new InvalidMimeTypeException('Only following filetypes are allowed : ' . implode(', ', $this->allowedMimetypes)); |
||
70 | } |
||
71 | |||
72 | $fs = new Filesystem(); |
||
73 | if (!$fs->exists($this->uploadDir . $dest_folder)) { |
||
74 | $fs->mkdir($this->uploadDir . $dest_folder); |
||
75 | } |
||
76 | |||
77 | $em = $this->entityManager; |
||
78 | $media = new Media(); |
||
79 | $media->setMime($file->getMimeType()); |
||
80 | |||
81 | // If there's one, we try to generate a new name |
||
82 | $extension = $file->getExtension(); |
||
83 | |||
84 | // Sanitizing the filename |
||
85 | $slugify = new Slugify(); |
||
86 | if ($file instanceof UploadedFile) { |
||
87 | if (empty($extension)) { |
||
88 | $extension = $file->getClientOriginalExtension(); |
||
89 | if (empty($extension)) { |
||
90 | $extension = $file->guessClientExtension(); |
||
91 | } |
||
92 | } |
||
93 | $filename = $slugify->slugify(basename($file->getClientOriginalName(), $extension)) . '.' . $extension; |
||
94 | } else { |
||
95 | if (empty($extension)) { |
||
96 | $extension = $file->guessClientExtension(); |
||
|
|||
97 | } |
||
98 | $filename = $slugify->slugify(basename($file->getFilename(), $extension)) . '.' . $extension; |
||
99 | } |
||
100 | |||
101 | // A media can have a lifetime and will be deleted with the cleanup function |
||
102 | if (!empty($lifetime)) { |
||
103 | $media->setLifetime($lifetime); |
||
104 | } |
||
105 | |||
106 | // Checking for a media with the same name |
||
107 | $mediaExists = $this->entityManager->getRepository('AlpixelMediaBundle:Media')->findOneByUri($dest_folder . $filename); |
||
108 | $mediaExists = (count($mediaExists) > 0); |
||
109 | if ($mediaExists === false) { |
||
110 | $mediaExists = $fs->exists($this->uploadDir . $dest_folder . $filename); |
||
111 | } |
||
112 | |||
113 | if ($mediaExists === true) { |
||
114 | $filename = basename($filename, '.' . $extension); |
||
115 | $i = 1; |
||
116 | do { |
||
117 | $media->setName($filename . '-' . $i++ . '.' . $extension); |
||
118 | $media->setUri($dest_folder . $media->getName()); |
||
119 | $mediaExists = $this->entityManager->getRepository('AlpixelMediaBundle:Media')->findOneByUri($media->getUri()); |
||
120 | $mediaExists = (count($mediaExists) > 0); |
||
121 | if ($mediaExists === false) { |
||
122 | $mediaExists = $fs->exists($this->uploadDir . $dest_folder . $filename); |
||
123 | } |
||
124 | } while ($mediaExists === true); |
||
125 | } else { |
||
126 | $media->setName($filename); |
||
127 | $media->setUri($dest_folder . $media->getName()); |
||
128 | } |
||
129 | |||
130 | $file->move($this->uploadDir . $dest_folder, $media->getName()); |
||
131 | chmod($this->uploadDir . $dest_folder . $media->getName(), 0664); |
||
132 | |||
133 | // Getting the salt defined in parameters.yml |
||
134 | $secret = $this->container->getParameter('secret'); |
||
135 | $media->setSecretKey(hash('sha256', $secret . $media->getName() . $media->getUri())); |
||
136 | |||
137 | $em->persist($media); |
||
138 | $em->flush(); |
||
139 | |||
140 | return $media; |
||
141 | } |
||
142 | |||
304 |
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: