| Conditions | 9 |
| Paths | 7 |
| Total Lines | 56 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 36 | protected function inlineImage($excerpt) |
||
| 37 | { |
||
| 38 | $image = parent::inlineImage($excerpt); |
||
| 39 | |||
| 40 | if (!isset($image)) { |
||
| 41 | return null; |
||
| 42 | } |
||
| 43 | |||
| 44 | preg_match('/'.self::PATTERN.'/s', $image['element']['attributes']['src'], $matches); |
||
| 45 | // nothing to do |
||
| 46 | if (empty($matches)) { |
||
| 47 | return $image; |
||
| 48 | } |
||
| 49 | $image['element']['attributes']['src'] = $matches[1]; // URL without query string |
||
| 50 | |||
| 51 | // no config or no GD, can't process |
||
| 52 | if ($this->config === null || !extension_loaded('gd')) { |
||
| 53 | return $image; |
||
| 54 | } |
||
| 55 | |||
| 56 | // has resize value |
||
| 57 | if (array_key_exists(3, $matches) && $matches[3] == 'resize') { |
||
| 58 | $resize = (int) $matches[4]; |
||
| 59 | $image['element']['attributes']['width'] = $resize; |
||
| 60 | // external image? return data URL |
||
| 61 | if (preg_match('~^(?:f|ht)tps?://~i', $image['element']['attributes']['src'])) { |
||
| 62 | try { |
||
| 63 | $img = Image::make($image['element']['attributes']['src']); |
||
| 64 | } catch (NotReadableException $e) { |
||
| 65 | throw new Exception(sprintf('Cannot get image "%s"', $image['element']['attributes']['src'])); |
||
| 66 | } |
||
| 67 | $imgPath = (string) $img->encode('data-url'); |
||
| 68 | $image['element']['attributes']['src'] = $imgPath; |
||
| 69 | |||
| 70 | return $image; |
||
| 71 | } |
||
| 72 | // save thumb file |
||
| 73 | $img = Image::make($this->config->getStaticPath().'/'.$image['element']['attributes']['src']); |
||
| 74 | $img->resize($resize, null, function ($constraint) { |
||
| 75 | $constraint->aspectRatio(); |
||
| 76 | $constraint->upsize(); |
||
| 77 | }); |
||
| 78 | $imgThumbPath = '/'.self::TMP_DIR.'/images/thumbs/'.$resize; |
||
| 79 | $imgPath = $imgThumbPath.$image['element']['attributes']['src']; |
||
| 80 | $imgSubdir = Util::getFS()->makePathRelative( |
||
| 81 | dirname($imgPath), |
||
| 82 | $imgThumbPath |
||
| 83 | ); |
||
| 84 | Util::getFS()->mkdir($this->config->getDestinationDir().$imgThumbPath.'/'.$imgSubdir); |
||
| 85 | $img->save($this->config->getDestinationDir().$imgPath); |
||
| 86 | $imgPath = '/images/thumbs/'.$resize.$image['element']['attributes']['src']; |
||
| 87 | $image['element']['attributes']['src'] = $imgPath; |
||
| 88 | } |
||
| 89 | |||
| 90 | return $image; |
||
| 91 | } |
||
| 92 | } |
||
| 93 |