| Conditions | 28 |
| Paths | > 20000 |
| Total Lines | 174 |
| Code Lines | 88 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 7 | ||
| Bugs | 2 | Features | 1 |
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 |
||
| 206 | protected function inlineImage($Excerpt) |
||
| 207 | { |
||
| 208 | $InlineImage = parent::inlineImage($Excerpt); |
||
| 209 | if (!isset($InlineImage)) { |
||
| 210 | return null; |
||
| 211 | } |
||
| 212 | |||
| 213 | // remove quesry string |
||
| 214 | $InlineImage['element']['attributes']['src'] = $this->removeQueryString($InlineImage['element']['attributes']['src']); |
||
| 215 | |||
| 216 | // normalize path |
||
| 217 | $InlineImage['element']['attributes']['src'] = $this->normalizePath($InlineImage['element']['attributes']['src']); |
||
| 218 | |||
| 219 | // should be lazy loaded? |
||
| 220 | if ($this->builder->getConfig()->get('body.images.lazy.enabled') && !isset($InlineImage['element']['attributes']['loading'])) { |
||
| 221 | $InlineImage['element']['attributes']['loading'] = 'lazy'; |
||
| 222 | } |
||
| 223 | |||
| 224 | // add default class? |
||
| 225 | if ($this->builder->getConfig()->get('body.images.class')) { |
||
| 226 | $InlineImage['element']['attributes']['class'] .= ' '.$this->builder->getConfig()->get('body.images.class'); |
||
| 227 | $InlineImage['element']['attributes']['class'] = trim($InlineImage['element']['attributes']['class']); |
||
| 228 | } |
||
| 229 | |||
| 230 | // disable remote image handling? |
||
| 231 | if (Util\Url::isUrl($InlineImage['element']['attributes']['src']) && !$this->builder->getConfig()->get('body.images.remote.enabled') ?? true) { |
||
| 232 | return $InlineImage; |
||
| 233 | } |
||
| 234 | |||
| 235 | // create asset |
||
| 236 | $assetOptions = ['force_slash' => false]; |
||
| 237 | if ($this->builder->getConfig()->get('body.images.remote.fallback.enabled')) { |
||
| 238 | $assetOptions += ['remote_fallback' => $this->builder->getConfig()->get('body.images.remote.fallback.path')]; |
||
| 239 | } |
||
| 240 | $asset = new Asset($this->builder, $InlineImage['element']['attributes']['src'], $assetOptions); |
||
| 241 | $InlineImage['element']['attributes']['src'] = $asset; |
||
| 242 | $width = $asset->getWidth(); |
||
| 243 | |||
| 244 | /* |
||
| 245 | * Should be resized? |
||
| 246 | */ |
||
| 247 | $assetResized = null; |
||
| 248 | if (isset($InlineImage['element']['attributes']['width']) |
||
| 249 | && (int) $InlineImage['element']['attributes']['width'] < $width |
||
| 250 | && $this->builder->getConfig()->get('body.images.resize.enabled') |
||
| 251 | ) { |
||
| 252 | $width = (int) $InlineImage['element']['attributes']['width']; |
||
| 253 | |||
| 254 | try { |
||
| 255 | $assetResized = $asset->resize($width); |
||
| 256 | $InlineImage['element']['attributes']['src'] = $assetResized; |
||
| 257 | } catch (\Exception $e) { |
||
| 258 | $this->builder->getLogger()->debug($e->getMessage()); |
||
| 259 | |||
| 260 | return $InlineImage; |
||
| 261 | } |
||
| 262 | } |
||
| 263 | |||
| 264 | // set width |
||
| 265 | if (!isset($InlineImage['element']['attributes']['width'])) { |
||
| 266 | $InlineImage['element']['attributes']['width'] = $width; |
||
| 267 | } |
||
| 268 | // set height |
||
| 269 | if (!isset($InlineImage['element']['attributes']['height'])) { |
||
| 270 | $InlineImage['element']['attributes']['height'] = ($assetResized ?? $asset)->getHeight(); |
||
| 271 | } |
||
| 272 | |||
| 273 | /* |
||
| 274 | * Should be responsive? |
||
| 275 | */ |
||
| 276 | $sizes = ''; |
||
| 277 | if ($this->builder->getConfig()->get('body.images.responsive.enabled')) { |
||
| 278 | try { |
||
| 279 | if ($srcset = Image::buildSrcset( |
||
| 280 | $assetResized ?? $asset, |
||
| 281 | $this->builder->getConfig()->get('assets.images.responsive.widths') ?? [480, 640, 768, 1024, 1366, 1600, 1920] |
||
| 282 | )) { |
||
| 283 | $InlineImage['element']['attributes']['srcset'] = $srcset; |
||
| 284 | $sizes = (string) $this->builder->getConfig()->get('assets.images.responsive.sizes.default'); |
||
| 285 | if (isset($InlineImage['element']['attributes']['class'])) { |
||
| 286 | $sizes = Image::getSizes($InlineImage['element']['attributes']['class'], (array) $this->builder->getConfig()->get('assets.images.responsive.sizes')); |
||
| 287 | } |
||
| 288 | $InlineImage['element']['attributes']['sizes'] = $sizes; |
||
| 289 | } |
||
| 290 | } catch (\Exception $e) { |
||
| 291 | $this->builder->getLogger()->debug($e->getMessage()); |
||
| 292 | } |
||
| 293 | } |
||
| 294 | |||
| 295 | /* |
||
| 296 | <!-- if title: a <figure> is required to put in it a <figcaption> --> |
||
| 297 | <figure> |
||
| 298 | <!-- if WebP is enabled: a <picture> is required for the WebP <source> --> |
||
| 299 | <picture> |
||
| 300 | <source type="image/webp" |
||
| 301 | srcset="..." |
||
| 302 | sizes="..." |
||
| 303 | > |
||
| 304 | <img src="..." |
||
| 305 | srcset="..." |
||
| 306 | sizes="..." |
||
| 307 | > |
||
| 308 | </picture> |
||
| 309 | <figcaption><!-- title --></figcaption> |
||
| 310 | </figure> |
||
| 311 | */ |
||
| 312 | |||
| 313 | $image = $InlineImage; |
||
| 314 | |||
| 315 | // converts image to WebP and put it in picture > source |
||
| 316 | if ($this->builder->getConfig()->get('body.images.webp.enabled') ?? false |
||
| 317 | && (($InlineImage['element']['attributes']['src'])['type'] == 'image' |
||
| 318 | && ($InlineImage['element']['attributes']['src'])['subtype'] != 'image/webp') |
||
| 319 | ) { |
||
| 320 | try { |
||
| 321 | // InlineImage src must be an Asset instance |
||
| 322 | if (!$InlineImage['element']['attributes']['src'] instanceof Asset) { |
||
| 323 | throw new RuntimeException(\sprintf('Asset "%s" can\'t be converted to WebP', $InlineImage['element']['attributes']['src'])); |
||
| 324 | } |
||
| 325 | // abord if InlineImage is an animated GIF |
||
| 326 | if (Image::isAnimatedGif($InlineImage['element']['attributes']['src'])) { |
||
| 327 | throw new RuntimeException(\sprintf('Asset "%s" is an animated GIF and can\'t be converted to WebP', $InlineImage['element']['attributes']['src'])); |
||
| 328 | } |
||
| 329 | $assetWebp = Image::convertTopWebp($InlineImage['element']['attributes']['src'], $this->builder->getConfig()->get('assets.images.quality') ?? 75); |
||
| 330 | $srcset = ''; |
||
| 331 | // build responsives WebP? |
||
| 332 | if ($this->builder->getConfig()->get('body.images.responsive.enabled')) { |
||
| 333 | try { |
||
| 334 | $srcset = Image::buildSrcset( |
||
| 335 | $assetWebp, |
||
| 336 | $this->builder->getConfig()->get('assets.images.responsive.widths') ?? [480, 640, 768, 1024, 1366, 1600, 1920] |
||
| 337 | ); |
||
| 338 | } catch (\Exception $e) { |
||
| 339 | $this->builder->getLogger()->debug($e->getMessage()); |
||
| 340 | } |
||
| 341 | } |
||
| 342 | // if not, default image as srcset |
||
| 343 | if (empty($srcset)) { |
||
| 344 | $srcset = (string) $assetWebp; |
||
| 345 | } |
||
| 346 | $picture = [ |
||
| 347 | 'extent' => $InlineImage['extent'], |
||
| 348 | 'element' => [ |
||
| 349 | 'name' => 'picture', |
||
| 350 | 'handler' => 'elements', |
||
| 351 | 'attributes' => [ |
||
| 352 | 'title' => $image['element']['attributes']['title'], |
||
| 353 | ], |
||
| 354 | ], |
||
| 355 | ]; |
||
| 356 | $source = [ |
||
| 357 | 'element' => [ |
||
| 358 | 'name' => 'source', |
||
| 359 | 'attributes' => [ |
||
| 360 | 'type' => 'image/webp', |
||
| 361 | 'srcset' => $srcset, |
||
| 362 | 'sizes' => $sizes, |
||
| 363 | ], |
||
| 364 | ], |
||
| 365 | ]; |
||
| 366 | $picture['element']['text'][] = $source['element']; |
||
| 367 | $picture['element']['text'][] = $image['element']; |
||
| 368 | $image = $picture; |
||
| 369 | } catch (\Exception $e) { |
||
| 370 | $this->builder->getLogger()->debug($e->getMessage()); |
||
| 371 | } |
||
| 372 | } |
||
| 373 | |||
| 374 | // if title: put the <img> (or <picture>) in a <figure> and create a <figcaption> |
||
| 375 | if ($this->builder->getConfig()->get('body.images.caption.enabled')) { |
||
| 376 | return $this->createFigure($image); |
||
| 377 | } |
||
| 378 | |||
| 379 | return $image; |
||
| 380 | } |
||
| 631 |