Conditions | 14 |
Paths | 3 |
Total Lines | 114 |
Code Lines | 59 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
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 |
||
162 | protected function blockMedia($Excerpt) |
||
163 | { |
||
164 | if (1 !== preg_match($this->MarkdownMediaRegex, $Excerpt['text'], $matches)) { |
||
165 | return; |
||
166 | } |
||
167 | |||
168 | switch ($matches[1]) { |
||
169 | case 'audio': |
||
170 | dump($matches); |
||
171 | |||
172 | return; |
||
173 | break; |
||
|
|||
174 | case 'video': |
||
175 | dump($matches); |
||
176 | |||
177 | return; |
||
178 | break; |
||
179 | } |
||
180 | |||
181 | $InlineImage = $this->inlineImage($Excerpt); |
||
182 | if (!isset($InlineImage)) { |
||
183 | return; |
||
184 | } |
||
185 | |||
186 | $block = $InlineImage; |
||
187 | |||
188 | /* |
||
189 | <!-- if image has a title: a <figure> is required for <figcaption> --> |
||
190 | <figure> |
||
191 | <!-- if WebP: a <picture> is required for <source> --> |
||
192 | <picture> |
||
193 | <source type="image/webp" |
||
194 | srcset="..." |
||
195 | sizes="..." |
||
196 | > |
||
197 | <img src="..." |
||
198 | srcset="..." |
||
199 | sizes="..." |
||
200 | > |
||
201 | </picture> |
||
202 | <!-- title --> |
||
203 | <figcaption>...</figcaption> |
||
204 | </figure> |
||
205 | */ |
||
206 | |||
207 | // creates a <picture> used to add WebP <source> in addition to the image <img> element |
||
208 | if ($this->builder->getConfig()->get('body.images.webp.enabled') ?? false && ($InlineImage['element']['attributes']['src'])['subtype'] != 'image/webp') { |
||
209 | try { |
||
210 | // Image src must be an Asset instance |
||
211 | if (is_string($InlineImage['element']['attributes']['src'])) { |
||
212 | throw new RuntimeException(\sprintf('Asset "%s" can\'t be converted to WebP', $InlineImage['element']['attributes']['src'])); |
||
213 | } |
||
214 | // Image asset is an animated GIF |
||
215 | if (Image::isAnimatedGif($InlineImage['element']['attributes']['src'])) { |
||
216 | throw new RuntimeException(\sprintf('Asset "%s" is an animated GIF and can\'t be converted to WebP', $InlineImage['element']['attributes']['src'])); |
||
217 | } |
||
218 | $assetWebp = Image::convertTopWebp($InlineImage['element']['attributes']['src'], $this->builder->getConfig()->get('assets.images.quality') ?? 75); |
||
219 | $srcset = ''; |
||
220 | if ($this->builder->getConfig()->get('body.images.responsive.enabled')) { |
||
221 | $srcset = Image::buildSrcset( |
||
222 | $assetWebp, |
||
223 | $this->builder->getConfig()->get('assets.images.responsive.widths') ?? [480, 640, 768, 1024, 1366, 1600, 1920] |
||
224 | ); |
||
225 | } |
||
226 | if (empty($srcset)) { |
||
227 | $srcset = (string) $assetWebp; |
||
228 | } |
||
229 | $PictureBlock = [ |
||
230 | 'element' => [ |
||
231 | 'name' => 'picture', |
||
232 | 'handler' => 'elements', |
||
233 | ], |
||
234 | ]; |
||
235 | $source = [ |
||
236 | 'element' => [ |
||
237 | 'name' => 'source', |
||
238 | 'attributes' => [ |
||
239 | 'type' => 'image/webp', |
||
240 | 'srcset' => $srcset, |
||
241 | 'sizes' => $this->builder->getConfig()->get('assets.images.responsive.sizes.default'), |
||
242 | ], |
||
243 | ], |
||
244 | ]; |
||
245 | $PictureBlock['element']['text'][] = $source['element']; |
||
246 | $PictureBlock['element']['text'][] = $InlineImage['element']; |
||
247 | $block = $PictureBlock; |
||
248 | } catch (\Exception $e) { |
||
249 | $this->builder->getLogger()->debug($e->getMessage()); |
||
250 | } |
||
251 | } |
||
252 | |||
253 | // if there is a title: put the <img> (or <picture>) in a <figure> element to use the <figcaption> |
||
254 | if ($this->builder->getConfig()->get('body.images.caption.enabled') && !empty($InlineImage['element']['attributes']['title'])) { |
||
255 | $FigureBlock = [ |
||
256 | 'element' => [ |
||
257 | 'name' => 'figure', |
||
258 | 'handler' => 'elements', |
||
259 | 'text' => [ |
||
260 | $block['element'], |
||
261 | ], |
||
262 | ], |
||
263 | ]; |
||
264 | $InlineFigcaption = [ |
||
265 | 'element' => [ |
||
266 | 'name' => 'figcaption', |
||
267 | 'text' => $InlineImage['element']['attributes']['title'], |
||
268 | ], |
||
269 | ]; |
||
270 | $FigureBlock['element']['text'][] = $InlineFigcaption['element']; |
||
271 | |||
272 | return $FigureBlock; |
||
273 | } |
||
274 | |||
275 | return $block; |
||
276 | } |
||
396 |
The
break
statement is not necessary if it is preceded for example by areturn
statement:If you would like to keep this construct to be consistent with other
case
statements, you can safely mark this issue as a false-positive.