Completed
Push — master ( e17fc8...22dda8 )
by Felix
04:04
created

Url   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 9
c 1
b 0
f 1
lcom 1
cbo 0
dl 0
loc 69
ccs 17
cts 17
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 4 1
A part() 0 4 2
A __construct() 0 5 1
A has() 0 4 1
A decode() 0 6 1
A normalize() 0 8 3
1
<?php
2
3
namespace Felix\Scraper;
4
5
class Url
6
{
7
    /** @var string */
8
    protected $url;
9
10
    /** @var array */
11
    protected $parts;
12
13 4
    public function __construct($url)
14
    {
15 4
        $this->url = $url;
16 4
        $this->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...
17 4
    }
18
19 2
    public function __toString()
20
    {
21 2
        return $this->url;
22
    }
23
    
24
    /**
25
     * Obtener una parte de la URL.
26
     * 
27
     * @param $part string Parte de la URL (Ej: host).
28
     * 
29
     * @return string|null
30
     */
31 3
    public function part($part)
32
    {
33 3
        return array_key_exists($part, $this->parts) ? $this->parts[$part] : null;
34
    }
35
36
    /**
37
     * Url tiene una parte.
38
     * 
39
     * @return boolean true|false
40
     */
41 2
    public function has($part)
42
    {
43 2
        return $this->part($part) !== null;
44
    }
45
46
    /**
47
     *  Decodificación URL.
48
     * 
49
     * @return object
50
     */
51 1
    public function decode()
52
    {
53 1
        $this->url = urldecode($this->url);
54
        
55 1
        return $this;
56
    }
57
58
    /**
59
     * Dada una URL, normaliza esa URL.
60
     * 
61
     * @param $schemeAndHost string Esquema y dominio base (Ej. http://example.com)
62
     * 
63
     * @return string  
64
     */
65 1
    public function normalize($schemeAndHost)
66
    {
67 1
        if (!$this->has('host') || !$this->has('scheme')) {
68 1
            $this->url = $schemeAndHost.$this->url;
69
        }
70
71 1
        return $this;
72
    }
73
}