Completed
Push — master ( 127df7...bbb152 )
by Felix
02:15
created

Url::decodeUnreserved()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Felix\Scraper;
4
5
class Url
6
{
7
    /** @var array */
8
    private static $parts;
9
10
    /**
11
     * Obtener una parte de la URL.
12
     *
13
     * @param $part string Parte de la URL (Ej: host).
14
     *
15
     * @return string|null
16
     */
17 1
    public static function part($part)
18
    {
19 1
        return array_key_exists($part, self::$parts) ? self::$parts[$part] : null;
20
    }
21
22
    /**
23
     * Url tiene una parte.
24
     *
25
     * @return bool true|false
26
     */
27
    public static function has($part)
28
    {
29
        return self::part($part) !== null;
30
    }
31
32
    /**
33
     * Agregar host a url.
34
     *
35
     * @param $url string Url
36
     * @param $host string Dominio
37
     *
38
     * @return string
39
     */
40 1
    public static function addHost($url, $host)
41
    {
42 1
        return rtrim($host, '/') . $url;
43
    }
44
45
    /**
46
     * Agregar http a url.
47
     *
48
     * @param $url string Url
49
     * @param $scheme string Esquema
50
     *
51
     * @return string $url
52
     */
53 1
    public static function addScheme($url, $scheme = 'http://')
54
    {
55 1
        return $scheme . ltrim($url, '/');
56
    }
57
58
    /**
59
     * Decode unreserved characters
60
     *
61
     * @param $url string Url.
62
     *
63
     * @return string
64
     */
65 1
    public static function encode($url)
66
    {
67 1
        $components = explode("/", $url);
68 1
        $end = array_pop($components);
69 1
        array_push($components, rawurlencode($end));
70 1
        $url = implode("/", $components);
71
72 1
        return $url;
73
    }
74
75
    /**
76
     * Normalizar URL.
77
     *
78
     * @param $url string Url a normalizar.
79
     * @param $schemeAndHost string Esquema y dominio base (Ej. http://example.com)
80
     *
81
     * @return string
82
     */
83 1
    public static function normalize($url, $host = null)
84
    {
85 1
        self::$parts = parse_url($url);
0 ignored issues
show
Documentation Bug introduced by
It seems like parse_url($url) can also be of type false. However, the property $parts is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
86
87 1
        if (self::$parts === false) {
88
            throw new \Exception($url.' es una URL mal formada y no se puede procesar');
89
        }
90
91 1
        if (! self::part('host') && $host !== null) {
0 ignored issues
show
Bug Best Practice introduced by
The expression self::part('host') of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
92 1
            $url = self::addHost($url, $host);
93
        }
94
95 1
        if (! self::part('scheme') && self::part('host')) {
0 ignored issues
show
Bug Best Practice introduced by
The expression self::part('scheme') of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
Bug Best Practice introduced by
The expression self::part('host') of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
96 1
            $url = self::addScheme($url);
97
        }
98
99 1
        $url = self::encode($url);
100
        
101 1
        return $url;
102
    }
103
}
104