ChildClassDeclaration   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 1
dl 0
loc 81
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getShortName() 0 4 1
A getNamespaceName() 0 4 1
A getFullName() 0 8 2
A makeShortName() 0 10 2
A makeNamespaceName() 0 14 3
1
<?php
2
3
/**
4
 * Spiral Framework. Cycle ProxyFactory
5
 *
6
 * @license MIT
7
 * @author  Valentin V (Vvval)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Cycle\ORM\Promise\Declaration\Declaration;
13
14
use Cycle\ORM\Promise\Declaration\DeclarationInterface;
15
16
final class ChildClassDeclaration implements DeclarationInterface
17
{
18
    /** @var string */
19
    private $shortName;
20
21
    /** @var string|null */
22
    private $namespace;
23
24
    /**
25
     * @param string               $name
26
     * @param DeclarationInterface $parent
27
     */
28
    public function __construct(string $name, DeclarationInterface $parent)
29
    {
30
        $this->shortName = $this->makeShortName($name);
31
        $this->namespace = $this->makeNamespaceName($name, $parent);
32
    }
33
34
    /**
35
     * @return string
36
     */
37
    public function getShortName(): string
38
    {
39
        return $this->shortName;
40
    }
41
42
    /**
43
     * @return string|null
44
     */
45
    public function getNamespaceName(): ?string
46
    {
47
        return $this->namespace;
48
    }
49
50
    /**
51
     * @return string
52
     */
53
    public function getFullName(): string
54
    {
55
        if (empty($this->namespace)) {
56
            return "\\{$this->shortName}";
57
        }
58
59
        return "{$this->namespace}\\{$this->shortName}";
60
    }
61
62
    /**
63
     * @param string $class
64
     * @return string
65
     */
66
    private function makeShortName(string $class): string
67
    {
68
        $class = rtrim($class, '\\');
69
        $lastPosition = mb_strripos($class, '\\');
70
        if ($lastPosition === false) {
71
            return $class;
72
        }
73
74
        return mb_substr($class, $lastPosition + 1);
75
    }
76
77
    /**
78
     * @param string               $class
79
     * @param DeclarationInterface $parent
80
     * @return string|null
81
     */
82
    private function makeNamespaceName(string $class, DeclarationInterface $parent): ?string
83
    {
84
        $class = rtrim($class, '\\');
85
        $lastPosition = mb_strripos($class, '\\');
86
        if ($lastPosition === 0) {
87
            return null;
88
        }
89
90
        if ($lastPosition !== false) {
91
            return ltrim(mb_substr($class, 0, $lastPosition), '\\');
92
        }
93
94
        return $parent->getNamespaceName();
95
    }
96
}
97