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