Passed
Push — v4 ( 925817...1aae3a )
by Andrew
17:25 queued 10:34
created

PictureTag::init()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
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 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 $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 <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
     * @param $config
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
51
     */
52
    public function __construct($config = [])
53
    {
54
        parent::__construct($config);
55
        // Populate the $imageAttrs
56
        $this->imgAttrs = [
57
            'class' => '',
58
            'style' => '',
59
            'width' => $this->optimizedImage->placeholderWidth,
60
            'height' => $this->optimizedImage->placeholderHeight,
61
            '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

61
            'src' => reset(/** @scrutinizer ignore-type */ $this->optimizedImage->optimizedImageUrls),
Loading history...
62
            'loading' => '',
63
        ];
64
        // Populate the $sourceAttrs
65
        $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

65
        $this->populateSourceAttrs(/** @scrutinizer ignore-type */ $this->optimizedImage, []);
Loading history...
66
        // Populate the $pictureAttrs
67
        $this->pictureAttrs = [];
68
    }
69
70
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
71
     * @inheritDoc
72
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
73
    public function init(): void
74
    {
75
        parent::init();
76
        // If the original image is an SVG or gif, don't add the placeholder box CSS so that transparency works as intended
77
        $path = parse_url($this->imgAttrs['src'], PHP_URL_PATH);
78
        $extension = pathinfo($path, PATHINFO_EXTENSION);
79
        if ($extension === 'svg' || $extension === 'gif') {
80
            $this->placeholder = 'none';
81
        }
82
    }
83
84
    /**
85
     * Set the $loading property
86
     *
87
     * @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...
88
     * @return $this
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
89
     */
90
    public function loadingStrategy(string $value): PictureTag
91
    {
92
        $this->loadingStrategy = $value;
93
94
        return $this;
95
    }
96
97
    /**
98
     * Set the $placeholder property
99
     *
100
     * @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...
101
     * @return $this
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
102
     */
103
    public function placeholder(string $value): PictureTag
104
    {
105
        $this->placeholder = $value;
106
107
        return $this;
108
    }
109
110
    /**
111
     * Merge the passed array of tag attributes into $pictureAttrs
112
     *
113
     * @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...
114
     * @return $this
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
115
     */
116
    public function pictureAttrs(array $value): PictureTag
117
    {
118
        $this->pictureAttrs = array_merge($this->pictureAttrs, $value);
119
120
        return $this;
121
    }
122
123
    /**
124
     * Merge the passed array of tag attributes into $sourceAttrs
125
     *
126
     * @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...
127
     * @return $this
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
128
     */
129
    public function sourceAttrs(array $value): PictureTag
130
    {
131
        foreach ($this->sourceAttrs as &$attrs) {
132
            $attrs = array_merge($attrs, $value);
133
        }
134
        unset($attrs);
135
136
        return $this;
137
    }
138
139
    /**
140
     * Merge the passed array of tag attributes into $imgAttrs
141
     *
142
     * @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...
143
     * @return $this
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
144
     */
145
    public function imgAttrs(array $value): PictureTag
146
    {
147
        $this->imgAttrs = array_merge($this->imgAttrs, $value);
148
149
        return $this;
150
    }
151
152
    /**
153
     * Add art direction sources to the $sourceAttrs
154
     *
155
     * @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...
156
     * @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...
157
     * @return PictureTag
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
158
     */
159
    public function addSourceFrom(OptimizedImage $optimizedImage, array $sourceAttrs = []): PictureTag
160
    {
161
        $this->populateSourceAttrs($optimizedImage, $sourceAttrs);
162
163
        return $this;
164
    }
165
166
    /**
167
     * Generate a complete <img> tag for the $optimizedImage OptimizedImage model
168
     *
169
     * @return Markup
170
     */
171
    public function render(): Markup
172
    {
173
        $content = '';
174
        // Handle the <source> tag(s)
175
        foreach ($this->sourceAttrs as $attrs) {
176
            // Handle lazy loading
177
            if ($this->loadingStrategy !== 'eager') {
178
                $attrs = $this->swapLazyLoadAttrs($this->loadingStrategy, $this->placeholder, $attrs);
179
            }
180
            // Remove any empty attributes
181
            $attrs = array_filter($attrs);
182
            // Render the tag
183
            $content .= Html::tag('source', '', $attrs);
184
        }
185
        // Handle the <img> tag
186
        $attrs = $this->imgAttrs;
187
        // Handle lazy loading
188
        if ($this->loadingStrategy !== 'eager') {
189
            $attrs = $this->swapLazyLoadAttrs($this->loadingStrategy, $this->placeholder, $attrs);
190
        }
191
        // Remove any empty attributes
192
        $attrs = $this->filterEmptyAttributes($attrs);
193
        // Render the tag
194
        $content .= Html::tag('img', '', $attrs);
195
        // Handle the <picture> tag
196
        $attrs = $this->pictureAttrs;
197
        // Remove any empty attributes
198
        $attrs = array_filter($attrs);
199
        // Render the tag
200
        $tag = Html::tag('picture', $content, $attrs);
201
202
        return Template::raw($tag);
203
    }
204
205
    /**
206
     * Populate the $sourceAttrs from the passed in $optimizedImage and $sizes
207
     *
208
     * @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...
209
     * @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...
210
     * @return void
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
211
     */
212
    protected function populateSourceAttrs(OptimizedImage $optimizedImage, array $sourceAttrs): void
213
    {
214
        if (!empty($optimizedImage->optimizedWebPImageUrls)) {
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->optimizedWebPImageUrls),
218
                'type' => 'image/webp',
219
                'sizes' => '100vw',
220
                'width' => $optimizedImage->placeholderWidth,
221
                'height' => $optimizedImage->placeholderHeight,
222
            ], $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...
223
        }
224
        $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...
225
            'media' => '',
226
            '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

226
            'srcset' => $optimizedImage->getSrcsetFromArray(/** @scrutinizer ignore-type */ $optimizedImage->optimizedImageUrls),
Loading history...
227
            'sizes' => '100vw',
228
            'width' => $optimizedImage->placeholderWidth,
229
            'height' => $optimizedImage->placeholderHeight,
230
        ], $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...
231
    }
232
}
233