Conditions | 11 |
Paths | 25 |
Total Lines | 55 |
Code Lines | 33 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | 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 |
||
33 | protected function inlineImage($excerpt) |
||
34 | { |
||
35 | $image = parent::inlineImage($excerpt); |
||
36 | if (!isset($image)) { |
||
37 | return null; |
||
38 | } |
||
39 | $image['element']['attributes']['src'] = trim($this->removeQuery($image['element']['attributes']['src'])); |
||
40 | $asset = new Asset($this->builder, $image['element']['attributes']['src']); |
||
41 | $width = $asset->getWidth(); |
||
42 | /** |
||
43 | * Should be lazy loaded? |
||
44 | */ |
||
45 | if ($this->builder->getConfig()->get('content.images.lazy.enabled')) { |
||
46 | $image['element']['attributes']['loading'] = 'lazy'; |
||
47 | } |
||
48 | /** |
||
49 | * Should be responsive? |
||
50 | */ |
||
51 | if ($this->builder->getConfig()->get('content.images.responsive.enabled')) { |
||
52 | $steps = $this->builder->getConfig()->get('content.images.responsive.width.steps'); |
||
53 | $wMin = $this->builder->getConfig()->get('content.images.responsive.width.min'); |
||
54 | $wMax = $this->builder->getConfig()->get('content.images.responsive.width.max'); |
||
55 | if ($width < $wMax) { |
||
56 | $wMax = $width; |
||
57 | } |
||
58 | $srcset = ''; |
||
59 | for ($i = 1; $i <= $steps; $i++) { |
||
60 | $w = (int) ceil($wMin + ($wMax - $wMin) / $steps * $i); |
||
61 | $a = new Asset($this->builder, $image['element']['attributes']['src']); |
||
62 | $img = $a->resize($w); |
||
63 | $srcset .= sprintf('%s %sw', $img, $w); |
||
64 | if ($i < $steps) { |
||
65 | $srcset .= ', '; |
||
66 | } |
||
67 | } |
||
68 | // ie: srcset="/img-480.jpg 480w, /img-800.jpg 800w" |
||
69 | $image['element']['attributes']['srcset'] = $srcset; |
||
70 | $image['element']['attributes']['sizes'] = $this->builder->getConfig()->get('content.images.responsive.sizes.default'); |
||
71 | } |
||
72 | /** |
||
73 | * Should be resized? |
||
74 | */ |
||
75 | if ($image['element']['attributes']['width'] !== null |
||
76 | && (int) $image['element']['attributes']['width'] < $width |
||
77 | && $this->builder->getConfig()->get('content.images.resize.enabled') |
||
78 | ) { |
||
79 | $width = (int) $image['element']['attributes']['width']; |
||
80 | $imageResized = $asset->resize($width); |
||
81 | $image['element']['attributes']['src'] = $imageResized; |
||
82 | } |
||
83 | if ($image['element']['attributes']['width'] === null) { |
||
84 | $image['element']['attributes']['width'] = $width; |
||
85 | } |
||
86 | |||
87 | return $image; |
||
88 | } |
||
132 |