Passed
Branch v5 (86ca0f)
by Andrew
05:08
created

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

212
            'srcset' => $optimizedImage->getSrcsetFromArray(/** @scrutinizer ignore-type */ $optimizedImage->optimizedImageUrls),
Loading history...
213
            'sizes' => '100vw',
214
            'width' => $optimizedImage->placeholderWidth,
215
            'height' => $optimizedImage->placeholderHeight,
216
        ], $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...
217
    }
218
}
219