| Conditions | 7 |
| Paths | 10 |
| Total Lines | 59 |
| Code Lines | 39 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 9 | ||
| Bugs | 1 | 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 |
||
| 94 | public function showMediaAction(Media $media, $filter = null) |
||
| 95 | { |
||
| 96 | $response = new Response(); |
||
| 97 | $lastModified = new \DateTime('now'); |
||
| 98 | |||
| 99 | //Checking if it is an image or not |
||
| 100 | $src = $this->get('alpixel_media.manager')->getAbsolutePath($media); |
||
| 101 | $isImage = @getimagesize($src); |
||
| 102 | |||
| 103 | if ($isImage) { |
||
| 104 | $response->headers->set('Content-disposition', 'inline;filename='.$media->getName()); |
||
| 105 | if (!empty($filter) && $isImage) { |
||
| 106 | $src = $this->get('alpixel_media.manager')->getAbsolutePath($media, $filter); |
||
| 107 | $dataManager = $this->get('liip_imagine.data.manager'); // the data manager service |
||
| 108 | $filterManager = $this->get('liip_imagine.filter.manager'); // the filter manager service |
||
| 109 | $uploadDir = $this->get('alpixel_media.manager')->getUploadDir($filter); |
||
| 110 | |||
| 111 | if (!is_file($src)) { |
||
| 112 | $fs = new Filesystem(); |
||
| 113 | if (!$fs->exists($uploadDir.$media->getFolder())) { |
||
| 114 | $fs->mkdir($uploadDir.$media->getFolder()); |
||
| 115 | } |
||
| 116 | |||
| 117 | $path = 'upload/'.$media->getUri(); |
||
| 118 | |||
| 119 | // find the image and determine its type |
||
| 120 | $image = $dataManager->find($filter, $path); |
||
| 121 | |||
| 122 | // run the filter |
||
| 123 | $responseData = $filterManager->applyFilter($image, $filter); |
||
| 124 | $data = $responseData->getContent(); |
||
| 125 | file_put_contents($uploadDir.$media->getUri(), $data); |
||
| 126 | } else { |
||
| 127 | $data = file_get_contents($src); |
||
| 128 | $lastModified->setTimestamp(filemtime($src)); |
||
| 129 | } |
||
| 130 | } else { |
||
| 131 | $src = $this->get('alpixel_media.manager')->getAbsolutePath($media); |
||
| 132 | $lastModified->setTimestamp(filemtime($src)); |
||
| 133 | $data = file_get_contents($src); |
||
| 134 | } |
||
| 135 | } else { |
||
| 136 | $lastModified->setTimestamp(filemtime($src)); |
||
| 137 | $data = file_get_contents($src); |
||
| 138 | $response->headers->set('Content-disposition', 'attachment;filename='.basename($media->getUri())); |
||
| 139 | } |
||
| 140 | |||
| 141 | $response->setLastModified($lastModified); |
||
| 142 | $response->setPublic(); |
||
| 143 | $response->headers->set('Content-Type', $media->getMime()); |
||
| 144 | |||
| 145 | if ($response->isNotModified($this->get('request'))) { |
||
| 146 | return $response; |
||
| 147 | } |
||
| 148 | |||
| 149 | $response->setContent($data); |
||
| 150 | |||
| 151 | return $response; |
||
| 152 | } |
||
| 153 | } |
||
| 154 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: