ImageTransform   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 18
eloc 54
c 1
b 1
f 0
dl 0
loc 161
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A prefetchRemoteFile() 0 13 1
A getTemplatesRoot() 0 11 1
A displayName() 0 3 1
A getPurgeUrl() 0 3 1
A purgeUrl() 0 3 1
A getTransformUrl() 0 3 1
A getWebPUrl() 0 3 1
A getAssetUri() 0 14 2
A appendExtension() 0 13 4
A decomposeUrl() 0 21 5
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\base\SavableComponent;
15
use craft\elements\Asset;
16
use craft\helpers\FileHelper;
17
use craft\helpers\StringHelper;
18
use craft\models\ImageTransform as CraftImageTransformModel;
19
use nystudio107\imageoptimize\helpers\UrlHelper;
20
use ReflectionClass;
21
22
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
23
 * @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...
24
 * @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...
25
 * @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...
26
 */
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...
27
abstract class ImageTransform extends SavableComponent implements ImageTransformInterface
28
{
29
    // Traits
30
    // =========================================================================
31
32
    use ImageTransformTrait;
33
34
    // Static Methods
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', 'Generic Transform');
43
    }
44
45
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
46
     * @inheritdoc
47
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
48
    public static function getTemplatesRoot(): array
49
    {
50
        $reflect = new ReflectionClass(static::class);
51
        $classPath = FileHelper::normalizePath(
52
                dirname($reflect->getFileName())
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 12 spaces, but found 16.
Loading history...
53
                . '/../templates'
54
            )
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 8 spaces, but found 12.
Loading history...
55
            . DIRECTORY_SEPARATOR;
56
        $id = StringHelper::toKebabCase($reflect->getShortName());
57
58
        return [$id, $classPath];
59
    }
60
61
    // Public Methods
62
    // =========================================================================
63
64
    /**
0 ignored issues
show
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...
Coding Style introduced by
Missing short description in doc comment
Loading history...
65
     * @inheritdoc
66
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
67
    public function getTransformUrl(Asset $asset, CraftImageTransformModel|string|array|null $transform): ?string
68
    {
69
        return null;
70
    }
71
72
    /**
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...
73
     * @inheritdoc
74
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
75
    public function getWebPUrl(string $url, Asset $asset, CraftImageTransformModel|string|array|null $transform): ?string
76
    {
77
        return $url;
78
    }
79
80
    /**
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...
81
     * @inheritdoc
82
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
83
    public function getPurgeUrl(Asset $asset): ?string
84
    {
85
        return null;
86
    }
87
88
    /**
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...
89
     * @inheritdoc
90
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
91
    public function purgeUrl(string $url): bool
92
    {
93
        return true;
94
    }
95
96
    /**
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...
97
     * @inheritdoc
98
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
99
    public function getAssetUri(Asset $asset): ?string
100
    {
101
        $volume = $asset->getVolume();
102
        $assetPath = $volume->getSubpath() . $asset->getPath();
103
104
        // Account for volume types with a subfolder setting
105
        // e.g. craftcms/aws-s3, craftcms/google-cloud
106
        if ($volume->getFs()->subfolder ?? null) {
107
            $subfolder = $volume->getFs()->subfolder;
108
            $subfolder = Craft::parseEnv($subfolder);
0 ignored issues
show
Deprecated Code introduced by
The function Craft::parseEnv() has been deprecated: in 3.7.29. [[App::parseEnv()]] should be used instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

108
            $subfolder = /** @scrutinizer ignore-deprecated */ Craft::parseEnv($subfolder);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
109
            return rtrim($subfolder, '/') . '/' . $assetPath;
0 ignored issues
show
Bug introduced by
It seems like $subfolder can also be of type null; however, parameter $string of rtrim() does only seem to accept string, 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

109
            return rtrim(/** @scrutinizer ignore-type */ $subfolder, '/') . '/' . $assetPath;
Loading history...
110
        }
111
112
        return $assetPath;
113
    }
114
115
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
116
     * @param string $url
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 8 spaces but found 1
Loading history...
117
     * @noinspection PhpComposerExtensionStubsInspection
0 ignored issues
show
Coding Style introduced by
Tag @noinspection cannot be grouped with parameter tags in a doc comment
Loading history...
118
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
119
    public function prefetchRemoteFile(string $url): void
120
    {
121
        // Get an absolute URL with protocol that curl will be happy with
122
        $url = UrlHelper::absoluteUrlWithProtocol($url);
123
        $ch = curl_init($url);
124
        curl_setopt_array($ch, [
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...
125
            CURLOPT_RETURNTRANSFER => 1,
126
            CURLOPT_FOLLOWLOCATION => 1,
127
            CURLOPT_SSL_VERIFYPEER => 0,
128
            CURLOPT_NOBODY => 1,
129
        ]);
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...
130
        curl_exec($ch);
131
        curl_close($ch);
132
    }
133
134
    /**
135
     * Append an extension a passed url or path
136
     *
137
     * @param $pathOrUrl
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
138
     * @param $extension
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
139
     *
140
     * @return string
141
     */
142
    public function appendExtension($pathOrUrl, $extension): string
143
    {
144
        $path = $this->decomposeUrl($pathOrUrl);
145
        $path_parts = pathinfo($path['path']);
146
        $new_path = ($path_parts['filename']) . '.' . ($path_parts['extension'] ?? '') . $extension;
147
        if (!empty($path_parts['dirname']) && $path_parts['dirname'] !== '.') {
148
            $dirname = $path_parts['dirname'];
149
            $dirname = $dirname === '/' ? '' : $dirname;
150
            $new_path = $dirname . DIRECTORY_SEPARATOR . $new_path;
151
            $new_path = preg_replace('/([^:])(\/{2,})/', '$1/', $new_path);
152
        }
153
154
        return $path['prefix'] . $new_path . $path['suffix'];
155
    }
156
157
    // Protected Methods
158
    // =========================================================================
159
160
    /**
161
     * Decompose a URL into a prefix, path, and suffix
162
     *
163
     * @param $pathOrUrl
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
164
     *
165
     * @return array
166
     */
167
    protected function decomposeUrl($pathOrUrl): array
168
    {
169
        $result = array();
170
171
        if (filter_var($pathOrUrl, FILTER_VALIDATE_URL)) {
172
            $url_parts = parse_url($pathOrUrl);
173
            $result['prefix'] = $url_parts['scheme'] . '://' . $url_parts['host'];
174
            if (!empty($url_parts['port'])) {
175
                $result['prefix'] .= ':' . $url_parts['port'];
176
            }
177
            $result['path'] = $url_parts['path'];
178
            $result['suffix'] = '';
179
            $result['suffix'] .= empty($url_parts['query']) ? '' : '?' . $url_parts['query'];
180
            $result['suffix'] .= empty($url_parts['fragment']) ? '' : '#' . $url_parts['fragment'];
181
        } else {
182
            $result['prefix'] = '';
183
            $result['path'] = $pathOrUrl;
184
            $result['suffix'] = '';
185
        }
186
187
        return $result;
188
    }
189
}
190