ImgTag::render()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 13
rs 10
cc 2
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 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', 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 <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
     * @inheritDoc
41
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
42
    public function init(): void
43
    {
44
        parent::init();
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
        // If the original image is an SVG or gif, don't add the placeholder box CSS so that transparency works as intended
57
        $path = parse_url($this->imgAttrs['src'], PHP_URL_PATH);
58
        $extension = pathinfo($path, PATHINFO_EXTENSION);
59
        if ($extension === 'svg' || $extension === 'gif') {
60
            $this->placeholder = 'none';
61
        }
62
    }
63
64
    /**
65
     * Set the $loading property
66
     *
67
     * @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...
68
     * @return $this
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
69
     */
70
    public function loadingStrategy(string $value): ImgTag
71
    {
72
        $this->loadingStrategy = $value;
73
74
        return $this;
75
    }
76
77
    /**
78
     * Set the $placeholder property
79
     *
80
     * @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...
81
     * @return $this
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
82
     */
83
    public function placeholder(string $value): ImgTag
84
    {
85
        $this->placeholder = $value;
86
87
        return $this;
88
    }
89
90
    /**
91
     * Merge the passed array of tag attributes into $imgAttrs
92
     *
93
     * @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...
94
     * @return $this
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
95
     */
96
    public function imgAttrs(array $value): ImgTag
97
    {
98
        $this->imgAttrs = array_merge($this->imgAttrs, $value);
99
100
        return $this;
101
    }
102
103
    /**
104
     * Generate a complete <img> tag for the $optimizedImage OptimizedImage model
105
     *
106
     * @return Markup
107
     */
108
    public function render(): Markup
109
    {
110
        $attrs = $this->imgAttrs;
111
        // Handle lazy loading
112
        if ($this->loadingStrategy !== 'eager') {
113
            $attrs = $this->swapLazyLoadAttrs($this->loadingStrategy, $this->placeholder, $attrs);
114
        }
115
        // Remove any empty attributes
116
        $attrs = $this->filterEmptyAttributes($attrs);
117
        // Render the tag
118
        $tag = Html::tag('img', '', $attrs);
119
120
        return Template::raw($tag);
121
    }
122
}
123