Conditions | 18 |
Paths | 530 |
Total Lines | 145 |
Code Lines | 82 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
Bugs | 3 | 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 |
||
221 | protected function blockMedia($Excerpt) |
||
222 | { |
||
223 | if (1 !== preg_match($this->MarkdownMediaRegex, $Excerpt['text'])) { |
||
224 | return; |
||
225 | } |
||
226 | |||
227 | $InlineImage = $this->inlineImage($Excerpt); |
||
228 | if (!isset($InlineImage)) { |
||
229 | return; |
||
230 | } |
||
231 | |||
232 | // clean title (and preserve raw HTML) |
||
233 | $titleRawHtml = ''; |
||
234 | if (isset($InlineImage['element']['attributes']['title'])) { |
||
235 | $titleRawHtml = $this->line($InlineImage['element']['attributes']['title']); |
||
236 | $InlineImage['element']['attributes']['title'] = strip_tags($titleRawHtml); |
||
237 | } |
||
238 | |||
239 | $block = $InlineImage; |
||
240 | |||
241 | switch ($block['element']['attributes']['alt']) { |
||
242 | case 'audio': |
||
243 | $audio = []; |
||
244 | $audio['element'] = [ |
||
245 | 'name' => 'audio', |
||
246 | 'handler' => 'element', |
||
247 | ]; |
||
248 | $audio['element']['attributes'] = $block['element']['attributes']; |
||
249 | unset($audio['element']['attributes']['loading']); |
||
250 | $block = $audio; |
||
251 | unset($block['element']['attributes']['alt']); |
||
252 | break; |
||
253 | case 'video': |
||
254 | $video = []; |
||
255 | $video['element'] = [ |
||
256 | 'name' => 'video', |
||
257 | 'handler' => 'element', |
||
258 | ]; |
||
259 | $video['element']['attributes'] = $block['element']['attributes']; |
||
260 | unset($video['element']['attributes']['loading']); |
||
261 | if (isset($block['element']['attributes']['poster'])) { |
||
262 | $video['element']['attributes']['poster'] = new Asset($this->builder, $block['element']['attributes']['poster'], ['force_slash' => false]); |
||
263 | } |
||
264 | $block = $video; |
||
265 | unset($block['element']['attributes']['alt']); |
||
266 | } |
||
267 | |||
268 | /* |
||
269 | <!-- if image has a title: a <figure> is required for <figcaption> --> |
||
270 | <figure> |
||
271 | <!-- if WebP: a <picture> is required for <source> --> |
||
272 | <picture> |
||
273 | <source type="image/webp" |
||
274 | srcset="..." |
||
275 | sizes="..." |
||
276 | > |
||
277 | <img src="..." |
||
278 | srcset="..." |
||
279 | sizes="..." |
||
280 | > |
||
281 | </picture> |
||
282 | <!-- title --> |
||
283 | <figcaption>...</figcaption> |
||
284 | </figure> |
||
285 | */ |
||
286 | |||
287 | // creates a <picture> used to add WebP <source> in addition to the image <img> element |
||
288 | if ($this->builder->getConfig()->get('body.images.webp.enabled') ?? false |
||
289 | && (($InlineImage['element']['attributes']['src'])['type'] == 'image' |
||
290 | && ($InlineImage['element']['attributes']['src'])['subtype'] != 'image/webp') |
||
291 | ) { |
||
292 | try { |
||
293 | // Image src must be an Asset instance |
||
294 | if (is_string($InlineImage['element']['attributes']['src'])) { |
||
295 | throw new RuntimeException(\sprintf('Asset "%s" can\'t be converted to WebP', $InlineImage['element']['attributes']['src'])); |
||
296 | } |
||
297 | // Image asset is an animated GIF |
||
298 | if (Image::isAnimatedGif($InlineImage['element']['attributes']['src'])) { |
||
299 | throw new RuntimeException(\sprintf('Asset "%s" is an animated GIF and can\'t be converted to WebP', $InlineImage['element']['attributes']['src'])); |
||
300 | } |
||
301 | $assetWebp = Image::convertTopWebp($InlineImage['element']['attributes']['src'], $this->builder->getConfig()->get('assets.images.quality') ?? 75); |
||
302 | $srcset = ''; |
||
303 | // build responsives WebP? |
||
304 | if ($this->builder->getConfig()->get('body.images.responsive.enabled')) { |
||
305 | try { |
||
306 | $srcset = Image::buildSrcset( |
||
307 | $assetWebp, |
||
308 | $this->builder->getConfig()->get('assets.images.responsive.widths') ?? [480, 640, 768, 1024, 1366, 1600, 1920] |
||
309 | ); |
||
310 | } catch (\Exception $e) { |
||
311 | $this->builder->getLogger()->debug($e->getMessage()); |
||
312 | } |
||
313 | } |
||
314 | // if not, default image as srcset |
||
315 | if (empty($srcset)) { |
||
316 | $srcset = (string) $assetWebp; |
||
317 | } |
||
318 | $PictureBlock = [ |
||
319 | 'element' => [ |
||
320 | 'name' => 'picture', |
||
321 | 'handler' => 'elements', |
||
322 | ], |
||
323 | ]; |
||
324 | $source = [ |
||
325 | 'element' => [ |
||
326 | 'name' => 'source', |
||
327 | 'attributes' => [ |
||
328 | 'type' => 'image/webp', |
||
329 | 'srcset' => $srcset, |
||
330 | 'sizes' => $this->builder->getConfig()->get('assets.images.responsive.sizes.default'), |
||
331 | ], |
||
332 | ], |
||
333 | ]; |
||
334 | $PictureBlock['element']['text'][] = $source['element']; |
||
335 | $PictureBlock['element']['text'][] = $block['element']; |
||
336 | $block = $PictureBlock; |
||
337 | } catch (\Exception $e) { |
||
338 | $this->builder->getLogger()->debug($e->getMessage()); |
||
339 | } |
||
340 | } |
||
341 | |||
342 | // if there is a title: put the <img> (or <picture>) in a <figure> element to use the <figcaption> |
||
343 | if ($this->builder->getConfig()->get('body.images.caption.enabled') && !empty($titleRawHtml)) { |
||
344 | $FigureBlock = [ |
||
345 | 'element' => [ |
||
346 | 'name' => 'figure', |
||
347 | 'handler' => 'elements', |
||
348 | 'text' => [ |
||
349 | $block['element'], |
||
350 | ], |
||
351 | ], |
||
352 | ]; |
||
353 | $InlineFigcaption = [ |
||
354 | 'element' => [ |
||
355 | 'name' => 'figcaption', |
||
356 | 'allowRawHtmlInSafeMode' => true, |
||
357 | 'rawHtml' => $this->line($titleRawHtml), |
||
358 | ], |
||
359 | ]; |
||
360 | $FigureBlock['element']['text'][] = $InlineFigcaption['element']; |
||
361 | |||
362 | return $FigureBlock; |
||
363 | } |
||
364 | |||
365 | return $block; |
||
366 | } |
||
514 |