Passed
Push — master ( 7e45f3...e280fd )
by Pol
02:20
created

Path   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 178
Duplicated Lines 0 %

Test Coverage

Coverage 98.21%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 46
c 5
b 0
f 0
dl 0
loc 178
ccs 55
cts 56
cp 0.9821
rs 10
wmc 25

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 9 3
A basename() 0 3 2
A dirname() 0 3 2
A isAbsolute() 0 3 1
A fromString() 0 22 3
A getFragments() 0 9 2
A shift() 0 7 2
A isValid() 0 7 2
A getFirstPart() 0 7 2
A isRoot() 0 3 1
A withScheme() 0 7 1
A getLastPart() 0 7 2
A getScheme() 0 3 1
A getIterator() 0 3 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace drupol\phpvfs\Utils;
6
7
class Path implements PathInterface, \IteratorAggregate
8
{
9
    /**
10
     * @var bool
11
     */
12
    private $absolute;
13
14
    /**
15
     * @var string[]
16
     */
17
    private $fragments;
18
19
    /**
20
     * @var string
21
     */
22
    private $scheme = '';
23
24
    /**
25
     * {@inheritdoc}
26
     */
27 7
    public function __toString()
28
    {
29 7
        if ('' !== $this->getScheme()) {
30 1
            $root = $this->getScheme() . '://';
31
        } else {
32 7
            $root = $this->isAbsolute() ? '/' : '';
33
        }
34
35 7
        return $root . \implode('/', $this->getFragments());
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 17
    public function basename(): string
42
    {
43 17
        return \basename(($this->isAbsolute() ? '/' : '') . \implode('/', $this->getFragments()));
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 16
    public function dirname(): string
50
    {
51 16
        return \dirname(($this->isAbsolute() ? '/' : '') . \implode('/', $this->getFragments()));
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 27
    public static function fromString(string $id): Path
58
    {
59 27
        $instance = new self();
60
61 27
        if (false !== $parsed = \parse_url($id)) {
62 26
            if (\array_key_exists('scheme', $parsed)) {
63 7
                $instance->scheme = $parsed['scheme'];
64
65 7
                $id = \DIRECTORY_SEPARATOR . $parsed['host'] . $parsed['path'];
66
            }
67
        }
68
69 27
        $instance->absolute = 0 === \strpos($id, '/');
70
71 27
        $instance->fragments = \array_filter(
72 27
            \explode(
73 27
                \DIRECTORY_SEPARATOR,
74 27
                \ltrim($id, \DIRECTORY_SEPARATOR)
75
            )
76
        );
77
78 27
        return $instance;
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84 3
    public function getFirstPart(): string
85
    {
86 3
        $first = \reset($this->fragments);
87
88 3
        return empty($first) ?
89 1
            \DIRECTORY_SEPARATOR :
90 3
            $first;
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96 19
    public function getIterator()
97
    {
98 19
        yield from $this->getFragments();
99 19
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 3
    public function getLastPart(): string
105
    {
106 3
        if (empty($this->fragments)) {
107 1
            return \DIRECTORY_SEPARATOR;
108
        }
109
110 2
        return \end($this->fragments);
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116 7
    public function getScheme(): string
117
    {
118 7
        return $this->scheme;
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124 25
    public function isAbsolute(): bool
125
    {
126 25
        return $this->absolute;
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132 5
    public function isRoot(): bool
133
    {
134 5
        return '' === $this->basename();
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140 5
    public function isValid(): bool
141
    {
142 5
        if (\preg_match('/^[^*?"<>|:]*$/', \trim((string) $this->withScheme(null), ' /'))) {
143 3
            return true;
144
        }
145
146 2
        return false;
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152 3
    public function shift(): string
153
    {
154 3
        if (null !== $shift = \array_shift($this->fragments)) {
155 3
            return $shift;
156
        }
157
158
        throw new \Exception('Error while shifting the path.');
159
    }
160
161
    /**
162
     * {@inheritdoc}
163
     */
164 6
    public function withScheme(?string $scheme): Path
165
    {
166 6
        $clone = clone $this;
167
168 6
        $clone->scheme = $scheme ?? '';
169
170 6
        return $clone;
171
    }
172
173
    /**
174
     * {@inheritdoc}
175
     */
176 25
    protected function getFragments(): array
177
    {
178 25
        $fragments = $this->fragments;
179
180 25
        if (empty($fragments[0])) {
181 4
            $fragments[0] = \DIRECTORY_SEPARATOR;
182
        }
183
184 25
        return $fragments;
185
    }
186
}
187