Conditions | 15 |
Paths | 850 |
Total Lines | 53 |
Code Lines | 34 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 2 | 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 |
||
30 | protected function swapLazyLoadAttrs(string $loading, string $placeHolder, array $attrs): array |
||
31 | { |
||
32 | // Set the class and loading attributes |
||
33 | if (isset($attrs['class'])) { |
||
34 | $attrs['class'] = trim($attrs['class'] . ' lazyload'); |
||
35 | } |
||
36 | // Set the style on this element to be the placeholder image as the background-image |
||
37 | if (isset($attrs['style']) && !empty($attrs['src'])) { |
||
38 | if (empty($attrs['style'])) { |
||
39 | $attrs['style'] = []; |
||
40 | } |
||
41 | // If the original image is an SVG or gif, don't add the placeholder box CSS so that transparency works as intended |
||
42 | $path = parse_url($attrs['src'], PHP_URL_PATH); |
||
43 | $extension = pathinfo($path, PATHINFO_EXTENSION); |
||
44 | if ($extension !== 'svg' && $extension !== 'gif') { |
||
45 | $attrs['style']['background-image'] = 'url(' . $this->getLazyLoadSrc($placeHolder) . ')'; |
||
46 | $attrs['style']['background-size'] = 'cover'; |
||
47 | } |
||
48 | } |
||
49 | // Handle attributes that lazy and lazySizesFallback have in common |
||
50 | switch ($loading) { |
||
51 | case 'lazy': |
||
52 | case 'lazySizesFallback': |
||
53 | if (isset($attrs['loading'])) { |
||
54 | $attrs['loading'] = 'lazy'; |
||
55 | } |
||
56 | break; |
||
57 | default: |
||
58 | break; |
||
59 | } |
||
60 | // Handle attributes that lazySizes and lazySizesFallback have in common |
||
61 | switch ($loading) { |
||
62 | case 'lazySizes': |
||
63 | case 'lazySizesFallback': |
||
64 | // Only swap to data- attributes if they want the LazySizes fallback |
||
65 | if (!empty($attrs['sizes'])) { |
||
66 | $attrs['data-sizes'] = $attrs['sizes']; |
||
67 | $attrs['sizes'] = ''; |
||
68 | } |
||
69 | if (!empty($attrs['srcset'])) { |
||
70 | $attrs['data-srcset'] = $attrs['srcset']; |
||
71 | $attrs['srcset'] = ''; |
||
72 | } |
||
73 | if (!empty($attrs['src'])) { |
||
74 | $attrs['data-src'] = $attrs['src']; |
||
75 | $attrs['src'] = $this->getLazyLoadSrc($placeHolder); |
||
76 | } |
||
77 | break; |
||
78 | default: |
||
79 | break; |
||
80 | } |
||
81 | |||
82 | return $attrs; |
||
83 | } |
||
103 |