CraftImageTransform   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 21
dl 0
loc 92
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 6 1
A getSettingsHtml() 0 9 1
A rules() 0 4 1
A getTransformUrl() 0 12 2
A getWebPUrl() 0 3 1
A displayName() 0 3 1
A fields() 0 3 1
1
<?php
2
/**
3
 * ImageOptimize 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) 2018 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\imagetransforms;
12
13
use Craft;
14
use craft\elements\Asset;
15
use craft\models\ImageTransform as CraftImageTransformModel;
16
use nystudio107\imageoptimize\ImageOptimize;
17
use nystudio107\imageoptimize\models\Settings;
18
19
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
20
 * @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...
21
 * @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...
22
 * @since     1.6.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...
23
 */
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...
24
class CraftImageTransform extends ImageTransform
25
{
26
    // Static Methods
27
    // =========================================================================
28
29
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
30
     * @var bool
31
     */
32
    public bool $generateTransformsBeforePageLoad;
33
34
    // Public Properties
35
    // =========================================================================
36
37
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
38
     * @inheritdoc
39
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
40
    public static function displayName(): string
41
    {
42
        return Craft::t('image-optimize', 'Craft');
43
    }
44
45
    // Public Methods
46
    // =========================================================================
47
48
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
49
     * @inheritDoc
50
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
51
    public function init(): void
52
    {
53
        /** @var Settings $settings */
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
54
        $settings = ImageOptimize::$plugin->getSettings();
0 ignored issues
show
Bug introduced by
The method getSettings() 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

54
        /** @scrutinizer ignore-call */ 
55
        $settings = ImageOptimize::$plugin->getSettings();

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...
55
        // Get our $generateTransformsBeforePageLoad setting
56
        $this->generateTransformsBeforePageLoad = $settings->generateTransformsBeforePageLoad ?? true;
57
    }
58
59
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $asset should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $transform should have a doc-comment as per coding-style.
Loading history...
60
     * @inheritDoc
61
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
62
    public function getTransformUrl(Asset $asset, CraftImageTransformModel|string|array|null $transform): ?string
63
    {
64
        // Generate the URLs to the optimized images
65
        $oldValue = Craft::$app->getConfig()->getGeneral()->generateTransformsBeforePageLoad;
66
67
        if ($this->generateTransformsBeforePageLoad) {
68
            Craft::$app->getConfig()->getGeneral()->generateTransformsBeforePageLoad = true;
69
        }
70
        $url = $asset->getUrl($transform);
71
        Craft::$app->getConfig()->getGeneral()->generateTransformsBeforePageLoad = $oldValue;
72
73
        return $url;
74
    }
75
76
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $url should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $asset should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $transform should have a doc-comment as per coding-style.
Loading history...
77
     * @inheritDoc
78
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
79
    public function getWebPUrl(string $url, Asset $asset, CraftImageTransformModel|string|array|null $transform): ?string
80
    {
81
        return $this->appendExtension($url, '.webp');
82
    }
83
84
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
85
     * @inheritdoc
86
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
87
    public function getSettingsHtml(): ?string
88
    {
89
        $imageProcessors = ImageOptimize::$plugin->optimize->getActiveImageProcessors();
90
        $variantCreators = ImageOptimize::$plugin->optimize->getActiveVariantCreators();
91
92
        return Craft::$app->getView()->renderTemplate('craft-image-transform/settings/image-transforms/craft.twig', [
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
93
            'imageTransform' => $this,
94
            'imageProcessors' => $imageProcessors,
95
            'variantCreators' => $variantCreators,
96
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
97
    }
98
99
    /**
100
     * No savable fields for this component
101
     *
102
     * @return array
103
     */
104
    public function fields(): array
105
    {
106
        return [];
107
    }
108
109
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
110
     * @inheritdoc
111
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
112
    public function rules(): array
113
    {
114
        $rules = parent::rules();
115
        return array_merge($rules, [
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
116
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
117
    }
118
}
119