Completed
Push — master ( 5d4822...d1d02b )
by Maik
01:58
created

UrlParser   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getPortByScheme() 0 18 4
B parseUrl() 0 30 6
1
<?php
2
3
/**
4
 * This file is part of the PHP Generics package.
5
 *
6
 * @package Generics
7
 */
8
namespace Generics\Util;
9
10
use Generics\Socket\InvalidUrlException;
11
use Generics\Socket\Url;
12
13
/**
14
 * This class provides a parser to retrieve Url objects out of arbitrary URIs
15
 *
16
 * @author Maik Greubel <[email protected]>
17
 */
18
class UrlParser
19
{
20
21
    /**
22
     * Parse a URI into a Url
23
     *
24
     * @param string $url
25
     * @throws InvalidUrlException
26
     * @return \Generics\Socket\Url
27
     */
28 21
    public static function parseUrl($url)
29
    {
30 21
        $parts = parse_url($url);
31
32 21
        if (! Arrays::hasElement($parts, 'host') ) {
0 ignored issues
show
Security Bug introduced by
It seems like $parts defined by parse_url($url) on line 30 can also be of type false; however, Generics\Util\Arrays::hasElement() does only seem to accept array, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
33 17
            throw new InvalidUrlException('This URL does not contain a host part');
34
        }
35 14
        if (! Arrays::hasElement($parts, 'scheme') ) {
0 ignored issues
show
Security Bug introduced by
It seems like $parts defined by parse_url($url) on line 30 can also be of type false; however, Generics\Util\Arrays::hasElement() does only seem to accept array, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
36 1
            throw new InvalidUrlException('This URL does not contain a scheme part');
37
        }
38
39 13
        $address = $parts['host'];
40 13
        $scheme = $parts['scheme'];
41 13
        $port = 0;
42 13
        $path = "/";
43
44 13
        if (isset($parts['port'])) {
45 2
            $port = intval($parts['port']);
46
        }
47
48 13
        if ($port == 0) {
49 11
            $port = self::getPortByScheme($scheme);
50
        }
51
52 11
        if (isset($parts['path'])) {
53 10
            $path = $parts['path'];
54
        }
55
56 11
        return new Url($address, $port, $path, $scheme);
57
    }
58
59
    /**
60
     * Get port number by scheme name.
61
     * The port will be the default which is defined by
62
     * http://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers
63
     *
64
     * @param string $scheme The scheme.
65
     * @throws InvalidUrlException
66
     * @return int
67
     */
68 11
    public static function getPortByScheme($scheme)
69
    {
70
        switch ($scheme) {
71 11
            case 'http':
72 5
                return 80;
73
74 6
            case 'https':
75 2
                return 443;
76
77 4
            case 'ftp':
78 2
                return 21;
79
80
            default:
81 2
                throw new InvalidUrlException("Scheme {scheme} is not handled!", array(
82 2
                    'scheme' => $scheme
83
                ));
84
        }
85
    }
86
}
87