Completed
Push — master ( dc5b75...3324e4 )
by Valentin
03:55
created

ChildClassDeclaration::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Cycle\ORM\Promise\Declaration\Declaration;
5
6
use Cycle\ORM\Promise\Declaration\DeclarationInterface;
7
8
final class ChildClassDeclaration implements DeclarationInterface
9
{
10
    /** @var string */
11
    private $shortName;
12
13
    /** @var string|null */
14
    private $namespace;
15
16
    public function __construct(string $name, DeclarationInterface $parent)
17
    {
18
        $this->shortName = $this->makeShortName($name);
19
        $this->namespace = $this->makeNamespaceName($name, $parent);
20
    }
21
22
    public function getShortName(): string
23
    {
24
        return $this->shortName;
25
    }
26
27
    public function getNamespaceName(): ?string
28
    {
29
        return $this->namespace;
30
    }
31
32
    public function getFullName(): string
33
    {
34
        if (empty($this->namespace)) {
35
            return "\\{$this->shortName}";
36
        }
37
38
        return "{$this->namespace}\\{$this->shortName}";
39
    }
40
41
    private function makeShortName(string $class): string
42
    {
43
        $class = rtrim($class, '\\');
44
        $lastPosition = mb_strripos($class, '\\');
45
        if ($lastPosition === false) {
46
            return $class;
47
        }
48
49
        return mb_substr($class, $lastPosition + 1);
50
    }
51
52
    private function makeNamespaceName(string $class, DeclarationInterface $parent): ?string
53
    {
54
        $class = rtrim($class, '\\');
55
        $lastPosition = mb_strripos($class, '\\');
56
        if ($lastPosition === 0) {
57
            return null;
58
        }
59
60
        if ($lastPosition !== false) {
61
            return ltrim(mb_substr($class, 0, $lastPosition), '\\');
62
        }
63
64
        return $parent->getNamespaceName();
65
    }
66
}