BaseImageTag::swapLazyLoadAttrs()   F
last analyzed

Complexity

Conditions 15
Paths 850

Size

Total Lines 53
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 2 Features 0
Metric Value
eloc 34
c 3
b 2
f 0
dl 0
loc 53
rs 1.9583
cc 15
nc 850
nop 3

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
/**
3
 * Image Optimize plugin for Craft CMS
4
 *
5
 * Automatically optimize images after they've been transformed
6
 *
7
 * @link      https://nystudio107.com
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @copyright tag
Loading history...
8
 * @copyright Copyright (c) nystudio107
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
9
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
10
11
namespace nystudio107\imageoptimize\models;
12
13
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
14
 * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Coding Style introduced by
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
15
 * @package   ImageOptimize
0 ignored issues
show
Coding Style introduced by
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
16
 * @since     5.0.0-beta.1
0 ignored issues
show
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
Coding Style introduced by
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
17
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
18
abstract class BaseImageTag extends BaseTag
19
{
20
    /**
21
     * Swap the tag attributes to work with lazy loading
22
     * ref: https://web.dev/native-lazy-loading/#how-do-i-handle-browsers-that-don't-yet-support-native-lazy-loading
23
     *
24
     * @param string $loading 'eager', 'lazy', 'lazySizes', 'lazySizesFallback'
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter name; 1 found
Loading history...
25
     * @param string $placeHolder 'box', 'color', 'image', 'silhouette'
26
     * @param array $attrs
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
27
     *
28
     * @return array
29
     */
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':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
52
            case 'lazySizesFallback':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
53
                if (isset($attrs['loading'])) {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
54
                    $attrs['loading'] = 'lazy';
55
                }
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
56
                break;
57
            default:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
58
                break;
59
        }
60
        // Handle attributes that lazySizes and lazySizesFallback have in common
61
        switch ($loading) {
62
            case 'lazySizes':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
63
            case 'lazySizesFallback':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
64
                // Only swap to data- attributes if they want the LazySizes fallback
65
                if (!empty($attrs['sizes'])) {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
66
                    $attrs['data-sizes'] = $attrs['sizes'];
67
                    $attrs['sizes'] = '';
68
                }
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
69
                if (!empty($attrs['srcset'])) {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
70
                    $attrs['data-srcset'] = $attrs['srcset'];
71
                    $attrs['srcset'] = '';
72
                }
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
73
                if (!empty($attrs['src'])) {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
74
                    $attrs['data-src'] = $attrs['src'];
75
                    $attrs['src'] = $this->getLazyLoadSrc($placeHolder);
76
                }
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
77
                break;
78
            default:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
79
                break;
80
        }
81
82
        return $attrs;
83
    }
84
85
    /**
86
     * Return a lazy loading placeholder image based on the passed in $lazyload setting
87
     *
88
     * @param string $lazyLoad
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
89
     *
90
     * @return string
91
     */
92
    protected function getLazyLoadSrc(string $lazyLoad): string
93
    {
94
        $lazyLoad = strtolower($lazyLoad);
95
        return match ($lazyLoad) {
96
            'image' => $this->optimizedImage->getPlaceholderImage(),
0 ignored issues
show
Bug introduced by
The method getPlaceholderImage() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

96
            'image' => $this->optimizedImage->/** @scrutinizer ignore-call */ getPlaceholderImage(),

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
97
            'silhouette' => $this->optimizedImage->getPlaceholderSilhouette(),
98
            'color' => $this->optimizedImage->getPlaceholderBox($this->colorPalette[0] ?? null),
0 ignored issues
show
Bug Best Practice introduced by
The property colorPalette does not exist on nystudio107\imageoptimize\models\BaseImageTag. Since you implemented __get, consider adding a @property annotation.
Loading history...
99
            default => $this->optimizedImage->getPlaceholderBox('#CCC'),
100
        };
101
    }
102
}
103