| Conditions | 41 |
| Paths | > 20000 |
| Total Lines | 226 |
| Code Lines | 124 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 8 | ||
| Bugs | 6 | 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 |
||
| 238 | protected function inlineImage($Excerpt) |
||
| 239 | { |
||
| 240 | $InlineImage = parent::inlineImage($Excerpt); // @phpstan-ignore staticMethod.notFound |
||
| 241 | if (!isset($InlineImage)) { |
||
| 242 | return null; |
||
| 243 | } |
||
| 244 | |||
| 245 | // remove link attributes |
||
| 246 | unset($InlineImage['element']['attributes']['target'], $InlineImage['element']['attributes']['rel']); |
||
| 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 $this->createFigure($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 $this->createFigure($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::buildHtmlSrcsetW( |
||
| 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()->warning($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 | // abord if InlineImage is an animated GIF |
||
| 409 | if (Image::isAnimatedGif($assetResized ?? $asset)) { |
||
| 410 | $filepath = Util::joinFile($this->config->getOutputPath(), $assetResized['path'] ?? $asset['path'] ?? ''); |
||
| 411 | throw new RuntimeException(\sprintf('Asset "%s" is not converted (animated GIF).', $filepath)); |
||
| 412 | } |
||
| 413 | $sources = []; |
||
| 414 | foreach ($formats as $format) { |
||
| 415 | $srcset = ''; |
||
| 416 | try { |
||
| 417 | $assetConverted = ($assetResized ?? $asset)->convert($format); |
||
| 418 | // build responsive images? |
||
| 419 | if ($this->config->isEnabled('pages.body.images.responsive')) { |
||
| 420 | $srcset = Image::buildHtmlSrcset($assetConverted, $this->config->getAssetsImagesWidths()); |
||
| 421 | } |
||
| 422 | // if not, use default image as srcset |
||
| 423 | if (empty($srcset)) { |
||
| 424 | $srcset = (string) $assetConverted; |
||
| 425 | } |
||
| 426 | // add format to <sources> |
||
| 427 | $sources[] = [ |
||
| 428 | 'name' => 'source', |
||
| 429 | 'attributes' => [ |
||
| 430 | 'type' => "image/$format", |
||
| 431 | 'srcset' => $srcset, |
||
| 432 | 'sizes' => $sizes, |
||
| 433 | 'width' => $InlineImage['element']['attributes']['width'], |
||
| 434 | 'height' => $InlineImage['element']['attributes']['height'], |
||
| 435 | ], |
||
| 436 | ]; |
||
| 437 | } catch (\Exception $e) { |
||
| 438 | $this->builder->getLogger()->warning($e->getMessage()); |
||
| 439 | continue; |
||
| 440 | } |
||
| 441 | } |
||
| 442 | if (\count($sources) > 0) { |
||
| 443 | $picture = [ |
||
| 444 | 'extent' => $InlineImage['extent'], |
||
| 445 | 'element' => [ |
||
| 446 | 'name' => 'picture', |
||
| 447 | 'handler' => 'elements', |
||
| 448 | 'attributes' => [ |
||
| 449 | 'title' => $image['element']['attributes']['title'], |
||
| 450 | ], |
||
| 451 | ], |
||
| 452 | ]; |
||
| 453 | $picture['element']['text'] = $sources; |
||
| 454 | unset($image['element']['attributes']['title']); // phpstan-ignore unset.offset |
||
| 455 | $picture['element']['text'][] = $image['element']; |
||
| 456 | $image = $picture; |
||
| 457 | } |
||
| 458 | } catch (\Exception $e) { |
||
| 459 | $this->builder->getLogger()->debug($e->getMessage()); |
||
| 460 | } |
||
| 461 | } |
||
| 462 | |||
| 463 | return $this->createFigure($image); |
||
| 464 | } |
||
| 802 |