Completed
Push — master ( d1d02b...e3e5bf )
by Maik
02:45
created

UrlParser::parseUrl()   C

Complexity

Conditions 8
Paths 10

Size

Total Lines 29
Code Lines 17

Duplication

Lines 6
Ratio 20.69 %

Code Coverage

Tests 17
CRAP Score 8

Importance

Changes 4
Bugs 0 Features 0
Metric Value
dl 6
loc 29
ccs 17
cts 17
cp 1
rs 5.3846
c 4
b 0
f 0
cc 8
eloc 17
nc 10
nop 1
crap 8
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
	 * Parse a URI into a Url
22
	 *
23
	 * @param string $url
24
	 * @throws InvalidUrlException
25
	 * @return \Generics\Socket\Url
26
	 */
27 17
	public static function parseUrl($url) {
28 17
		$parts = parse_url ( $url );
29
		
30 17 View Code Duplication
		if (false === $parts || false === Arrays::hasElement ( $parts, 'host' )) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
31 14
			throw new InvalidUrlException ( 'This URL does not contain a host part' );
32
		}
33 14 View Code Duplication
		if (false === $parts || false === Arrays::hasElement ( $parts, 'scheme' )) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34 1
			throw new InvalidUrlException ( 'This URL does not contain a scheme part' );
35
		}
36
		
37 13
		$address = $parts ['host'];
38 13
		$scheme = $parts ['scheme'];
39 13
		$port = 0;
40 13
		$path = "/";
41
		
42 13
		if (isset ( $parts ['port'] )) {
43 2
			$port = intval ( $parts ['port'] );
44
		}
45
		
46 13
		if ($port == 0) {
47 11
			$port = self::getPortByScheme ( $scheme );
48
		}
49
		
50 11
		if (isset ( $parts ['path'] )) {
51 10
			$path = $parts ['path'];
52
		}
53
		
54 11
		return new Url ( $address, $port, $path, $scheme );
55
	}
56
	
57
	/**
58
	 * Get port number by scheme name.
59
	 * The port will be the default which is defined by
60
	 * http://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers
61
	 *
62
	 * @param string $scheme
63
	 *        	The scheme.
64
	 * @throws InvalidUrlException
65
	 * @return int
66
	 */
67 11
	public static function getPortByScheme($scheme) {
68
		switch ($scheme) {
69 11
			case 'http' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
70 5
				return 80;
71
			
72 6
			case 'https' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
73 2
				return 443;
74
			
75 4
			case 'ftp' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
76 2
				return 21;
77
			
78
			default :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a DEFAULT statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in the default statement.

switch ($expr) {
    default : //wrong
        doSomething();
        break;
}

switch ($expr) {
    default: //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
79 2
				throw new InvalidUrlException ( "Scheme {scheme} is not handled!", array (
80 2
						'scheme' => $scheme 
81
				) );
82
		}
83
	}
84
}
85