PathToolsTwigExtension::parseUrlFilter()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
/**
3
 * PathTools plugin for Craft CMS 3.x
4
 *
5
 * This twig plugin for the Craft CMS brings convenient path & url manipulation functions & filters to your
6
 * Twig templates.
7
 *
8
 * @link      https://nystudio107.com
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @copyright tag
Loading history...
9
 * @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...
10
 */
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...
11
12
namespace nystudio107\pathtools\twigextensions;
13
14
use Craft;
15
16
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
17
 * @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...
18
 * @package   PathTools
0 ignored issues
show
Coding Style introduced by
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
19
 * @since     1.0.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...
20
 */
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...
21
class PathToolsTwigExtension extends \Twig_Extension
0 ignored issues
show
Deprecated Code introduced by
The class Twig_Extension has been deprecated: since Twig 2.7, use "Twig\Extension\AbstractExtension" instead ( Ignorable by Annotation )

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

21
class PathToolsTwigExtension extends /** @scrutinizer ignore-deprecated */ \Twig_Extension
Loading history...
22
{
23
    // Public Methods
24
    // =========================================================================
25
26
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
27
     * @inheritdoc
28
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
29
    public function getName()
30
    {
31
        return 'PathTools';
32
    }
33
34
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
35
     * @inheritdoc
36
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
37
    public function getFilters()
38
    {
39
        return [
40
            new \Twig_SimpleFilter('pathinfo', [$this, 'pathInfoFilter']),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFilter has been deprecated: since Twig 2.7, use "Twig\TwigFilter" instead ( Ignorable by Annotation )

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

40
            /** @scrutinizer ignore-deprecated */ new \Twig_SimpleFilter('pathinfo', [$this, 'pathInfoFilter']),
Loading history...
41
            new \Twig_SimpleFilter('basename', [$this, 'baseNameFilter']),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFilter has been deprecated: since Twig 2.7, use "Twig\TwigFilter" instead ( Ignorable by Annotation )

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

41
            /** @scrutinizer ignore-deprecated */ new \Twig_SimpleFilter('basename', [$this, 'baseNameFilter']),
Loading history...
42
            new \Twig_SimpleFilter('dirname', [$this, 'dirNameFilter']),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFilter has been deprecated: since Twig 2.7, use "Twig\TwigFilter" instead ( Ignorable by Annotation )

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

42
            /** @scrutinizer ignore-deprecated */ new \Twig_SimpleFilter('dirname', [$this, 'dirNameFilter']),
Loading history...
43
            new \Twig_SimpleFilter('parse_url', [$this, 'parseUrlFilter']),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFilter has been deprecated: since Twig 2.7, use "Twig\TwigFilter" instead ( Ignorable by Annotation )

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

43
            /** @scrutinizer ignore-deprecated */ new \Twig_SimpleFilter('parse_url', [$this, 'parseUrlFilter']),
Loading history...
44
            new \Twig_SimpleFilter('parse_string', [$this, 'parseStringFilter']),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFilter has been deprecated: since Twig 2.7, use "Twig\TwigFilter" instead ( Ignorable by Annotation )

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

44
            /** @scrutinizer ignore-deprecated */ new \Twig_SimpleFilter('parse_string', [$this, 'parseStringFilter']),
Loading history...
45
            new \Twig_SimpleFilter('swap_extension', [$this, 'swapExtensionFilter']),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFilter has been deprecated: since Twig 2.7, use "Twig\TwigFilter" instead ( Ignorable by Annotation )

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

45
            /** @scrutinizer ignore-deprecated */ new \Twig_SimpleFilter('swap_extension', [$this, 'swapExtensionFilter']),
Loading history...
46
            new \Twig_SimpleFilter('swap_directory', [$this, 'swapDirectoryFilter']),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFilter has been deprecated: since Twig 2.7, use "Twig\TwigFilter" instead ( Ignorable by Annotation )

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

46
            /** @scrutinizer ignore-deprecated */ new \Twig_SimpleFilter('swap_directory', [$this, 'swapDirectoryFilter']),
Loading history...
47
            new \Twig_SimpleFilter('append_suffix', [$this, 'appendSuffixFilter']),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFilter has been deprecated: since Twig 2.7, use "Twig\TwigFilter" instead ( Ignorable by Annotation )

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

47
            /** @scrutinizer ignore-deprecated */ new \Twig_SimpleFilter('append_suffix', [$this, 'appendSuffixFilter']),
Loading history...
48
        ];
49
    }
50
51
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
52
     * @inheritdoc
53
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
54
    public function getFunctions()
55
    {
56
        return [
57
            new \Twig_SimpleFunction('pathinfo', [$this, 'pathInfoFilter']),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated: since Twig 2.7, use "Twig\TwigFunction" instead ( Ignorable by Annotation )

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

57
            /** @scrutinizer ignore-deprecated */ new \Twig_SimpleFunction('pathinfo', [$this, 'pathInfoFilter']),
Loading history...
58
            new \Twig_SimpleFunction('basename', [$this, 'baseNameFilter']),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated: since Twig 2.7, use "Twig\TwigFunction" instead ( Ignorable by Annotation )

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

58
            /** @scrutinizer ignore-deprecated */ new \Twig_SimpleFunction('basename', [$this, 'baseNameFilter']),
Loading history...
59
            new \Twig_SimpleFunction('dirname', [$this, 'dirNameFilter']),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated: since Twig 2.7, use "Twig\TwigFunction" instead ( Ignorable by Annotation )

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

59
            /** @scrutinizer ignore-deprecated */ new \Twig_SimpleFunction('dirname', [$this, 'dirNameFilter']),
Loading history...
60
            new \Twig_SimpleFunction('parse_url', [$this, 'parseUrlFilter']),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated: since Twig 2.7, use "Twig\TwigFunction" instead ( Ignorable by Annotation )

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

60
            /** @scrutinizer ignore-deprecated */ new \Twig_SimpleFunction('parse_url', [$this, 'parseUrlFilter']),
Loading history...
61
            new \Twig_SimpleFunction('parse_string', [$this, 'parseStringFilter']),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated: since Twig 2.7, use "Twig\TwigFunction" instead ( Ignorable by Annotation )

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

61
            /** @scrutinizer ignore-deprecated */ new \Twig_SimpleFunction('parse_string', [$this, 'parseStringFilter']),
Loading history...
62
            new \Twig_SimpleFunction('swap_extension', [$this, 'swapExtensionFilter']),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated: since Twig 2.7, use "Twig\TwigFunction" instead ( Ignorable by Annotation )

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

62
            /** @scrutinizer ignore-deprecated */ new \Twig_SimpleFunction('swap_extension', [$this, 'swapExtensionFilter']),
Loading history...
63
            new \Twig_SimpleFunction('swap_directory', [$this, 'swapDirectoryFilter']),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated: since Twig 2.7, use "Twig\TwigFunction" instead ( Ignorable by Annotation )

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

63
            /** @scrutinizer ignore-deprecated */ new \Twig_SimpleFunction('swap_directory', [$this, 'swapDirectoryFilter']),
Loading history...
64
            new \Twig_SimpleFunction('append_suffix', [$this, 'appendSuffixFilter']),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated: since Twig 2.7, use "Twig\TwigFunction" instead ( Ignorable by Annotation )

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

64
            /** @scrutinizer ignore-deprecated */ new \Twig_SimpleFunction('append_suffix', [$this, 'appendSuffixFilter']),
Loading history...
65
        ];
66
    }
67
68
69
    /**
70
     * php pathinfo() wrapper -- http://php.net/manual/en/function.pathinfo.php
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
71
     *
72
     * @param $path
0 ignored issues
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
73
     * @param bool $options
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...
74
     * @return mixed
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
75
     */
76
    public function pathInfoFilter($path, $options = false)
77
    {
78
        if ($options) {
79
            $output = pathinfo($path, $options);
0 ignored issues
show
Bug introduced by
$options of type true is incompatible with the type integer expected by parameter $flags of pathinfo(). ( Ignorable by Annotation )

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

79
            $output = pathinfo($path, /** @scrutinizer ignore-type */ $options);
Loading history...
80
        } else {
81
            $output = pathinfo($path);
82
        }
83
        return $output;
84
    }
85
86
    /**
87
     * php basename() wrapper -- http://php.net/manual/en/function.basename.php
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
88
     *
89
     * @param $path
0 ignored issues
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
90
     * @param bool $suffix
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...
91
     * @return string
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
92
     */
93
    public function baseNameFilter($path, $suffix = false)
94
    {
95
        if ($suffix) {
96
            $output = basename($path, $suffix);
0 ignored issues
show
Bug introduced by
$suffix of type true is incompatible with the type string expected by parameter $suffix of basename(). ( Ignorable by Annotation )

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

96
            $output = basename($path, /** @scrutinizer ignore-type */ $suffix);
Loading history...
97
        } else {
98
            $output = basename($path);
99
        }
100
        return $output;
101
    }
102
103
    /**
104
     * php dirname() wrapper -- http://php.net/manual/en/function.dirname.php
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
105
     *
106
     * @param $path
0 ignored issues
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
107
     * @return string
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
108
     */
109
    public function dirNameFilter($path)
110
    {
111
        $output = dirname($path);
112
        return $output;
113
    }
114
115
    /**
116
     * php parse_url() wrapper -- http://php.net/manual/en/function.parse-url.php
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
117
     *
118
     * @param $url
0 ignored issues
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
119
     * @param int $component
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...
120
     * @return mixed
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
121
     */
122
    public function parseUrlFilter($url, $component = -1)
123
    {
124
        $output = parse_url($url, $component);
125
        return $output;
126
    }
127
128
    /**
129
     * php parse_str() wrapper -- http://php.net/manual/en/function.parse-str.php
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
130
     *
131
     * @param $string
0 ignored issues
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
132
     * @return mixed
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
133
     */
134
    public function parseStringFilter($string)
135
    {
136
        parse_str($string, $output);
137
        return $output;
138
    }
139
140
    /**
141
     * Swap the file extension on a passed url or path
142
     *
143
     * @param $path_or_url
0 ignored issues
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
144
     * @param $extension
0 ignored issues
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
145
     * @return string
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
146
     */
147
    public function swapExtensionFilter($path_or_url, $extension)
148
    {
149
        $path = $this->decomposeUrl($path_or_url);
150
        $path_parts = pathinfo($path['path']);
151
        $new_path = $path_parts['filename'] . "." . $extension;
152
        if (!empty($path_parts['dirname']) && $path_parts['dirname'] !== ".") {
153
            $new_path = $path_parts['dirname'] . DIRECTORY_SEPARATOR . $new_path;
154
            $new_path = preg_replace('#/+#', '/', $new_path);
155
        }
156
        $output = $path['prefix'] . $new_path . $path['suffix'];
157
        return $output;
158
    }
159
160
    /**
161
     * Swap the file directory on a passed url or path
162
     *
163
     * @param $path_or_url
0 ignored issues
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
164
     * @param $directory
0 ignored issues
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
165
     * @return string
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
166
     */
167
    public function swapDirectoryFilter($path_or_url, $directory)
168
    {
169
170
        $path = $this->decomposeUrl($path_or_url);
171
        $path_parts = pathinfo($path['path']);
172
        $new_path = $directory . DIRECTORY_SEPARATOR . $path_parts['basename'];
173
174
        $output = $path['prefix'] . $new_path . $path['suffix'];
175
        return $output;
176
    }
177
178
179
    /**
180
     * Append a suffix a passed url or path
181
     *
182
     * @param $path_or_url
0 ignored issues
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
183
     * @param $suffix
0 ignored issues
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
184
     * @return string
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
185
     */
186
    public function appendSuffixFilter($path_or_url, $suffix)
187
    {
188
        $path = $this->decomposeUrl($path_or_url);
189
        $path_parts = pathinfo($path['path']);
190
        $new_path = $path_parts['filename'] . $suffix . "." . $path_parts['extension'];
191
        if (!empty($path_parts['dirname']) && $path_parts['dirname'] !== ".") {
192
            $new_path = $path_parts['dirname'] . DIRECTORY_SEPARATOR . $new_path;
193
            $new_path = preg_replace('#/+#', '/', $new_path);
194
        }
195
        $output = $path['prefix'] . $new_path . $path['suffix'];
196
        return $output;
197
    }
198
199
    // Private Methods
200
    // =========================================================================
201
202
    /**
203
     * Decompose a url into a prefix, path, and suffix
204
     *
205
     * @param $path_or_url
0 ignored issues
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
206
     * @return array
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
207
     */
208
    private function decomposeUrl($path_or_url)
0 ignored issues
show
Coding Style introduced by
Private method name "PathToolsTwigExtension::decomposeUrl" must be prefixed with an underscore
Loading history...
209
    {
210
        $result = array();
211
212
        if (filter_var($path_or_url, FILTER_VALIDATE_URL)) {
213
            $url_parts = parse_url($path_or_url);
214
            $result['prefix'] = $url_parts['scheme'] . "://" . $url_parts['host'];
215
            $result['path'] = $url_parts['path'];
216
            $result['suffix'] = "";
217
            $result['suffix'] .= (empty($url_parts['query'])) ? "" : "?" . $url_parts['query'];
218
            $result['suffix'] .= (empty($url_parts['fragment'])) ? "" : "#" . $url_parts['fragment'];
219
        } else {
220
            $result['prefix'] = "";
221
            $result['path'] = $path_or_url;
222
            $result['suffix'] = "";
223
        }
224
225
        return $result;
226
    }
227
}
228