Conditions | 44 |
Paths | > 20000 |
Total Lines | 237 |
Code Lines | 131 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 1 | 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 |
||
269 | protected function inlineImage($Excerpt) |
||
270 | { |
||
271 | $InlineImage = parent::inlineImage($Excerpt); // @phpstan-ignore staticMethod.notFound |
||
272 | if (!isset($InlineImage)) { |
||
273 | return null; |
||
274 | } |
||
275 | |||
276 | // normalize path |
||
277 | $InlineImage['element']['attributes']['src'] = $this->normalizePath($InlineImage['element']['attributes']['src']); |
||
278 | |||
279 | // should be lazy loaded? |
||
280 | if ($this->config->isEnabled('pages.body.images.lazy') && !isset($InlineImage['element']['attributes']['loading'])) { |
||
281 | $InlineImage['element']['attributes']['loading'] = 'lazy'; |
||
282 | } |
||
283 | // should be decoding async? |
||
284 | if ($this->config->isEnabled('pages.body.images.decoding') && !isset($InlineImage['element']['attributes']['decoding'])) { |
||
285 | $InlineImage['element']['attributes']['decoding'] = 'async'; |
||
286 | } |
||
287 | // add default class? |
||
288 | if ((string) $this->config->get('pages.body.images.class')) { |
||
289 | if (!\array_key_exists('class', $InlineImage['element']['attributes'])) { |
||
290 | $InlineImage['element']['attributes']['class'] = ''; |
||
291 | } |
||
292 | $InlineImage['element']['attributes']['class'] .= ' ' . (string) $this->config->get('pages.body.images.class'); |
||
293 | $InlineImage['element']['attributes']['class'] = trim($InlineImage['element']['attributes']['class']); |
||
294 | } |
||
295 | |||
296 | // disable remote image handling? |
||
297 | if (Util\File::isRemote($InlineImage['element']['attributes']['src']) && !$this->config->isEnabled('pages.body.images.remote')) { |
||
298 | return $InlineImage; |
||
299 | } |
||
300 | |||
301 | // create asset |
||
302 | $assetOptions = ['leading_slash' => false]; |
||
303 | if ($this->config->isEnabled('pages.body.images.remote.fallback')) { |
||
304 | $assetOptions = ['leading_slash' => true]; |
||
305 | $assetOptions += ['fallback' => (string) $this->config->get('pages.body.images.remote.fallback')]; |
||
306 | } |
||
307 | $asset = new Asset($this->builder, $InlineImage['element']['attributes']['src'], $assetOptions); |
||
308 | $InlineImage['element']['attributes']['src'] = $asset; |
||
309 | $width = $asset['width']; |
||
310 | |||
311 | /* |
||
312 | * Should be resized? |
||
313 | */ |
||
314 | $shouldResize = false; |
||
315 | $assetResized = null; |
||
316 | // pages.body.images.resize |
||
317 | if ( |
||
318 | \is_int($this->config->get('pages.body.images.resize')) |
||
319 | && $this->config->get('pages.body.images.resize') > 0 |
||
320 | && $width > $this->config->get('pages.body.images.resize') |
||
321 | ) { |
||
322 | $shouldResize = true; |
||
323 | $width = $this->config->get('pages.body.images.resize'); |
||
324 | } |
||
325 | // width attribute |
||
326 | if ( |
||
327 | isset($InlineImage['element']['attributes']['width']) |
||
328 | && $width > (int) $InlineImage['element']['attributes']['width'] |
||
329 | ) { |
||
330 | $shouldResize = true; |
||
331 | $width = (int) $InlineImage['element']['attributes']['width']; |
||
332 | } |
||
333 | // responsive images |
||
334 | if ( |
||
335 | $this->config->isEnabled('pages.body.images.responsive') |
||
336 | && !empty($this->config->getAssetsImagesWidths()) |
||
337 | && $width > max($this->config->getAssetsImagesWidths()) |
||
338 | ) { |
||
339 | $shouldResize = true; |
||
340 | $width = max($this->config->getAssetsImagesWidths()); |
||
341 | } |
||
342 | if ($shouldResize) { |
||
343 | try { |
||
344 | $assetResized = $asset->resize($width); |
||
345 | $InlineImage['element']['attributes']['src'] = $assetResized; |
||
346 | } catch (\Exception $e) { |
||
347 | $this->builder->getLogger()->debug($e->getMessage()); |
||
348 | |||
349 | return $InlineImage; |
||
350 | } |
||
351 | } |
||
352 | |||
353 | // set width |
||
354 | $InlineImage['element']['attributes']['width'] = $width; |
||
355 | // set height |
||
356 | $InlineImage['element']['attributes']['height'] = $assetResized['height'] ?? $asset['height']; |
||
357 | |||
358 | // placeholder |
||
359 | if ( |
||
360 | (!empty($this->config->get('pages.body.images.placeholder')) || isset($InlineImage['element']['attributes']['placeholder'])) |
||
361 | && \in_array($InlineImage['element']['attributes']['src']['subtype'], ['image/jpeg', 'image/png', 'image/gif']) |
||
362 | ) { |
||
363 | if (!\array_key_exists('placeholder', $InlineImage['element']['attributes'])) { |
||
364 | $InlineImage['element']['attributes']['placeholder'] = (string) $this->config->get('pages.body.images.placeholder'); |
||
365 | } |
||
366 | if (!\array_key_exists('style', $InlineImage['element']['attributes'])) { |
||
367 | $InlineImage['element']['attributes']['style'] = ''; |
||
368 | } |
||
369 | $InlineImage['element']['attributes']['style'] = trim($InlineImage['element']['attributes']['style'], ';'); |
||
370 | switch ($InlineImage['element']['attributes']['placeholder']) { |
||
371 | case 'color': |
||
372 | $InlineImage['element']['attributes']['style'] .= \sprintf(';max-width:100%%;height:auto;background-color:%s;', Image::getDominantColor($InlineImage['element']['attributes']['src'])); |
||
373 | break; |
||
374 | case 'lqip': |
||
375 | // aborts if animated GIF for performance reasons |
||
376 | if (Image::isAnimatedGif($InlineImage['element']['attributes']['src'])) { |
||
377 | break; |
||
378 | } |
||
379 | $InlineImage['element']['attributes']['style'] .= \sprintf(';max-width:100%%;height:auto;background-image:url(%s);background-repeat:no-repeat;background-position:center;background-size:cover;', Image::getLqip($InlineImage['element']['attributes']['src'])); |
||
380 | break; |
||
381 | } |
||
382 | unset($InlineImage['element']['attributes']['placeholder']); |
||
383 | $InlineImage['element']['attributes']['style'] = trim($InlineImage['element']['attributes']['style']); |
||
384 | } |
||
385 | |||
386 | /* |
||
387 | * Should be responsive? |
||
388 | */ |
||
389 | $sizes = ''; |
||
390 | if ($this->config->isEnabled('pages.body.images.responsive')) { |
||
391 | try { |
||
392 | if ( |
||
393 | $srcset = Image::buildSrcset( |
||
394 | $assetResized ?? $asset, |
||
395 | $this->config->getAssetsImagesWidths() |
||
396 | ) |
||
397 | ) { |
||
398 | $InlineImage['element']['attributes']['srcset'] = $srcset; |
||
399 | $sizes = Image::getSizes($InlineImage['element']['attributes']['class'] ?? '', (array) $this->config->getAssetsImagesSizes()); |
||
400 | $InlineImage['element']['attributes']['sizes'] = $sizes; |
||
401 | } |
||
402 | } catch (\Exception $e) { |
||
403 | $this->builder->getLogger()->debug($e->getMessage()); |
||
404 | } |
||
405 | } |
||
406 | |||
407 | /* |
||
408 | <!-- if title: a <figure> is required to put in it a <figcaption> --> |
||
409 | <figure> |
||
410 | <!-- if formats: a <picture> is required for each <source> --> |
||
411 | <picture> |
||
412 | <source type="image/avif" |
||
413 | srcset="..." |
||
414 | sizes="..." |
||
415 | > |
||
416 | <source type="image/webp" |
||
417 | srcset="..." |
||
418 | sizes="..." |
||
419 | > |
||
420 | <img src="..." |
||
421 | srcset="..." |
||
422 | sizes="..." |
||
423 | > |
||
424 | </picture> |
||
425 | <figcaption><!-- title --></figcaption> |
||
426 | </figure> |
||
427 | */ |
||
428 | |||
429 | $image = $InlineImage; |
||
430 | |||
431 | // converts image to formats and put them in picture > source |
||
432 | if ( |
||
433 | \count($formats = ((array) $this->config->get('pages.body.images.formats'))) > 0 |
||
434 | && \in_array($InlineImage['element']['attributes']['src']['subtype'], ['image/jpeg', 'image/png', 'image/gif']) |
||
435 | ) { |
||
436 | try { |
||
437 | // InlineImage src must be an Asset instance |
||
438 | if (!$InlineImage['element']['attributes']['src'] instanceof Asset) { |
||
439 | throw new RuntimeException(\sprintf('Asset "%s" can\'t be converted.', $InlineImage['element']['attributes']['src'])); |
||
440 | } |
||
441 | // abord if InlineImage is an animated GIF |
||
442 | if (Image::isAnimatedGif($InlineImage['element']['attributes']['src'])) { |
||
443 | $filepath = Util::joinFile($this->config->getOutputPath(), $InlineImage['element']['attributes']['src']['path']); |
||
444 | throw new RuntimeException(\sprintf('Asset "%s" is not converted (animated GIF).', $filepath)); |
||
445 | } |
||
446 | $sources = []; |
||
447 | foreach ($formats as $format) { |
||
448 | $srcset = ''; |
||
449 | try { |
||
450 | $assetConverted = $InlineImage['element']['attributes']['src']->convert($format); |
||
451 | } catch (\Exception $e) { |
||
452 | $this->builder->getLogger()->debug($e->getMessage()); |
||
453 | continue; |
||
454 | } |
||
455 | // build responsive images? |
||
456 | if ($this->config->isEnabled('pages.body.images.responsive')) { |
||
457 | try { |
||
458 | $srcset = Image::buildSrcset($assetConverted, $this->config->getAssetsImagesWidths()); |
||
459 | } catch (\Exception $e) { |
||
460 | $this->builder->getLogger()->debug($e->getMessage()); |
||
461 | } |
||
462 | } |
||
463 | // if not, use default image as srcset |
||
464 | if (empty($srcset)) { |
||
465 | $srcset = (string) $assetConverted; |
||
466 | } |
||
467 | // add format to <sources> |
||
468 | $sources[] = [ |
||
469 | 'name' => 'source', |
||
470 | 'attributes' => [ |
||
471 | 'type' => "image/$format", |
||
472 | 'srcset' => $srcset, |
||
473 | 'sizes' => $sizes, |
||
474 | 'width' => $InlineImage['element']['attributes']['width'], |
||
475 | 'height' => $InlineImage['element']['attributes']['height'], |
||
476 | ], |
||
477 | ]; |
||
478 | } |
||
479 | if (\count($sources) > 0) { |
||
480 | $picture = [ |
||
481 | 'extent' => $InlineImage['extent'], |
||
482 | 'element' => [ |
||
483 | 'name' => 'picture', |
||
484 | 'handler' => 'elements', |
||
485 | 'attributes' => [ |
||
486 | 'title' => $image['element']['attributes']['title'], |
||
487 | ], |
||
488 | ], |
||
489 | ]; |
||
490 | $picture['element']['text'] = $sources; |
||
491 | unset($image['element']['attributes']['title']); // @phpstan-ignore unset.offset |
||
492 | $picture['element']['text'][] = $image['element']; |
||
493 | $image = $picture; |
||
494 | } |
||
495 | } catch (\Exception $e) { |
||
496 | $this->builder->getLogger()->debug($e->getMessage()); |
||
497 | } |
||
498 | } |
||
499 | |||
500 | // if title: put the <img> (or <picture>) in a <figure> and create a <figcaption> |
||
501 | if ($this->config->isEnabled('pages.body.images.caption')) { |
||
502 | return $this->createFigure($image); |
||
503 | } |
||
504 | |||
505 | return $image; |
||
506 | } |
||
799 |