Passed
Push — v5 ( 10813e...ae9f14 )
by Andrew
26:15 queued 20:08
created

LinkPreloadTag::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 15
rs 9.9332
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 LinkPreloadTag extends BaseTag
23
{
24
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
25
     * @var array array of tag attributes for the <link rel="preload"> tag
26
     */
27
    public array $linkAttrs = [];
28
29
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
30
     * @inheritDoc
31
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
32
    public function init(): void
33
    {
34
        parent::init();
35
        // Any web browser that supports link rel="preload" as="image" also supports webp, so prefer that
36
        $srcset = $this->optimizedImage->optimizedImageUrls;
37
        if (!empty($this->optimizedImage->optimizedWebPImageUrls)) {
38
            $srcset = $this->optimizedImage->optimizedWebPImageUrls;
39
        }
40
        // Populate the $imageAttrs
41
        $this->linkAttrs = [
42
            'rel' => 'preload',
43
            'as' => 'image',
44
            'href' => reset($srcset),
0 ignored issues
show
Bug introduced by
It seems like $srcset 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

44
            'href' => reset(/** @scrutinizer ignore-type */ $srcset),
Loading history...
45
            'imagesrcset' => $this->optimizedImage->getSrcsetFromArray($srcset),
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

45
            'imagesrcset' => $this->optimizedImage->/** @scrutinizer ignore-call */ getSrcsetFromArray($srcset),

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...
46
            'imagesizes' => '100vw',
47
        ];
48
    }
49
50
    /**
51
     * Merge the passed array of tag attributes into $linkAttrs
52
     *
53
     * @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...
54
     * @return $this
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
55
     */
56
    public function linkAttrs(array $value): LinkPreloadTag
57
    {
58
        $this->linkAttrs = array_merge($this->linkAttrs, $value);
59
60
        return $this;
61
    }
62
63
    /**
64
     * Generate a complete <link rel="preload"> tag for the $optimizedImage OptimizedImage model
65
     * ref: https://web.dev/preload-responsive-images/#imagesrcset-and-imagesizes
66
     *
67
     * @return Markup
68
     */
69
    public function render(): Markup
70
    {
71
        $attrs = $this->linkAttrs;
72
        // Remove any empty attributes
73
        $attrs = $this->filterEmptyAttributes($attrs);
74
        // Render the tag
75
        $tag = Html::tag('link', '', $attrs);
76
77
        return Template::raw($tag);
78
    }
79
}
80