Passed
Push — develop ( 02dcc0...b4966a )
by Andrew
66:22 queued 61:23
created

ImageOptimizeVariable   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 9
eloc 13
c 6
b 0
f 0
dl 0
loc 88
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A serverSupportsWebP() 0 11 4
A placeholderBox() 0 3 1
A renderLazyLoadJs() 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 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 lazy load JavaScript shim
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 renderLazyLoadJs($scriptAttrs = [], $variables = [])
38
    {
39
        return Template::raw(ImageOptimize::$plugin->optimize->renderLazyLoadJs($scriptAttrs, $variables));
40
    }
41
42
    /**
43
     * Return an SVG box as a placeholder image
44
     *
45
     * @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...
46
     * @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...
47
     * @param string|null $color
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
48
     *
49
     * @return \Twig_Markup|null
50
     */
51
    public function placeholderBox($width, $height, $color = null)
52
    {
53
        return Template::raw(ImageOptimize::$plugin->placeholder->generatePlaceholderBox($width, $height, $color));
54
    }
55
56
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
57
     * @param Asset $asset
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
58
     * @param array $variants
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
59
     * @param bool  $generatePlaceholders
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
60
     *
61
     * @return OptimizedImage|null
62
     */
63
    public function createOptimizedImages(
64
        Asset $asset,
65
        $variants = null,
66
        $generatePlaceholders = false
67
    ) {
68
        // Override our settings for lengthy operations, since we're doing this via Twig
69
        ImageOptimize::$generatePlaceholders = $generatePlaceholders;
70
71
        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

71
        return ImageOptimize::$plugin->optimizedImages->createOptimizedImages($asset, /** @scrutinizer ignore-type */ $variants);
Loading history...
72
    }
73
74
    /**
75
     * Returns whether `.webp` is a format supported by the server
76
     *
77
     * @return bool
78
     */
79
    public function serverSupportsWebP(): bool
80
    {
81
        $result = false;
82
        $variantCreators = ImageOptimize::$plugin->optimize->getActiveVariantCreators();
83
        foreach ($variantCreators as $variantCreator) {
84
            if ($variantCreator['creator'] === 'cwebp' && $variantCreator['installed']) {
85
                $result = true;
86
            }
87
        }
88
89
        return $result;
90
    }
91
92
    /**
93
     * Creates an Image Transform with a given config.
94
     *
95
     * @param mixed $config The Image Transform’s class name, or its config,
96
     *                      with a `type` value and optionally a `settings` value
97
     *
98
     * @return null|ImageTransformInterface The Image Transform
99
     */
100
    public function createImageTransformType($config): ImageTransformInterface
101
    {
102
        return ImageOptimize::$plugin->optimize->createImageTransformType($config);
103
    }
104
105
    /**
106
     * Return whether we are running Craft 3.1 or later
107
     *
108
     * @return bool
109
     */
110
    public function craft31(): bool
111
    {
112
        return ImageOptimize::$craft31;
113
    }
114
}
115