| Conditions | 14 |
| Paths | 850 |
| Total Lines | 51 |
| Code Lines | 32 |
| 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 | // Don't add the background placeholder if it is set to 'none' |
||
| 42 | if ($placeHolder !== 'none') { |
||
| 43 | $attrs['style']['background-image'] = 'url(' . $this->getLazyLoadSrc($placeHolder) . ')'; |
||
| 44 | $attrs['style']['background-size'] = 'cover'; |
||
| 45 | } |
||
| 46 | } |
||
| 47 | // Handle attributes that lazy and lazySizesFallback have in common |
||
| 48 | switch ($loading) { |
||
| 49 | case 'lazy': |
||
| 50 | case 'lazySizesFallback': |
||
| 51 | if (isset($attrs['loading'])) { |
||
| 52 | $attrs['loading'] = 'lazy'; |
||
| 53 | } |
||
| 54 | break; |
||
| 55 | default: |
||
| 56 | break; |
||
| 57 | } |
||
| 58 | // Handle attributes that lazySizes and lazySizesFallback have in common |
||
| 59 | switch ($loading) { |
||
| 60 | case 'lazySizes': |
||
| 61 | case 'lazySizesFallback': |
||
| 62 | // Only swap to data- attributes if they want the LazySizes fallback |
||
| 63 | if (!empty($attrs['sizes'])) { |
||
| 64 | $attrs['data-sizes'] = $attrs['sizes']; |
||
| 65 | $attrs['sizes'] = ''; |
||
| 66 | } |
||
| 67 | if (!empty($attrs['srcset'])) { |
||
| 68 | $attrs['data-srcset'] = $attrs['srcset']; |
||
| 69 | $attrs['srcset'] = ''; |
||
| 70 | } |
||
| 71 | if (!empty($attrs['src'])) { |
||
| 72 | $attrs['data-src'] = $attrs['src']; |
||
| 73 | $attrs['src'] = $this->getLazyLoadSrc($placeHolder); |
||
| 74 | } |
||
| 75 | break; |
||
| 76 | default: |
||
| 77 | break; |
||
| 78 | } |
||
| 79 | |||
| 80 | return $attrs; |
||
| 81 | } |
||
| 101 |