Completed
Push — master ( da5777...ed8552 )
by Lars
13:41
created

Parser   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 96.3%

Importance

Changes 10
Bugs 0 Features 3
Metric Value
wmc 11
c 10
b 0
f 3
lcom 1
cbo 2
dl 0
loc 90
ccs 26
cts 27
cp 0.963
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
C parseUrl() 0 27 7
A doParseUrl() 0 17 2
1
<?php
2
3
/*
4
 * This file is part of the Purl package, a project by Jonathan H. Wage.
5
 *
6
 * (c) 2013 Jonathan H. Wage
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Purl;
13
14
use Pdp\Parser as PslParser;
15
16
/**
17
 * Parser class.
18
 *
19
 * @author      Jonathan H. Wage <[email protected]>
20
 */
21
class Parser implements ParserInterface
22
{
23
24
    /**
25
     * @var PslParser Public Suffix List parser
26
     */
27
    private static $pslParser = null;
28
29
    private static $defaultParts = array(
30
        'scheme'             => null,
31
        'host'               => null,
32
        'port'               => null,
33
        'user'               => null,
34
        'pass'               => null,
35
        'path'               => null,
36
        'query'              => null,
37
        'fragment'           => null,
38
        'publicSuffix'       => null,
39
        'registerableDomain' => null,
40
        'subdomain'          => null,
41
        'canonical'          => null,
42
        'resource'           => null
43
    );
44
45
    /**
46
     * Public constructor
47
     *
48
     * @param PslParser $pslParser Public Suffix List parser
49
     */
50 61
    public function __construct(PslParser $pslParser)
51
    {
52 61
        if (self::$pslParser === null) {
53 1
            self::$pslParser = $pslParser;
54 1
        }
55 61
    }
56
57
    /**
58
     * @inheritDoc
59
     */
60 61
    public function parseUrl($url)
61
    {
62 61
        $url = (string) $url;
63
64 61
        $parsedUrl = $this->doParseUrl($url);
65
66 60
        if ($parsedUrl === false) {
67
            throw new \InvalidArgumentException(sprintf('Invalid url %s', $url));
68
        }
69
70 60
        $parsedUrl = array_merge(self::$defaultParts, $parsedUrl);
71
72 60
        if (isset($parsedUrl['host'])) {
73 59
            $parsedUrl['publicSuffix'] = self::$pslParser->getPublicSuffix($parsedUrl['host']);
74 59
            $parsedUrl['registerableDomain'] = self::$pslParser->getRegistrableDomain($parsedUrl['host']);
75 59
            $parsedUrl['subdomain'] = self::$pslParser->getSubdomain($parsedUrl['host']);
76 59
            $parsedUrl['canonical'] = implode('.', array_reverse(explode('.', $parsedUrl['host']))).(isset($parsedUrl['path']) ? $parsedUrl['path'] : '').(isset($parsedUrl['query']) ? '?'.$parsedUrl['query'] : '');
77
78 59
            $parsedUrl['resource'] = isset($parsedUrl['path']) ? $parsedUrl['path'] : '';
79
80 59
            if (isset($parsedUrl['query'])) {
81 6
                $parsedUrl['resource'] .= '?'.$parsedUrl['query'];
82 6
            }
83 59
        }
84
85 60
        return $parsedUrl;
86
    }
87
88
    /**
89
     * @param string $url
90
     *
91
     * @return array $parsedUrl
92
     */
93 61
    protected function doParseUrl($url)
94
    {
95
        // If there's a single leading forward slash, use parse_url()
96
        // Expected matches:
97
        //
98
        // "/one/two"   YES
99
        // "/"          YES PLEASE
100
        // "//"         NO
101
        // "//one/two"  NO
102
        // ""           HELL NO
103 61
        if (preg_match('#^[\/]([^\/]|$)#', $url) === 1) {
104 2
            return parse_url($url);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression parse_url($url); of type array<string,string>|false adds false to the return on line 104 which is incompatible with the return type documented by Purl\Parser::doParseUrl of type array. It seems like you forgot to handle an error condition.
Loading history...
105
        } else {
106
            // Otherwise use the PSL parser
107 60
            return self::$pslParser->parseUrl($url)->toArray();
108
        }
109 1
    }
110
}
111