Passed
Branch v3 (3b6795)
by Andrew
07:36
created

UrlHelper::combineQueryStringsFromUrls()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 12
ccs 0
cts 9
cp 0
rs 10
cc 2
nc 2
nop 1
crap 6
1
<?php
2
/**
3
 * Retour plugin for Craft CMS 3.x
4
 *
5
 * Retour allows you to intelligently redirect legacy URLs, so that you don't
6
 * lose SEO value when rebuilding & restructuring a website
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) 2020 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\retour\helpers;
13
14
use craft\helpers\UrlHelper as CraftUrlHelper;
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   Retour
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     3.1.34
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 UrlHelper extends CraftUrlHelper
22
{
23
    // Public Static Methods
24
    // =========================================================================
25
26
    /**
27
     * Returns a query string that is a combination of al of the query strings from
28
     * the passed in $urls
29
     *
30
     * @param ...$urls
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...
31
     * @return string
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
32
     */
33
    public static function combineQueryStringsFromUrls(...$urls): string
34
    {
35
        $queryParams = [];
36
        foreach ($urls as $url) {
37
            $parsedUrl = parse_url($url);
38
            $params = [];
39
            parse_str($parsedUrl['query'] ?? '', $params);
40
            $queryParams[] = $params;
41
        }
42
        $queryParams = array_unique(array_merge([], ...$queryParams));
43
44
        return http_build_query($queryParams);
45
    }
46
47
    /**
48
     * Return a sanitized URL
49
     *
50
     * @param string $url
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
51
     *
52
     * @return string
53
     */
54 1
    public static function sanitizeUrl(string $url): string
55
    {
56
        // HTML decode the entities, then strip out any tags
57 1
        $url = html_entity_decode($url, ENT_NOQUOTES, 'UTF-8');
58 1
        $url = urldecode($url);
59 1
        $url = strip_tags($url);
60
        // Remove any Twig tags that somehow are present in the incoming URL
61
        /** @noinspection CallableParameterUseCaseInTypeContextInspection */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
62 1
        $url = preg_replace('/{.*}/', '', $url);
63
        // Remove any linebreaks that may be errantly in the URL
64 1
        $url = (string)str_replace([
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...
65 1
                PHP_EOL,
66 1
                "\r",
67 1
                "\n",
68
            ]
69 1
            , '', $url
0 ignored issues
show
Coding Style introduced by
Space found before comma in argument list
Loading history...
70
        );
71
72 1
        return $url;
73
    }
74
}
75