| Conditions | 13 |
| Paths | 33 |
| Total Lines | 60 |
| Code Lines | 33 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 23 | public function profile(\Base $f3, array $params = []) |
||
| 24 | { |
||
| 25 | // get asset table entry |
||
| 26 | $asset = new Mappers\Assets; |
||
| 27 | $asset->load(['users_uuid = ? AND ' . $asset->quotekey('key') . ' = ?', $params['uuid'], $params['key']]); |
||
| 28 | if (null === $asset->uuid) { |
||
| 29 | return $f3->status(404); |
||
| 30 | } |
||
| 31 | |||
| 32 | // get image dimensions |
||
| 33 | $max = $f3->get('assets.image.max'); |
||
| 34 | $height = abs((int) $f3->get('REQUEST.height')); |
||
| 35 | $width = abs((int) $f3->get('REQUEST.width')); |
||
| 36 | |||
| 37 | if (empty($height) || empty($width)) { |
||
| 38 | // resize on longest side |
||
| 39 | if ($height > $width) { |
||
| 40 | $width = $height; |
||
| 41 | } elseif ($width > $height) { |
||
| 42 | $height = $width; |
||
| 43 | } else { |
||
| 44 | // use default height/width if missing |
||
| 45 | $height = $f3->get('assets.image.default.height'); |
||
| 46 | $width = $f3->get('assets.image.default.width'); |
||
| 47 | } |
||
| 48 | } elseif ($width > $max['width'] || $height > $max['height']) { |
||
| 49 | // make sure maximum width/height not exceeded |
||
| 50 | $height = $height > $max['height'] ? $max['height'] : $height; |
||
| 51 | $width = $width > $max['width'] ? $max['width'] : $width; |
||
| 52 | } |
||
| 53 | |||
| 54 | // load user mapper |
||
| 55 | $usersMapper = new Mappers\Users; |
||
| 56 | $usersMapper->load($params['uuid']); |
||
| 57 | |||
| 58 | // work out filename |
||
| 59 | $hash = $f3->hash($asset); // generate unique hash for asset |
||
| 60 | $filename = 'profile_' . $width . 'x' . $height . '.jpg'; |
||
| 61 | |||
| 62 | // return the url if exists |
||
| 63 | $url = $usersMapper->profileImageUrl($filename); |
||
| 64 | if (false !== $url) { |
||
| 65 | return $f3->reroute($url); |
||
| 66 | } |
||
| 67 | |||
| 68 | // 404 if the original asset file does not exist |
||
| 69 | if (!file_exists($asset->filename)) { |
||
| 70 | return $f3->status(404); |
||
| 71 | } |
||
| 72 | |||
| 73 | // create new resized file |
||
| 74 | $img = new \Image($asset->filename); |
||
| 75 | $img->resize($width, $height); |
||
| 76 | if (!$f3->write($usersMapper->profileImageFilePath($filename), $img->dump('jpeg', $f3->get('assets.image.default.quality.jpg')))) { |
||
| 77 | return $f3->status(404); |
||
| 78 | } |
||
| 79 | |||
| 80 | // serve the generated image via the web |
||
| 81 | return $f3->reroute($url); |
||
| 82 | } |
||
| 83 | } |
||
| 84 |
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.