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

PictureTag::imgAttrs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 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
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 PictureTag 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 $loading = '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'
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 <picture> tag
36
     */
37
    public array $pictureAttrs = [];
38
39
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
40
     * @var array array of tag attributes for the <source> tags
41
     */
42
    public array $sourceAttrs = [];
43
44
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
45
     * @var array array of tag attributes for the <img> tag
46
     */
47
    public array $imgAttrs = [];
48
49
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
50
     * @var OptimizedImage[] array OptimizedImage models to add as art direction
51
     */
52
    public array $artDirection = [];
53
54
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
55
     * @param $config
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
56
     */
57
    public function __construct($config = [])
58
    {
59
        parent::__construct($config);
60
        // Populate the $imageAttrs
61
        $this->imgAttrs = [
62
            'class' => '',
63
            'style' => '',
64
            'width' => $this->optimizedImage->placeholderWidth,
65
            'height' => $this->optimizedImage->placeholderHeight,
66
            '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

66
            'src' => reset(/** @scrutinizer ignore-type */ $this->optimizedImage->optimizedImageUrls),
Loading history...
67
            'loading' => '',
68
        ];
69
        // Populate the $sourceAttrs
70
        $this->populateSourceAttrs($this->optimizedImage, []);
0 ignored issues
show
Bug introduced by
It seems like $this->optimizedImage can also be of type null; however, parameter $optimizedImage of nystudio107\imageoptimiz...::populateSourceAttrs() does only seem to accept nystudio107\imageoptimize\models\OptimizedImage, 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

70
        $this->populateSourceAttrs(/** @scrutinizer ignore-type */ $this->optimizedImage, []);
Loading history...
71
        // Populate the $pictureAttrs
72
        $this->pictureAttrs = [];
73
    }
74
75
    /**
76
     * Set the $loading property
77
     *
78
     * @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...
79
     * @return $this
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
80
     */
81
    public function loading(string $value): PictureTag
82
    {
83
        $this->loading = $value;
84
85
        return $this;
86
    }
87
88
    /**
89
     * Set the $placeholder property
90
     *
91
     * @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...
92
     * @return $this
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
93
     */
94
    public function placeholder(string $value): PictureTag
95
    {
96
        $this->placeholder = $value;
97
98
        return $this;
99
    }
100
101
    /**
102
     * Merge the passed array of tag attributes into $pictureAttrs
103
     *
104
     * @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...
105
     * @return $this
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
106
     */
107
    public function pictureAttrs(array $value): PictureTag
108
    {
109
        $this->pictureAttrs = array_merge($this->pictureAttrs, $value);
110
111
        return $this;
112
    }
113
114
    /**
115
     * Merge the passed array of tag attributes into $sourceAttrs
116
     *
117
     * @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...
118
     * @return $this
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
119
     */
120
    public function sourceAttrs(array $value): PictureTag
121
    {
122
        foreach ($this->sourceAttrs as &$attrs) {
123
            $attrs = array_merge($attrs, $value);
124
        }
125
        unset($attrs);
126
127
        return $this;
128
    }
129
130
    /**
131
     * Merge the passed array of tag attributes into $imgAttrs
132
     *
133
     * @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...
134
     * @return $this
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
135
     */
136
    public function imgAttrs(array $value): PictureTag
137
    {
138
        $this->imgAttrs = array_merge($this->imgAttrs, $value);
139
140
        return $this;
141
    }
142
143
    /**
144
     * Add art direction sources to the $sourceAttrs
145
     *
146
     * @param OptimizedImage $optimizedImage
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...
147
     * @param array $sourceAttrs
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 10 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
148
     * @return PictureTag
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
149
     */
150
    public function artDirection(OptimizedImage $optimizedImage, array $sourceAttrs = []): PictureTag
151
    {
152
        $this->populateSourceAttrs($optimizedImage, $sourceAttrs);
153
154
        return $this;
155
    }
156
157
    /**
158
     * Generate a complete <img> tag for the $optimizedImage OptimizedImage model
159
     *
160
     * @return Markup
161
     */
162
    public function render(): Markup
163
    {
164
        $content = '';
165
        // Handle the <source> tag(s)
166
        foreach ($this->sourceAttrs as $attrs) {
167
            // Handle lazy loading
168
            if ($this->loading !== 'eager') {
169
                $attrs = $this->swapLazyLoadAttrs($this->loading, $this->placeholder, $attrs);
170
            }
171
            // Remove any empty attributes
172
            $attrs = array_filter($attrs);
173
            // Render the tag
174
            $content .= Html::tag('source', '', $attrs);
175
        }
176
        // Handle the <img> tag
177
        $attrs = $this->imgAttrs;
178
        // Handle lazy loading
179
        if ($this->loading !== 'eager') {
180
            $attrs = $this->swapLazyLoadAttrs($this->loading, $this->placeholder, $attrs);
181
        }
182
        // Remove any empty attributes
183
        $attrs = array_filter($attrs);
184
        // Render the tag
185
        $content .= Html::tag('img', '', $attrs);
186
        // Handle the <picture> tag
187
        $attrs = $this->pictureAttrs;
188
        // Remove any empty attributes
189
        $attrs = array_filter($attrs);
190
        // Render the tag
191
        $tag = Html::tag('picture', $content, $attrs);
192
193
        return Template::raw($tag);
194
    }
195
196
    /**
197
     * Populate the $sourceAttrs from the passed in $optimizedImage and $sizes
198
     *
199
     * @param OptimizedImage $optimizedImage
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...
200
     * @param array $sourceAttrs attributes to add to the $sourceAttrs array
0 ignored issues
show
Coding Style introduced by
Expected 10 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
201
     * @return void
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
202
     */
203
    protected function populateSourceAttrs(OptimizedImage $optimizedImage, array $sourceAttrs): void
204
    {
205
        if (!empty($optimizedImage->optimizedWebPImageUrls)) {
206
            $this->sourceAttrs[] = array_merge([
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
207
                'media' => '',
208
                'srcset' => $optimizedImage->getSrcsetFromArray($optimizedImage->optimizedWebPImageUrls),
209
                'type' => 'image/webp',
210
                'sizes' => '100vw',
211
                'width' => $optimizedImage->placeholderWidth,
212
                'height' => $optimizedImage->placeholderHeight,
213
            ], $sourceAttrs);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
214
        }
215
        $this->sourceAttrs[] = array_merge([
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
216
            'media' => '',
217
            'srcset' => $optimizedImage->getSrcsetFromArray($optimizedImage->optimizedImageUrls),
0 ignored issues
show
Bug introduced by
It seems like $optimizedImage->optimizedImageUrls can also be of type null; however, parameter $array of nystudio107\imageoptimiz...e::getSrcsetFromArray() does only seem to accept array, 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

217
            'srcset' => $optimizedImage->getSrcsetFromArray(/** @scrutinizer ignore-type */ $optimizedImage->optimizedImageUrls),
Loading history...
218
            'sizes' => '100vw',
219
            'width' => $optimizedImage->placeholderWidth,
220
            'height' => $optimizedImage->placeholderHeight,
221
        ], $sourceAttrs);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
222
    }
223
}
224