Passed
Push — v3 ( e153f6...4a04ce )
by Andrew
14:32 queued 14s
created

UrlHelper::mergeUrlWithPath()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 10
dl 0
loc 15
ccs 0
cts 11
cp 0
rs 9.9332
c 1
b 1
f 0
cc 4
nc 3
nop 2
crap 20
1
<?php
2
/**
3
 * Retour plugin for Craft CMS
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), SORT_REGULAR);
43
44
        return http_build_query($queryParams);
45
    }
46
47
    /**
48
     * Merge the $url and $path together, combining any overlapping path segments
49
     *
50
     * @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 2 spaces but found 1
Loading history...
51
     * @param string $path
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...
52
     * @return string
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
53
     */
54
    public static function mergeUrlWithPath(string $url, string $path): string
55
    {
56
        $overlap = 0;
57
        $urlOffset = strlen($url);
58
        $pathLength = strlen($path);
59
        $pathOffset = 0;
60
        while ($urlOffset > 0 && $pathOffset < $pathLength) {
61
            $urlOffset--;
62
            $pathOffset++;
63
            if (str_starts_with($path, substr($url, $urlOffset, $pathOffset))) {
64
                $overlap = $pathOffset;
65
            }
66
        }
67
68
        return rtrim($url, '/') . '/' . ltrim(substr($path, $overlap), '/');
69
    }
70
71
    /**
72
     * Return a sanitized URL
73
     *
74
     * @param string $url
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
75
     *
76
     * @return string
77
     */
78 1
    public static function sanitizeUrl(string $url): string
79
    {
80
        // HTML decode the entities, then strip out any tags
81 1
        $url = html_entity_decode($url, ENT_NOQUOTES, 'UTF-8');
82 1
        $url = urldecode($url);
83 1
        $url = strip_tags($url);
84
        // Remove any Twig tags that somehow are present in the incoming URL
85
        /** @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...
86 1
        $url = preg_replace('/{.*}/', '', $url);
87
        // Remove any linebreaks that may be errantly in the URL
88 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...
89 1
                PHP_EOL,
90 1
                "\r",
91 1
                "\n",
92 1
            ]
93 1
            , '', $url
0 ignored issues
show
Coding Style introduced by
Space found before comma in argument list
Loading history...
94 1
        );
95
96 1
        return $url;
97
    }
98
}
99