Passed
Push — develop ( ba8028...b93325 )
by Andrew
11:03
created

ImageTransform::getTransformParams()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * ImageOptimize 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) 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 nystudio107\imageoptimize\helpers\UrlHelper;
14
15
use craft\base\SavableComponent;
16
use craft\elements\Asset;
17
use craft\helpers\FileHelper;
18
use craft\helpers\StringHelper;
19
use craft\models\AssetTransform;
20
21
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
22
 * @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...
23
 * @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...
24
 * @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...
25
 */
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...
26
abstract class ImageTransform extends SavableComponent implements ImageTransformInterface
27
{
28
    // Traits
29
    // =========================================================================
30
31
    use ImageTransformTrait;
32
33
    // Static Methods
34
    // =========================================================================
35
36
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
37
     * @inheritdoc
38
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
39
    public static function displayName(): string
40
    {
41
        return Craft::t('image-optimize', 'Generic Transform');
42
    }
43
44
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
45
     * @inheritdoc
46
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
47
    public static function getTemplatesRoot(): array
48
    {
49
        $reflect = new \ReflectionClass(static::class);
50
        $classPath = FileHelper::normalizePath(
51
            dirname($reflect->getFileName())
52
            . '/../templates'
53
        )
54
        . DIRECTORY_SEPARATOR;
55
        $id = StringHelper::toKebabCase($reflect->getShortName());
56
57
        return [$id, $classPath];
58
    }
59
60
    // Public Methods
61
    // =========================================================================
62
63
    /**
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...
64
     * @inheritdoc
65
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
66
    public function getTransformUrl(Asset $asset, $transform)
67
    {
68
        $url = null;
69
70
        return $url;
71
    }
72
73
    /**
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...
74
     * @inheritdoc
75
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
76
    public function getWebPUrl(string $url, Asset $asset, $transform): string
77
    {
78
        return $url;
79
    }
80
81
    /**
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...
82
     * @inheritdoc
83
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
84
    public function getPurgeUrl(Asset $asset)
85
    {
86
        $url = null;
87
88
        return $url;
89
    }
90
91
    /**
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...
92
     * @inheritdoc
93
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
94
    public function purgeUrl(string $url): bool
95
    {
96
        return true;
97
    }
98
99
    /**
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...
100
     * @inheritdoc
101
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
102
    public function getAssetUri(Asset $asset)
103
    {
104
        $volume = $asset->getVolume();
105
        $assetPath = $asset->getPath();
106
107
        // Account for volume types with a subfolder setting
108
        // e.g. craftcms/aws-s3, craftcms/google-cloud
109
        if ($volume->subfolder ?? null) {
0 ignored issues
show
Bug introduced by
Accessing subfolder on the interface craft\base\VolumeInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
110
            return rtrim($volume->subfolder, '/').'/'.$assetPath;
111
        }
112
113
        return $assetPath;
114
    }
115
116
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
117
     * @param string $url
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
118
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
119
    public function prefetchRemoteFile($url)
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...
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_setopt_array() does only seem to accept resource, 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

124
        curl_setopt_array(/** @scrutinizer ignore-type */ $ch, [
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);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_exec() does only seem to accept resource, 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

130
        curl_exec(/** @scrutinizer ignore-type */ $ch);
Loading history...
131
        curl_close($ch);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_close() does only seem to accept resource, 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

131
        curl_close(/** @scrutinizer ignore-type */ $ch);
Loading history...
132
    }
133
134
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $pathOrUrl should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $extension should have a doc-comment as per coding-style.
Loading history...
135
     * Append an extension a passed url or path
136
     *
137
     * @param $pathOrUrl
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter name
Loading history...
138
     * @param $extension
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter name
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
            $new_path = $path_parts['dirname'] . DIRECTORY_SEPARATOR . $new_path;
149
            $new_path = preg_replace('/([^:])(\/{2,})/', '$1/', $new_path);
150
        }
151
        $output = $path['prefix'] . $new_path . $path['suffix'];
152
153
        return $output;
154
    }
155
156
    // Protected Methods
157
    // =========================================================================
158
159
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $pathOrUrl should have a doc-comment as per coding-style.
Loading history...
160
     * Decompose a url into a prefix, path, and suffix
161
     *
162
     * @param $pathOrUrl
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter name
Loading history...
163
     *
164
     * @return array
165
     */
166
    protected function decomposeUrl($pathOrUrl): array
167
    {
168
        $result = array();
169
170
        if (filter_var($pathOrUrl, FILTER_VALIDATE_URL)) {
171
            $url_parts = parse_url($pathOrUrl);
172
            $result['prefix'] = $url_parts['scheme'] . '://' . $url_parts['host'];
173
            $result['path'] = $url_parts['path'];
174
            $result['suffix'] = '';
175
            $result['suffix'] .= empty($url_parts['query']) ? '' : '?' . $url_parts['query'];
176
            $result['suffix'] .= empty($url_parts['fragment']) ? '' : '#' . $url_parts['fragment'];
177
        } else {
178
            $result['prefix'] = '';
179
            $result['path'] = $pathOrUrl;
180
            $result['suffix'] = '';
181
        }
182
183
        return $result;
184
    }
185
}
186