Passed
Push — v5 ( 232b22...10813e )
by Andrew
24:11 queued 16:50
created

ImgTag::init()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 8
rs 10
cc 3
nc 2
nop 0
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
use craft\helpers\Html;
14
use craft\helpers\Template;
15
use Twig\Markup;
16
17
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
18
 * @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...
19
 * @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...
20
 * @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...
21
 */
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...
22
class ImgTag extends BaseImageTag
23
{
24
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
25
     * @var string The loading scheme to use: 'eager', 'lazy', 'lazySizes', 'lazySizesFallback'
26
     */
27
    public string $loadingStrategy = 'eager';
28
29
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
30
     * @var string The type of placeholder image to use: 'box', 'color', 'image', 'silhouette', or 'none'
31
     */
32
    public string $placeholder = 'box';
33
34
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
35
     * @var array array of tag attributes for the <img> tag
36
     */
37
    public array $imgAttrs = [];
38
39
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
40
     * @param $config
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
41
     */
42
    public function __construct($config = [])
43
    {
44
        parent::__construct($config);
45
        // Populate the $imageAttrs
46
        $this->imgAttrs = [
47
            'class' => '',
48
            'style' => '',
49
            'width' => $this->optimizedImage->placeholderWidth,
50
            'height' => $this->optimizedImage->placeholderHeight,
51
            'src' => reset($this->optimizedImage->optimizedImageUrls),
0 ignored issues
show
Bug introduced by
It seems like $this->optimizedImage->optimizedImageUrls can also be of type null; however, parameter $array of reset() does only seem to accept array|object, maybe add an additional type check? ( Ignorable by Annotation )

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

51
            'src' => reset(/** @scrutinizer ignore-type */ $this->optimizedImage->optimizedImageUrls),
Loading history...
52
            'srcset' => $this->optimizedImage->getSrcsetFromArray($this->optimizedImage->optimizedImageUrls),
0 ignored issues
show
Bug introduced by
The method getSrcsetFromArray() 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

52
            'srcset' => $this->optimizedImage->/** @scrutinizer ignore-call */ getSrcsetFromArray($this->optimizedImage->optimizedImageUrls),

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...
53
            'sizes' => '100vw',
54
            'loading' => '',
55
        ];
56
    }
57
58
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
59
     * @inheritDoc
60
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
61
    public function init(): void
62
    {
63
        parent::init();
64
        // If the original image is an SVG or gif, don't add the placeholder box CSS so that transparency works as intended
65
        $path = parse_url($this->imgAttrs['src'], PHP_URL_PATH);
66
        $extension = pathinfo($path, PATHINFO_EXTENSION);
67
        if ($extension === 'svg' || $extension === 'gif') {
68
            $this->placeholder = 'none';
69
        }
70
    }
71
72
    /**
73
     * Set the $loading property
74
     *
75
     * @param string $value
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
76
     * @return $this
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
77
     */
78
    public function loadingStrategy(string $value): ImgTag
79
    {
80
        $this->loadingStrategy = $value;
81
82
        return $this;
83
    }
84
85
    /**
86
     * Set the $placeholder property
87
     *
88
     * @param string $value
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
89
     * @return $this
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
90
     */
91
    public function placeholder(string $value): ImgTag
92
    {
93
        $this->placeholder = $value;
94
95
        return $this;
96
    }
97
98
    /**
99
     * Merge the passed array of tag attributes into $imgAttrs
100
     *
101
     * @param array $value
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
102
     * @return $this
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
103
     */
104
    public function imgAttrs(array $value): ImgTag
105
    {
106
        $this->imgAttrs = array_merge($this->imgAttrs, $value);
107
108
        return $this;
109
    }
110
111
    /**
112
     * Generate a complete <img> tag for the $optimizedImage OptimizedImage model
113
     *
114
     * @return Markup
115
     */
116
    public function render(): Markup
117
    {
118
        $attrs = $this->imgAttrs;
119
        // Handle lazy loading
120
        if ($this->loadingStrategy !== 'eager') {
121
            $attrs = $this->swapLazyLoadAttrs($this->loadingStrategy, $this->placeholder, $attrs);
122
        }
123
        // Remove any empty attributes
124
        $attrs = $this->filterEmptyAttributes($attrs);
125
        // Render the tag
126
        $tag = Html::tag('img', '', $attrs);
127
128
        return Template::raw($tag);
129
    }
130
}
131