Passed
Push — master ( 5a4455...a3a054 )
by Pol
02:30
created

Path   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 28
dl 0
loc 117
ccs 38
cts 38
cp 1
rs 10
c 4
b 0
f 0
wmc 17

12 Methods

Rating   Name   Duplication   Size   Complexity  
A isAbsolute() 0 3 1
A fromString() 0 10 1
A getLastPart() 0 3 1
A basename() 0 3 1
A dirname() 0 3 2
A getIterator() 0 3 1
A getFragments() 0 9 2
A shift() 0 3 1
A isValid() 0 7 2
A __toString() 0 3 2
A getFirstPart() 0 7 2
A isRoot() 0 3 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace drupol\phpvfs\Utils;
6
7
class Path implements \IteratorAggregate
8
{
9
    /**
10
     * @var bool
11
     */
12
    private $absolute;
13
    /**
14
     * @var string[]
15
     */
16
    private $fragments;
17
18
    /**
19
     * {@inheritdoc}
20
     */
21 4
    public function __toString()
22
    {
23 4
        return ($this->isAbsolute() ? '/' : '') . \implode('/', $this->getFragments());
24
    }
25
26 13
    public function basename()
27
    {
28 13
        return $this->getLastPart();
29
    }
30
31 12
    public function dirname()
32
    {
33 12
        return ($this->isAbsolute() ? '/' : '') . \implode('/', \array_slice($this->getFragments(), 0, -1));
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 20
    public static function fromString(string $id)
40
    {
41 20
        $instance = new self();
42 20
        $instance->fragments = \explode(
43 20
            \DIRECTORY_SEPARATOR,
44 20
            \ltrim($id, \DIRECTORY_SEPARATOR)
45
        );
46 20
        $instance->absolute = 0 === \strpos($id, '/');
47
48 20
        return $instance;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 2
    public function getFirstPart()
55
    {
56 2
        $first = \reset($this->fragments);
57
58 2
        return empty($first) ?
59 1
            \DIRECTORY_SEPARATOR :
60 2
            $first;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 15
    public function getIterator()
67
    {
68 15
        yield from $this->getFragments();
69 15
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74 13
    public function getLastPart()
75
    {
76 13
        return \end($this->fragments);
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82 18
    public function isAbsolute()
83
    {
84 18
        return $this->absolute;
85
    }
86
87 4
    public function isRoot()
88
    {
89 4
        return '' === $this->basename();
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95 3
    public function isValid()
96
    {
97 3
        if (\preg_match('/^[^*?"<>|:]*$/', \trim($this->__toString(), ' /'))) {
98 2
            return true;
99
        }
100
101 1
        return false;
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107 2
    public function shift()
108
    {
109 2
        return \array_shift($this->fragments);
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115 18
    protected function getFragments()
116
    {
117 18
        $fragments = $this->fragments;
118
119 18
        if (empty($fragments[0])) {
120 2
            $fragments[0] = \DIRECTORY_SEPARATOR;
121
        }
122
123 18
        return $fragments;
124
    }
125
}
126