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

Path::dirname()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 1
nc 2
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 2
rs 10
c 1
b 0
f 0
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