Passed
Push — master ( 686045...01d1c1 )
by Thorsten
01:37
created

ConfigPath::fromPathString()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 12
cts 12
cp 1
rs 8.7624
c 0
b 0
f 0
cc 5
eloc 16
nc 6
nop 3
crap 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A ConfigPath::getScope() 0 4 1
A ConfigPath::getParts() 0 4 1
A ConfigPath::hasParts() 0 4 1
1
<?php
2
/**
3
 * This file is part of the daikon/config project.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
declare(strict_types=1);
10
11
namespace Daikon\Config;
12
13
final class ConfigPath implements ConfigPathInterface
14
{
15
    private const PATH_SEP = ".";
16
17
    private $scope;
18
19
    private $parts;
20
21 10
    public static function fromString(string $path): ConfigPathInterface
22
    {
23 10
        $separatorPosition = strpos($path, self::PATH_SEP);
24 10
        if ($separatorPosition === 0) {
25 1
            throw new \Exception("Initializing malformed ConfigPath: Path may not start with separator.");
26
        }
27 9
        $pathParts = explode(self::PATH_SEP, $path);
28 9
        return new static(array_shift($pathParts), $pathParts);
29
    }
30
31 4
    public function getScope(): string
32
    {
33 4
        return $this->scope;
34
    }
35
36 4
    public function getParts(): array
37
    {
38 4
        return $this->parts;
39
    }
40
41 4
    public function hasParts(): bool
42
    {
43 4
        return !empty($this->parts);
44
    }
45
46 1
    public function getLength(): int
47
    {
48 1
        return count($this->parts);
49
    }
50
51 1
    public function __toString(): string
52
    {
53 1
        $pathParts = $this->parts;
54 1
        array_unshift($pathParts, $this->scope);
55 1
        return join(self::PATH_SEP, $pathParts);
56
    }
57
58 9
    private function __construct(string $scope, array $parts)
59
    {
60 9
        if (empty($scope)) {
61 1
            throw new \Exception("Trying to create ConfigPath from empty scope.");
62
        }
63 8
        $this->scope = $scope;
64 8
        $this->parts = $parts;
65 8
    }
66
}
67