Passed
Push — develop ( 6a45d5...f44154 )
by Andrew
04:59
created

ImageOptimizeVariable::renderLazySizesFallbackJs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 2
1
<?php
2
/**
3
 * Image Optimize plugin for Craft CMS 3.x
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) 2017 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\variables;
12
13
use nystudio107\imageoptimize\ImageOptimize;
14
use nystudio107\imageoptimize\imagetransforms\ImageTransformInterface;
15
use nystudio107\imageoptimize\models\OptimizedImage;
16
17
use craft\elements\Asset;
18
use craft\helpers\Template;
19
20
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
21
 * @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...
22
 * @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...
23
 * @since     1.4.0
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...
24
 */
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...
25
class ImageOptimizeVariable extends ManifestVariable
26
{
27
    // Public Methods
28
    // =========================================================================
29
30
    /**
31
     * Render the LazySizes fallback JS
32
     *
33
     * @param array $scriptAttrs
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...
34
     * @param array $variables
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...
35
     * @return string
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
36
     */
37
    public function renderLazySizesFallbackJs($scriptAttrs = [], $variables = [])
38
    {
39
        return Template::raw(ImageOptimize::$plugin->optimize->renderLazySizesFallbackJs($scriptAttrs, $variables));
40
    }
41
42
    /**
43
     * Render the LazySizes JS
44
     *
45
     * @param array $scriptAttrs
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...
46
     * @param array $variables
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...
47
     * @return string
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
48
     */
49
    public function renderLazySizesJs($scriptAttrs = [], $variables = [])
50
    {
51
        return Template::raw(ImageOptimize::$plugin->optimize->renderLazySizesJs($scriptAttrs, $variables));
52
    }
53
54
    /**
55
     * Return an SVG box as a placeholder image
56
     *
57
     * @param             $width
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 1 spaces but found 13
Loading history...
58
     * @param             $height
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 1 spaces but found 13
Loading history...
59
     * @param string|null $color
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
60
     *
61
     * @return \Twig_Markup|null
62
     */
63
    public function placeholderBox($width, $height, $color = null)
64
    {
65
        return Template::raw(ImageOptimize::$plugin->placeholder->generatePlaceholderBox($width, $height, $color));
66
    }
67
68
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
69
     * @param Asset $asset
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
70
     * @param array $variants
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
71
     * @param bool  $generatePlaceholders
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
72
     *
73
     * @return OptimizedImage|null
74
     */
75
    public function createOptimizedImages(
76
        Asset $asset,
77
        $variants = null,
78
        $generatePlaceholders = false
79
    ) {
80
        // Override our settings for lengthy operations, since we're doing this via Twig
81
        ImageOptimize::$generatePlaceholders = $generatePlaceholders;
82
83
        return ImageOptimize::$plugin->optimizedImages->createOptimizedImages($asset, $variants);
0 ignored issues
show
Bug introduced by
It seems like $variants can also be of type null; however, parameter $variants of nystudio107\imageoptimiz...createOptimizedImages() 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

83
        return ImageOptimize::$plugin->optimizedImages->createOptimizedImages($asset, /** @scrutinizer ignore-type */ $variants);
Loading history...
84
    }
85
86
    /**
87
     * Returns whether `.webp` is a format supported by the server
88
     *
89
     * @return bool
90
     */
91
    public function serverSupportsWebP(): bool
92
    {
93
        return ImageOptimize::$plugin->optimize->serverSupportsWebP();
94
    }
95
96
    /**
97
     * Creates an Image Transform with a given config.
98
     *
99
     * @param mixed $config The Image Transform’s class name, or its config,
100
     *                      with a `type` value and optionally a `settings` value
101
     *
102
     * @return null|ImageTransformInterface The Image Transform
103
     */
104
    public function createImageTransformType($config): ImageTransformInterface
105
    {
106
        return ImageOptimize::$plugin->optimize->createImageTransformType($config);
107
    }
108
109
    /**
110
     * Return whether we are running Craft 3.1 or later
111
     *
112
     * @return bool
113
     */
114
    public function craft31(): bool
115
    {
116
        return ImageOptimize::$craft31;
117
    }
118
}
119