Completed
Push — develop ( 46e6b4...ebb20f )
by Chuck
16s
created

Uri   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 96%

Importance

Changes 0
Metric Value
dl 0
loc 93
ccs 24
cts 25
cp 0.96
rs 10
c 0
b 0
f 0
wmc 10
lcom 0
cbo 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A __toString() 0 4 1
A equals() 0 4 1
A validateString() 0 6 2
A validateUri() 0 6 2
A addFileSchemeWhenSchemeIsAbsent() 0 12 3
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * This file is part of phpDocumentor.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @author    Mike van Riel <[email protected]>
11
 * @copyright 2010-2018 Mike van Riel / Naenius (http://www.naenius.com)
12
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
13
 * @link      http://phpdoc.org
14
 */
15
16
namespace phpDocumentor\DomainModel;
17
18
/**
19
 * Value object for uri.
20
 * Uri can be either local or remote.
21
 */
22
final class Uri
23
{
24
    /**
25
     * Uri path.
26
     *
27
     * @var string
28
     */
29
    private $uri;
30
31
    /**
32
     * Initializes the Uri.
33
     *
34
     * @param string $uri
35
     * @throws \InvalidArgumentException
36
     */
37 4
    public function __construct($uri)
38
    {
39 4
        $this->validateString($uri);
40
41 4
        $uri = $this->addFileSchemeWhenSchemeIsAbsent($uri);
42
43 4
        $this->validateUri($uri);
44
45 4
        $this->uri = $uri;
46 4
    }
47
48
    /**
49
     * Returns a string representation of the uri.
50
     *
51
     * @return string
52
     */
53 2
    public function __toString()
54
    {
55 2
        return $this->uri;
56
    }
57
58
    /**
59
     * Checks if the provided uri is equal to the current uri.
60
     *
61
     * @param Uri $other
62
     */
63 2
    public function equals($other): bool
64
    {
65 2
        return $other == $this;
66
    }
67
68
    /**
69
     * Checks if $uri is of type string.
70
     *
71
     * @param string $uri
72
     *
73
     * @throws \InvalidArgumentException if $uri is not a string.
74
     */
75 7
    private function validateString($uri)
76
    {
77 7
        if (!\is_string($uri)) {
78 1
            throw new \InvalidArgumentException(sprintf('String required, %s given', \gettype($uri)));
79
        }
80 6
    }
81
82
    /**
83
     * Checks if $uri is valid.
84
     *
85
     * @param string $uri
86
     *
87
     * @throws \InvalidArgumentException if $uri is not a valid uri.
88
     */
89 6
    private function validateUri($uri)
90
    {
91 6
        if (filter_var($uri, FILTER_VALIDATE_URL) === false) {
92 1
            throw new \InvalidArgumentException(sprintf('%s is not a valid uri', $uri));
93
        }
94 5
    }
95
96
    /**
97
     * Checks if a scheme is present.
98
     * If no scheme is found, it is assumed that a local path is used, and file:// is prepended.
99
     *
100
     * @param string $uri
101
     */
102 6
    private function addFileSchemeWhenSchemeIsAbsent($uri): string
103
    {
104 6
        $scheme = parse_url($uri, PHP_URL_SCHEME);
105
106 6
        if (empty($scheme)) {
107 4
            $uri = 'file://' . $uri;
108 2
        } elseif (preg_match('/^[a-z]$/i', (string) $scheme)) { // windows driver letter
109
            $uri = 'file:///' . $uri;
110
        }
111
112 6
        return $uri;
113
    }
114
}
115