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