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