Passed
Branch develop (c0cf48)
by Baptiste
05:24
created

Name::withoutRoot()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
ccs 5
cts 5
cp 1
rs 9.6666
cc 2
eloc 4
nc 2
nop 0
crap 2
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Compose\Definition;
5
6
use Innmind\Compose\Exception\{
7
    NameMustContainAtLeastACharacter,
8
    NameNotNamespaced
9
};
10
use Innmind\Immutable\Str;
11
12
final class Name
13
{
14
    private $value;
15
16 154
    public function __construct(string $value)
17
    {
18 154
        $value = Str::of($value);
19
20 154
        if ($value->empty()) {
21 1
            throw new NameMustContainAtLeastACharacter;
22
        }
23
24 153
        $this->value = $value;
25 153
    }
26
27 2
    public function root(): self
28
    {
29 2
        $namespace = $this->value->split('.');
30
31 2
        if ($namespace->size() === 1) {
32 1
            throw new NameNotNamespaced((string) $this->value);
33
        }
34
35 1
        return new self((string) $namespace->first());
36
    }
37
38 2
    public function withoutRoot(): self
39
    {
40 2
        $namespace = $this->value->split('.');
41
42 2
        if ($namespace->size() === 1) {
43 1
            throw new NameNotNamespaced((string) $this->value);
44
        }
45
46 1
        return new self((string) $namespace->drop(1)->join('.'));
47
    }
48
49 139
    public function __toString(): string
50
    {
51 139
        return (string) $this->value;
52
    }
53
}
54