ImageOptimizeVariable   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 7
eloc 10
dl 0
loc 95
rs 10
c 5
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A renderLazySizesJs() 0 3 1
A renderLazySizesFallbackJs() 0 3 1
A placeholderBox() 0 3 1
A serverSupportsWebP() 0 3 1
A craft31() 0 3 1
A createImageTransformType() 0 3 1
A createOptimizedImages() 0 9 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) 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 craft\elements\Asset;
14
use craft\helpers\Template;
15
use nystudio107\imageoptimize\ImageOptimize;
16
use nystudio107\imageoptimize\imagetransforms\ImageTransformInterface;
17
use nystudio107\imageoptimize\models\OptimizedImage;
18
use nystudio107\pluginvite\variables\ViteVariableInterface;
19
use nystudio107\pluginvite\variables\ViteVariableTrait;
20
use Twig\Markup;
21
use yii\base\InvalidConfigException;
22
23
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
24
 * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
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...
25
 * @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...
26
 * @since     1.4.0
0 ignored issues
show
Coding Style introduced by
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
27
 */
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...
28
class ImageOptimizeVariable implements ViteVariableInterface
29
{
30
    use ViteVariableTrait;
31
32
    // Public Methods
33
    // =========================================================================
34
35
    /**
36
     * Render the LazySizes fallback JS
37
     *
38
     * @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...
39
     * @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...
40
     * @return Markup
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
41
     */
42
    public function renderLazySizesFallbackJs(array $scriptAttrs = [], array $variables = []): Markup
43
    {
44
        return Template::raw(ImageOptimize::$plugin->optimize->renderLazySizesFallbackJs($scriptAttrs, $variables));
45
    }
46
47
    /**
48
     * Render the LazySizes JS
49
     *
50
     * @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...
51
     * @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...
52
     * @return Markup
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
53
     */
54
    public function renderLazySizesJs(array $scriptAttrs = [], array $variables = []): Markup
55
    {
56
        return Template::raw(ImageOptimize::$plugin->optimize->renderLazySizesJs($scriptAttrs, $variables));
57
    }
58
59
    /**
60
     * Return an SVG box as a placeholder image
61
     *
62
     * @param             $width
0 ignored issues
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 1 spaces but found 13
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
63
     * @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...
64
     * @param ?string $color
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
65
     *
66
     * @return Markup
67
     */
68
    public function placeholderBox($width, $height, ?string $color = null): Markup
69
    {
70
        return Template::raw(ImageOptimize::$plugin->placeholder->generatePlaceholderBox($width, $height, $color));
71
    }
72
73
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
74
     * @param Asset $asset
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
75
     * @param ?array $variants
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
76
     * @param bool $generatePlaceholders
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
77
     *
78
     * @return ?OptimizedImage
79
     * @throws InvalidConfigException
80
     */
81
    public function createOptimizedImages(
82
        Asset  $asset,
83
        ?array $variants = null,
84
        bool   $generatePlaceholders = false,
85
    ): ?OptimizedImage {
86
        // Override our settings for lengthy operations, since we're doing this via Twig
87
        ImageOptimize::$generatePlaceholders = $generatePlaceholders;
88
89
        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

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