Conditions | 15 |
Paths | 52 |
Total Lines | 74 |
Code Lines | 45 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 1 | 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 |
||
39 | protected function inlineImage($excerpt) |
||
40 | { |
||
41 | $image = parent::inlineImage($excerpt); |
||
42 | if (!isset($image)) { |
||
43 | return null; |
||
44 | } |
||
45 | $image['element']['attributes']['src'] = $imageSource = trim($this->removeQuery($image['element']['attributes']['src'])); |
||
46 | $asset = new Asset($this->builder, $imageSource); |
||
47 | $width = $asset->getWidth(); |
||
48 | if ($width === false) { |
||
49 | return $image; |
||
50 | } |
||
51 | $image['element']['attributes']['src'] = $asset; |
||
52 | /** |
||
53 | * Should be lazy loaded? |
||
54 | */ |
||
55 | if ($this->builder->getConfig()->get('body.images.lazy.enabled')) { |
||
56 | $image['element']['attributes']['loading'] = 'lazy'; |
||
57 | } |
||
58 | /** |
||
59 | * Should be resized? |
||
60 | */ |
||
61 | $imageResized = null; |
||
62 | if (array_key_exists('width', $image['element']['attributes']) |
||
63 | && (int) $image['element']['attributes']['width'] < $width |
||
64 | && $this->builder->getConfig()->get('body.images.resize.enabled') |
||
65 | ) { |
||
66 | $width = (int) $image['element']['attributes']['width']; |
||
67 | |||
68 | try { |
||
69 | $imageResized = $asset->resize($width); |
||
70 | } catch (\Exception $e) { |
||
71 | $this->builder->getLogger()->debug($e->getMessage()); |
||
72 | |||
73 | return $image; |
||
74 | } |
||
75 | $image['element']['attributes']['src'] = $imageResized; |
||
76 | } |
||
77 | // set width |
||
78 | if (!array_key_exists('width', $image['element']['attributes'])) { |
||
79 | $image['element']['attributes']['width'] = $width; |
||
80 | } |
||
81 | // set height |
||
82 | if (!array_key_exists('height', $image['element']['attributes'])) { |
||
83 | $image['element']['attributes']['height'] = $asset->getHeight(); |
||
84 | } |
||
85 | /** |
||
86 | * Should be responsive? |
||
87 | */ |
||
88 | if ($this->builder->getConfig()->get('body.images.responsive.enabled')) { |
||
89 | $steps = $this->builder->getConfig()->get('body.images.responsive.width.steps'); |
||
90 | $wMin = $this->builder->getConfig()->get('body.images.responsive.width.min'); |
||
91 | $wMax = $this->builder->getConfig()->get('body.images.responsive.width.max'); |
||
92 | $srcset = ''; |
||
93 | for ($i = 1; $i <= $steps; $i++) { |
||
94 | $w = ceil($wMin * $i); |
||
95 | if ($w > $width || $w > $wMax) { |
||
96 | break; |
||
97 | } |
||
98 | $a = new Asset($this->builder, $imageSource); |
||
99 | $img = $a->resize(intval($w)); |
||
100 | $srcset .= sprintf('%s %sw', $img, $w); |
||
101 | if ($i < $steps) { |
||
102 | $srcset .= ', '; |
||
103 | } |
||
104 | } |
||
105 | $imageDefault = $imageResized ?? $asset; |
||
106 | $srcset .= sprintf('%s %sw', $imageDefault, $width); |
||
107 | // ie: srcset="/img-480.jpg 480w, /img-800.jpg 800w" |
||
108 | $image['element']['attributes']['srcset'] = $srcset; |
||
109 | $image['element']['attributes']['sizes'] = $this->builder->getConfig()->get('body.images.responsive.sizes.default'); |
||
110 | } |
||
111 | |||
112 | return $image; |
||
113 | } |
||
192 |