| Conditions | 10 |
| Paths | 46 |
| Total Lines | 93 |
| Code Lines | 48 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 2 | 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 |
||
| 180 | protected function blockImage($Line) |
||
| 181 | { |
||
| 182 | if (1 !== preg_match($this->MarkdownImageRegex, $Line['text'])) { |
||
| 183 | return; |
||
| 184 | } |
||
| 185 | |||
| 186 | $InlineImage = $this->inlineImage($Line); |
||
| 187 | if (!isset($InlineImage)) { |
||
| 188 | return; |
||
| 189 | } |
||
| 190 | |||
| 191 | $block = $InlineImage; |
||
| 192 | |||
| 193 | /* |
||
| 194 | <figure> |
||
| 195 | <picture> |
||
| 196 | <source type="image/webp" |
||
| 197 | srcset="..." |
||
| 198 | sizes="..." |
||
| 199 | > |
||
| 200 | <img src="..." |
||
| 201 | srcset="..." |
||
| 202 | sizes="..." |
||
| 203 | > |
||
| 204 | </picture> |
||
| 205 | <figcaption>...</figcaption> |
||
| 206 | </figure> |
||
| 207 | */ |
||
| 208 | |||
| 209 | // creates a <picture> element with a <source> (WebP) and an <img> element |
||
| 210 | if ($this->builder->getConfig()->get('body.images.webp.enabled') ?? false && !Image::isAnimatedGif($InlineImage['element']['attributes']['src'])) { |
||
| 211 | try { |
||
| 212 | if (is_string($InlineImage['element']['attributes']['src'])) { |
||
| 213 | throw new RuntimeException(\sprintf('Can\'t convert "%s" to WebP', $InlineImage['element']['attributes']['src'])); |
||
| 214 | } |
||
| 215 | $assetWebp = Image::convertTopWebp($InlineImage['element']['attributes']['src'], $this->builder->getConfig()->get('assets.images.quality') ?? 75); |
||
| 216 | $srcset = ''; |
||
| 217 | if ($this->builder->getConfig()->get('body.images.responsive.enabled')) { |
||
| 218 | $srcset = Image::buildSrcset( |
||
| 219 | $assetWebp, |
||
| 220 | $this->builder->getConfig()->get('assets.images.responsive.widths') ?? [480, 640, 768, 1024, 1366, 1600, 1920] |
||
| 221 | ); |
||
| 222 | } |
||
| 223 | if (empty($srcset)) { |
||
| 224 | $srcset = (string) $assetWebp; |
||
| 225 | } |
||
| 226 | $PictureBlock = [ |
||
| 227 | 'element' => [ |
||
| 228 | 'name' => 'picture', |
||
| 229 | 'handler' => 'elements', |
||
| 230 | ], |
||
| 231 | ]; |
||
| 232 | $source = [ |
||
| 233 | 'element' => [ |
||
| 234 | 'name' => 'source', |
||
| 235 | 'attributes' => [ |
||
| 236 | 'type' => 'image/webp', |
||
| 237 | 'srcset' => $srcset, |
||
| 238 | 'sizes' => $this->builder->getConfig()->get('assets.images.responsive.sizes.default'), |
||
| 239 | ], |
||
| 240 | ], |
||
| 241 | ]; |
||
| 242 | $PictureBlock['element']['text'][] = $source['element']; |
||
| 243 | $PictureBlock['element']['text'][] = $InlineImage['element']; |
||
| 244 | $block = $PictureBlock; |
||
| 245 | } catch (RuntimeException $e) { |
||
| 246 | $this->builder->getLogger()->debug($e->getMessage()); |
||
| 247 | } |
||
| 248 | } |
||
| 249 | |||
| 250 | // put <img> (or <picture>) in a <figure> element if there is a title (<figcaption>) |
||
| 251 | if (!empty($InlineImage['element']['attributes']['title'])) { |
||
| 252 | $FigureBlock = [ |
||
| 253 | 'element' => [ |
||
| 254 | 'name' => 'figure', |
||
| 255 | 'handler' => 'elements', |
||
| 256 | 'text' => [ |
||
| 257 | $block['element'], |
||
| 258 | ], |
||
| 259 | ], |
||
| 260 | ]; |
||
| 261 | $InlineFigcaption = [ |
||
| 262 | 'element' => [ |
||
| 263 | 'name' => 'figcaption', |
||
| 264 | 'text' => $InlineImage['element']['attributes']['title'], |
||
| 265 | ], |
||
| 266 | ]; |
||
| 267 | $FigureBlock['element']['text'][] = $InlineFigcaption['element']; |
||
| 268 | |||
| 269 | return $FigureBlock; |
||
| 270 | } |
||
| 271 | |||
| 272 | return $block; |
||
| 273 | } |
||
| 331 |