Passed
Push — master ( b083ab...a3db06 )
by Sebastian
03:30
created

URIFilter   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 8
c 0
b 0
f 0
dl 0
loc 27
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A filter() 0 25 3
1
<?php
2
/**
3
 * @package Application Utils
4
 * @subpackage URLInfo
5
 * @see \AppUtils\URLInfo\URIFilter
6
 */
7
8
declare(strict_types=1);
9
10
namespace AppUtils\URLInfo;
11
12
use AppUtils\ConvertHelper;
13
14
/**
15
 * Static class with utility methods to handle filtering
16
 * a URL string, by removing any unwanted special characters
17
 * and the like.
18
 *
19
 * @package Application Utils
20
 * @subpackage URLInfo
21
 * @author Sebastian Mordziol <[email protected]>
22
 */
23
class URIFilter
24
{
25
    public static function filter(string $url) : string
26
    {
27
        // fix ampersands if it comes from HTML
28
        $url = str_replace('&amp;', '&', $url);
29
30
        // In the case of tel URLs, we convert the syntax to use double
31
        // slashes to make them parsable.
32
        if (stripos($url, 'tel:') !== false && stripos($url, 'tel://') === false)
33
        {
34
            $url = str_replace('tel:', 'tel://', $url);
35
        }
36
37
        // we remove any control characters from the URL, since these
38
        // may be copied when copy+pasting from word or pdf documents
39
        // for example.
40
        $url = ConvertHelper::stripControlCharacters($url);
41
42
        // fix the pesky unicode hyphen that looks like a regular hyphen,
43
        // but isn't and can cause all sorts of problems
44
        $url = str_replace('‐', '-', $url);
45
46
        // remove url encoded and characters of newlines and tabs
47
        $url = str_replace(array("\n", "\r", "\t", '%0D', '%0A', '%09'), '', $url);
48
49
        return trim($url);
50
    }
51
}
52