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

ImgTag::loadingStrategy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
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 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'
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
    /**
59
     * Set the $loading property
60
     *
61
     * @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...
62
     * @return $this
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
63
     */
64
    public function loadingStrategy(string $value): ImgTag
65
    {
66
        $this->loadingStrategy = $value;
67
68
        return $this;
69
    }
70
71
    /**
72
     * Set the $placeholder property
73
     *
74
     * @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...
75
     * @return $this
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
76
     */
77
    public function placeholder(string $value): ImgTag
78
    {
79
        $this->placeholder = $value;
80
81
        return $this;
82
    }
83
84
    /**
85
     * Merge the passed array of tag attributes into $imgAttrs
86
     *
87
     * @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...
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 imgAttrs(array $value): ImgTag
91
    {
92
        $this->imgAttrs = array_merge($this->imgAttrs, $value);
93
94
        return $this;
95
    }
96
97
    /**
98
     * Generate a complete <img> tag for the $optimizedImage OptimizedImage model
99
     *
100
     * @return Markup
101
     */
102
    public function render(): Markup
103
    {
104
        $attrs = $this->imgAttrs;
105
        // Handle lazy loading
106
        if ($this->loadingStrategy !== 'eager') {
107
            $attrs = $this->swapLazyLoadAttrs($this->loadingStrategy, $this->placeholder, $attrs);
108
        }
109
        // Remove any empty attributes
110
        $attrs = array_filter($attrs);
111
        // Render the tag
112
        $tag = Html::tag('img', '', $attrs);
113
114
        return Template::raw($tag);
115
    }
116
}
117