ClassDefinition::getName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Spiral Framework, IDE Helper
4
 *
5
 * @author    Dmitry Mironov <[email protected]>
6
 * @licence   MIT
7
 */
8
9
namespace Spiral\IdeHelper\Model;
10
11
/**
12
 * Class ClassDefinition
13
 *
14
 * @package Spiral\IdeHelper\Model
15
 */
16
class ClassDefinition
17
{
18
    /**
19
     * @var string
20
     */
21
    private $name;
22
23
    /**
24
     * @var string
25
     */
26
    private $shortName;
27
28
    /**
29
     * @var string
30
     */
31
    private $namespace;
32
33
    /**
34
     * @var ClassMember[]
35
     */
36
    private $members;
37
38
    /**
39
     * ClassDefinition constructor.
40
     *
41
     * @param string $name
42
     * @param array  $members
43
     */
44
    public function __construct(string $name, array $members)
45
    {
46
        $this->name = $name;
47
        $this->members = $members;
48
49
        $this->setNames($name);
50
    }
51
52
    /**
53
     * @return string
54
     */
55
    public function getName(): string
56
    {
57
        return $this->name;
58
    }
59
60
    /**
61
     * @return string
62
     */
63
    public function getShortName(): string
64
    {
65
        return $this->shortName;
66
    }
67
68
    /**
69
     * @return string
70
     */
71
    public function getNamespace(): string
72
    {
73
        return $this->namespace;
74
    }
75
76
    /**
77
     * @return ClassMember[]
78
     */
79
    public function getMembers(): array
80
    {
81
        return $this->members;
82
    }
83
84
    private function setNames(string $name)
85
    {
86
        if (class_exists($name) || interface_exists($name)) {
87
            $reflection = new \ReflectionClass($name);
88
            $this->shortName = $reflection->getShortName();
89
            $this->namespace = $reflection->getNamespaceName();
90
        } else {
91
            $components = explode('\\', $name);
92
93
            $this->shortName = array_pop($components);
94
            $this->namespace = implode('\\', $components);
95
        }
96
    }
97
}
98