Passed
Push — v4 ( 7fc762...711ea3 )
by Andrew
14:46 queued 07:41
created

BaseImageTag   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
eloc 37
c 1
b 0
f 0
dl 0
loc 76
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
D swapLazyLoadAttrs() 0 47 12
A getLazyLoadSrc() 0 8 1
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
            $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':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
46
            case 'lazySizesFallback':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
47
                if (isset($attrs['loading'])) {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
48
                    $attrs['loading'] = 'lazy';
49
                }
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
50
                break;
51
            default:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
52
                break;
53
        }
54
        // Handle attributes that lazySizes and lazySizesFallback have in common
55
        switch ($loading) {
56
            case 'lazySizes':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
57
            case 'lazySizesFallback':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
58
                // Only swap to data- attributes if they want the LazySizes fallback
59
                if (!empty($attrs['sizes'])) {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
60
                    $attrs['data-sizes'] = $attrs['sizes'];
61
                    $attrs['sizes'] = '';
62
                }
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
63
                if (!empty($attrs['srcset'])) {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
64
                    $attrs['data-srcset'] = $attrs['srcset'];
65
                    $attrs['srcset'] = '';
66
                }
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
67
                if (!empty($attrs['src'])) {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
68
                    $attrs['data-src'] = $attrs['src'];
69
                    $attrs['src'] = $this->getLazyLoadSrc($placeHolder);
70
                }
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
71
                break;
72
            default:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
73
                break;
74
        }
75
76
        return $attrs;
77
    }
78
79
    /**
80
     * Return a lazy loading placeholder image based on the passed in $lazyload setting
81
     *
82
     * @param string $lazyLoad
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
83
     *
84
     * @return string
85
     */
86
    protected function getLazyLoadSrc(string $lazyLoad): string
87
    {
88
        $lazyLoad = strtolower($lazyLoad);
89
        return match ($lazyLoad) {
90
            '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

90
            '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...
91
            'silhouette' => $this->optimizedImage->getPlaceholderSilhouette(),
92
            '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...
93
            default => $this->optimizedImage->getPlaceholderBox('#CCC'),
94
        };
95
    }
96
}
97