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