Conditions | 14 |
Paths | 197 |
Total Lines | 102 |
Code Lines | 58 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
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 |
||
35 | protected function inlineImage($excerpt) |
||
36 | { |
||
37 | $image = parent::inlineImage($excerpt); |
||
38 | |||
39 | if (!isset($image)) { |
||
40 | return null; |
||
41 | } |
||
42 | |||
43 | $path = Util::joinFile( |
||
44 | $this->builder->getConfig()->getStaticTargetPath(), |
||
45 | ltrim($image['element']['attributes']['src']) |
||
46 | ); |
||
47 | if (Util::isExternalUrl($image['element']['attributes']['src'])) { |
||
48 | $path = $image['element']['attributes']['src']; |
||
49 | } |
||
50 | $width = $height = null; |
||
51 | list($width, $height, $type) = getimagesize($path); |
||
52 | |||
53 | // sets default attributes |
||
54 | $image['element']['attributes']['width'] = $width; |
||
55 | if ($type !== null) { |
||
56 | $image['element']['attributes']['loading'] = 'lazy'; |
||
57 | } |
||
58 | |||
59 | // captures query string. |
||
60 | // ie: "?resize=300&responsive" |
||
61 | $query = parse_url($image['element']['attributes']['src'], PHP_URL_QUERY); |
||
62 | if ($query === null) { |
||
63 | return $image; |
||
64 | } |
||
65 | parse_str($query, $result); |
||
66 | // cleans URL |
||
67 | $image['element']['attributes']['src'] = strtok($image['element']['attributes']['src'], '?'); |
||
68 | |||
69 | $path = Util::joinFile( |
||
70 | $this->builder->getConfig()->getStaticTargetPath(), |
||
71 | ltrim($image['element']['attributes']['src']) |
||
72 | ); |
||
73 | if (Util::isExternalUrl($image['element']['attributes']['src'])) { |
||
74 | $path = $image['element']['attributes']['src']; |
||
75 | } |
||
76 | $width = $height = null; |
||
77 | list($width, $height, $type) = getimagesize($path); |
||
78 | |||
79 | /** |
||
80 | * Should be responsive? |
||
81 | */ |
||
82 | $responsive = false; |
||
83 | if (array_key_exists('responsive', $result) && !Util::isExternalUrl($image['element']['attributes']['src'])) { |
||
84 | $responsive = true; |
||
85 | // process |
||
86 | $steps = 5; |
||
87 | $wMin = 320; |
||
88 | $wMax = 2560; |
||
89 | if ($width < $wMax) { |
||
90 | $wMax = $width; |
||
91 | } |
||
92 | $srcset = ''; |
||
93 | for ($i = 1; $i <= $steps; $i++) { |
||
94 | $w = (int) ceil($wMin + ($wMax - $wMin) / $steps * $i); |
||
95 | $img = (new Image($this->builder))->resize($image['element']['attributes']['src'], $w); |
||
96 | $srcset .= sprintf('%s %sw', $img, $w); |
||
97 | if ($i < $steps) { |
||
98 | $srcset .= ', '; |
||
99 | } |
||
100 | } |
||
101 | // ie: srcset="/img-480.jpg 480w, img-800.jpg 800w" |
||
102 | $image['element']['attributes']['srcset'] = $srcset; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Should be resized? |
||
107 | */ |
||
108 | if (array_key_exists('resize', $result)) { |
||
109 | $size = (int) $result['resize']; |
||
110 | $width = $size; |
||
111 | |||
112 | $image['element']['attributes']['src'] = (new Image($this->builder)) |
||
113 | ->resize($image['element']['attributes']['src'], $size); |
||
114 | |||
115 | $path = Util::joinPath( |
||
116 | $this->builder->getConfig()->getOutputPath(), |
||
117 | ltrim($image['element']['attributes']['src']) |
||
118 | ); |
||
119 | list($width) = getimagesize($path); |
||
120 | $image['element']['attributes']['width'] = $width; |
||
121 | } |
||
122 | |||
123 | // if responsive: set 'sizes' attribute |
||
124 | if ($responsive) { |
||
125 | // sizes="(max-width: 2800px) 100vw, 2800px" |
||
126 | $image['element']['attributes']['sizes'] = sprintf('(max-width: %spx) 100vw, %spx', $width, $width); |
||
127 | } |
||
128 | |||
129 | // set 'class' attribute |
||
130 | if (array_key_exists('class', $result)) { |
||
131 | $class = $result['class']; |
||
132 | $class = strtr($class, ',', ' '); |
||
133 | $image['element']['attributes']['class'] = $class; |
||
134 | } |
||
135 | |||
136 | return $image; |
||
137 | } |
||
139 |