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