| Conditions | 16 | 
| Paths | 115 | 
| Total Lines | 102 | 
| Code Lines | 56 | 
| 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 | ||
| 76 | protected function inlineImage($excerpt) | ||
| 77 |     { | ||
| 78 | $image = parent::inlineImage($excerpt); | ||
| 79 | |||
| 80 |         if (!isset($image)) { | ||
| 81 | return null; | ||
| 82 | } | ||
| 83 | |||
| 84 | // fetch image path | ||
| 85 | $path = Util::joinFile( | ||
| 86 | $this->builder->getConfig()->getStaticTargetPath(), | ||
| 87 | ltrim($this->removeQuery($image['element']['attributes']['src'])) | ||
| 88 | ); | ||
| 89 |         if (Util\Url::isUrl($image['element']['attributes']['src'])) { | ||
| 90 | $path = $this->removeQuery($image['element']['attributes']['src']); | ||
| 91 | } | ||
| 92 |         if (!is_file($path) && !Util\Url::isRemoteFileExists($path)) { | ||
| 93 | return $image; | ||
| 94 | } | ||
| 95 | |||
| 96 | // fetch image properties | ||
| 97 | $size = getimagesize($path); | ||
| 98 | $width = $size[0]; | ||
| 99 | $type = $size[2]; | ||
| 100 | |||
| 101 | // sets default attributes | ||
| 102 | $image['element']['attributes']['width'] = $width; | ||
| 103 |         if ($type !== null) { | ||
| 104 | $image['element']['attributes']['loading'] = 'lazy'; | ||
| 105 | } | ||
| 106 | |||
| 107 | // captures query string. | ||
| 108 | // ie: "?resize=300&responsive" | ||
| 109 | $query = parse_url($image['element']['attributes']['src'], PHP_URL_QUERY); | ||
| 110 |         if ($query === null) { | ||
| 111 | return $image; | ||
| 112 | } | ||
| 113 | parse_str($query, $result); | ||
| 114 | // cleans URL | ||
| 115 | $image['element']['attributes']['src'] = $this->removeQuery($image['element']['attributes']['src']); | ||
| 116 | |||
| 117 | /** | ||
| 118 | * Should be responsive? | ||
| 119 | */ | ||
| 120 | $responsive = false; | ||
| 121 |         if (array_key_exists('responsive', $result) && !Util\Url::isUrl($image['element']['attributes']['src'])) { | ||
| 122 | $responsive = true; | ||
| 123 | // process | ||
| 124 | $steps = 5; | ||
| 125 | $wMin = 320; | ||
| 126 | $wMax = 2560; | ||
| 127 |             if ($width < $wMax) { | ||
| 128 | $wMax = $width; | ||
| 129 | } | ||
| 130 | $srcset = ''; | ||
| 131 |             for ($i = 1; $i <= $steps; $i++) { | ||
| 132 | $w = (int) ceil($wMin + ($wMax - $wMin) / $steps * $i); | ||
| 133 | $img = (new Image($this->builder)) | ||
| 134 | ->load($image['element']['attributes']['src']) | ||
| 135 | ->resize($w); | ||
| 136 |                 $srcset .= sprintf('%s %sw', $img, $w); | ||
| 137 |                 if ($i < $steps) { | ||
| 138 | $srcset .= ', '; | ||
| 139 | } | ||
| 140 | } | ||
| 141 | // ie: srcset="/img-480.jpg 480w, img-800.jpg 800w" | ||
| 142 | $image['element']['attributes']['srcset'] = $srcset; | ||
| 143 | } | ||
| 144 | |||
| 145 | /** | ||
| 146 | * Should be resized? | ||
| 147 | */ | ||
| 148 |         if (array_key_exists('resize', $result)) { | ||
| 149 | $size = (int) $result['resize']; | ||
| 150 | $width = $size; | ||
| 151 | |||
| 152 | $imageResized = (new Image($this->builder)) | ||
| 153 | ->load($image['element']['attributes']['src']) | ||
| 154 | ->resize($size); | ||
| 155 | |||
| 156 | $image['element']['attributes']['src'] = $imageResized; | ||
| 157 | $image['element']['attributes']['width'] = $width; | ||
| 158 | |||
| 159 |             if (Util\Url::isUrl($image['element']['attributes']['src'])) { | ||
| 160 | return $image; | ||
| 161 | } | ||
| 162 | } | ||
| 163 | |||
| 164 | // if responsive: set 'sizes' attribute | ||
| 165 |         if ($responsive) { | ||
| 166 | // sizes="(max-width: 2800px) 100vw, 2800px" | ||
| 167 |             $image['element']['attributes']['sizes'] = sprintf('(max-width: %spx) 100vw, %spx', $width, $width); | ||
| 168 | } | ||
| 169 | |||
| 170 | // set 'class' attribute | ||
| 171 |         if (array_key_exists('class', $result)) { | ||
| 172 | $class = $result['class']; | ||
| 173 | $class = strtr($class, ',', ' '); | ||
| 174 | $image['element']['attributes']['class'] = $class; | ||
| 175 | } | ||
| 176 | |||
| 177 | return $image; | ||
| 178 | } | ||
| 185 |