| Conditions | 11 |
| Paths | 32 |
| Total Lines | 60 |
| Code Lines | 31 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| 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 |
||
| 48 | public function resize(string $path, int $size): string |
||
| 49 | { |
||
| 50 | $external = false; |
||
| 51 | $this->source = $path; |
||
| 52 | $this->size = $size; |
||
| 53 | |||
| 54 | // is external image? |
||
| 55 | if (Util::isExternalUrl($path)) { |
||
| 56 | $external = true; |
||
| 57 | } |
||
| 58 | |||
| 59 | if (!$external) { |
||
| 60 | // source |
||
| 61 | $this->source = $this->config->getStaticPath().'/'.$path; |
||
| 62 | if (!Util::getFS()->exists($this->source)) { |
||
| 63 | throw new Exception(sprintf('Can\'t resize "%s": file doesn\'t exits.', $path)); |
||
| 64 | } |
||
| 65 | // destination |
||
| 66 | // ie: .cache/images/thumbs |
||
| 67 | $this->thumbsDir = (string) $this->config->get('cache.dir') |
||
| 68 | .'/'.(string) $this->config->get('cache.images.dir') |
||
| 69 | .'/'.(string) $this->config->get('cache.images.thumbs.dir') |
||
| 70 | .'/'.$this->size; |
||
| 71 | // ie: .cache/images/thumbs/img/logo.png |
||
| 72 | $this->imageRelPath = $this->thumbsDir.'/'.ltrim($path, '/'); |
||
| 73 | // full absolute path |
||
| 74 | $this->destination = $this->config->getDestinationDir().'/'.$this->imageRelPath; |
||
| 75 | if ((bool) $this->config->get('cache.external')) { |
||
| 76 | $this->destination = $this->imageRelPath; |
||
| 77 | } |
||
| 78 | } |
||
| 79 | |||
| 80 | // is size is already OK? |
||
| 81 | list($width, $height) = getimagesize($external ? $path : $this->source); |
||
| 82 | if ($width <= $this->size && $height <= $this->size) { |
||
| 83 | return $path; |
||
| 84 | } |
||
| 85 | |||
| 86 | // if GD extension is not installed: can't process |
||
| 87 | if (!extension_loaded('gd')) { |
||
| 88 | return $path; |
||
| 89 | } |
||
| 90 | |||
| 91 | // external image: return data URL |
||
| 92 | if ($external) { |
||
| 93 | try { |
||
| 94 | $img = ImageManager::make($path); |
||
| 95 | } catch (NotReadableException $e) { |
||
| 96 | throw new Exception(sprintf('Cannot get image "%s"', $path)); |
||
| 97 | } |
||
| 98 | |||
| 99 | return (string) $img->encode('data-url'); |
||
| 100 | } |
||
| 101 | |||
| 102 | // resize |
||
| 103 | $this->doResize(); |
||
| 104 | |||
| 105 | // return relative path |
||
| 106 | return '/'.$this->config->get('cache.images.dir') |
||
|
|
|||
| 107 | .'/'.(string) $this->config->get('cache.images.thumbs.dir').'/'.$this->size.'/'.ltrim($path, '/'); |
||
| 108 | } |
||
| 131 |