| Conditions | 14 |
| Paths | 113 |
| Total Lines | 99 |
| Code Lines | 54 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 7 | ||
| 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 |
||
| 35 | protected function inlineImage($excerpt) |
||
| 36 | { |
||
| 37 | $image = parent::inlineImage($excerpt); |
||
| 38 | |||
| 39 | if (!isset($image)) { |
||
| 40 | return null; |
||
| 41 | } |
||
| 42 | |||
| 43 | $path = Util::joinFile( |
||
| 44 | $this->builder->getConfig()->getStaticTargetPath(), |
||
| 45 | ltrim($this->removeQuery($image['element']['attributes']['src'])) |
||
| 46 | ); |
||
| 47 | if (Util::isExternalUrl($image['element']['attributes']['src'])) { |
||
| 48 | $path = $this->removeQuery($image['element']['attributes']['src']); |
||
| 49 | } |
||
| 50 | $size = getimagesize($path); |
||
| 51 | $width = $size[0]; |
||
| 52 | $type = $size[2]; |
||
| 53 | |||
| 54 | // sets default attributes |
||
| 55 | $image['element']['attributes']['width'] = $width; |
||
| 56 | if ($type !== null) { |
||
| 57 | $image['element']['attributes']['loading'] = 'lazy'; |
||
| 58 | } |
||
| 59 | |||
| 60 | // captures query string. |
||
| 61 | // ie: "?resize=300&responsive" |
||
| 62 | $query = parse_url($image['element']['attributes']['src'], PHP_URL_QUERY); |
||
| 63 | if ($query === null) { |
||
| 64 | return $image; |
||
| 65 | } |
||
| 66 | parse_str($query, $result); |
||
| 67 | // cleans URL |
||
| 68 | $image['element']['attributes']['src'] = $this->removeQuery($image['element']['attributes']['src']); |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Should be responsive? |
||
| 72 | */ |
||
| 73 | $responsive = false; |
||
| 74 | if (array_key_exists('responsive', $result) && !Util::isExternalUrl($image['element']['attributes']['src'])) { |
||
| 75 | $responsive = true; |
||
| 76 | // process |
||
| 77 | $steps = 5; |
||
| 78 | $wMin = 320; |
||
| 79 | $wMax = 2560; |
||
| 80 | if ($width < $wMax) { |
||
| 81 | $wMax = $width; |
||
| 82 | } |
||
| 83 | $srcset = ''; |
||
| 84 | for ($i = 1; $i <= $steps; $i++) { |
||
| 85 | $w = (int) ceil($wMin + ($wMax - $wMin) / $steps * $i); |
||
| 86 | $img = (new Image($this->builder))->resize($image['element']['attributes']['src'], $w); |
||
| 87 | $srcset .= sprintf('%s %sw', $img, $w); |
||
| 88 | if ($i < $steps) { |
||
| 89 | $srcset .= ', '; |
||
| 90 | } |
||
| 91 | } |
||
| 92 | // ie: srcset="/img-480.jpg 480w, img-800.jpg 800w" |
||
| 93 | $image['element']['attributes']['srcset'] = $srcset; |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Should be resized? |
||
| 98 | */ |
||
| 99 | if (array_key_exists('resize', $result)) { |
||
| 100 | $size = (int) $result['resize']; |
||
| 101 | $width = $size; |
||
| 102 | |||
| 103 | $imageResized = (new Image($this->builder)) |
||
| 104 | ->resize($image['element']['attributes']['src'], $size); |
||
| 105 | |||
| 106 | if (Util::isExternalUrl($image['element']['attributes']['src'])) { |
||
| 107 | return $image; |
||
| 108 | } |
||
| 109 | |||
| 110 | $image['element']['attributes']['src'] = $imageResized; |
||
| 111 | |||
| 112 | $path = Util::joinPath( |
||
|
|
|||
| 113 | $this->builder->getConfig()->getOutputPath(), |
||
| 114 | ltrim($image['element']['attributes']['src']) |
||
| 115 | ); |
||
| 116 | |||
| 117 | $image['element']['attributes']['width'] = $width; |
||
| 118 | } |
||
| 119 | |||
| 120 | // if responsive: set 'sizes' attribute |
||
| 121 | if ($responsive) { |
||
| 122 | // sizes="(max-width: 2800px) 100vw, 2800px" |
||
| 123 | $image['element']['attributes']['sizes'] = sprintf('(max-width: %spx) 100vw, %spx', $width, $width); |
||
| 124 | } |
||
| 125 | |||
| 126 | // set 'class' attribute |
||
| 127 | if (array_key_exists('class', $result)) { |
||
| 128 | $class = $result['class']; |
||
| 129 | $class = strtr($class, ',', ' '); |
||
| 130 | $image['element']['attributes']['class'] = $class; |
||
| 131 | } |
||
| 132 | |||
| 133 | return $image; |
||
| 134 | } |
||
| 141 |