| Conditions | 12 |
| Paths | 340 |
| Total Lines | 47 |
| Code Lines | 30 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 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 | $attrs['style'] = trim( |
||
| 39 | $attrs['style'] . |
||
| 40 | 'background-image:url(' . $this->getLazyLoadSrc($placeHolder) . '); background-size: cover;' |
||
| 41 | ); |
||
| 42 | } |
||
| 43 | // Handle attributes that lazy and lazySizesFallback have in common |
||
| 44 | switch ($loading) { |
||
| 45 | case 'lazy': |
||
| 46 | case 'lazySizesFallback': |
||
| 47 | if (isset($attrs['loading'])) { |
||
| 48 | $attrs['loading'] = 'lazy'; |
||
| 49 | } |
||
| 50 | break; |
||
| 51 | default: |
||
| 52 | break; |
||
| 53 | } |
||
| 54 | // Handle attributes that lazySizes and lazySizesFallback have in common |
||
| 55 | switch ($loading) { |
||
| 56 | case 'lazySizes': |
||
| 57 | case 'lazySizesFallback': |
||
| 58 | // Only swap to data- attributes if they want the LazySizes fallback |
||
| 59 | if (!empty($attrs['sizes'])) { |
||
| 60 | $attrs['data-sizes'] = $attrs['sizes']; |
||
| 61 | $attrs['sizes'] = ''; |
||
| 62 | } |
||
| 63 | if (!empty($attrs['srcset'])) { |
||
| 64 | $attrs['data-srcset'] = $attrs['srcset']; |
||
| 65 | $attrs['srcset'] = ''; |
||
| 66 | } |
||
| 67 | if (!empty($attrs['src'])) { |
||
| 68 | $attrs['data-src'] = $attrs['src']; |
||
| 69 | $attrs['src'] = $this->getLazyLoadSrc($placeHolder); |
||
| 70 | } |
||
| 71 | break; |
||
| 72 | default: |
||
| 73 | break; |
||
| 74 | } |
||
| 75 | |||
| 76 | return $attrs; |
||
| 77 | } |
||
| 97 |