PictureTag::render()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 32
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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

218
            'srcset' => $optimizedImage->getSrcsetFromArray(/** @scrutinizer ignore-type */ $optimizedImage->optimizedImageUrls),
Loading history...
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
}
225