| Conditions | 4 |
| Paths | 4 |
| Total Lines | 52 |
| Code Lines | 30 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | 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 |
||
| 123 | public function onDispatchError(MvcEvent $event) |
||
| 124 | { |
||
| 125 | if (Application::ERROR_ROUTER_NO_MATCH != $event->getError()) { |
||
| 126 | // ignore other than 'no route' errors |
||
| 127 | return; |
||
| 128 | } |
||
| 129 | |||
| 130 | // try match uri pattern |
||
| 131 | $uri = $event->getRequest()->getRequestUri(); |
||
| 132 | $pattern = '#^' . preg_quote($this->uriPath, '#') . '/[0-9a-z]/[0-9a-z]/([0-9a-z]+)\.[a-zA-Z]{3,4}$#'; |
||
| 133 | $matches = []; |
||
| 134 | preg_match($pattern, $uri, $matches); |
||
| 135 | |||
| 136 | if (! isset($matches[1])) { |
||
| 137 | // uri does not match organization image path |
||
| 138 | return; |
||
| 139 | } |
||
| 140 | |||
| 141 | // try get image |
||
| 142 | $id = $matches[1]; |
||
| 143 | $serviceManager = $event->getApplication()->getServiceManager(); |
||
| 144 | $repository = $serviceManager->get('repositories')->get('Organizations/OrganizationImage'); |
||
| 145 | $image = $repository->find($id); |
||
| 146 | |||
| 147 | if (! $image) { |
||
| 148 | // abort if image does not exist |
||
| 149 | return; |
||
| 150 | } |
||
| 151 | |||
| 152 | $resource = $image->getResource(); |
||
| 153 | $path = $this->getImagePath($image); |
||
| 154 | |||
| 155 | // create directory(ies) |
||
| 156 | $this->createDirectoryRecursively(dirname($path)); |
||
| 157 | |||
| 158 | // store image |
||
| 159 | file_put_contents($path, $resource); |
||
| 160 | rewind($resource); |
||
| 161 | |||
| 162 | // return image in response as a stream |
||
| 163 | $headers = new Headers(); |
||
| 164 | $headers->addHeaders([ |
||
| 165 | 'Content-Type' => $image->getType(), |
||
| 166 | 'Content-Length' => $image->getLength() |
||
| 167 | ]); |
||
| 168 | $response = new Stream(); |
||
| 169 | $response->setStream($resource); |
||
| 170 | $response->setStatusCode(Response::STATUS_CODE_200); |
||
| 171 | $response->setStreamName($image->getName()); |
||
| 172 | $response->setHeaders($headers); |
||
| 173 | $event->setResponse($response); |
||
| 174 | } |
||
| 175 | |||
| 222 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.