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

Url::hasScheme()   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 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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
}