Completed
Push — master ( 793727...6af734 )
by Alexander
02:36
created

ReflectionClassConstant   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 170
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 9

Test Coverage

Coverage 98.15%

Importance

Changes 0
Metric Value
wmc 20
lcom 3
cbo 9
dl 0
loc 170
ccs 53
cts 54
cp 0.9815
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A collectFromClassNode() 0 20 4
A __construct() 0 15 3
A getDeclaringClass() 0 4 1
A getDocComment() 0 6 2
A getModifiers() 0 15 4
A getName() 0 4 1
A getValue() 0 6 1
A isPrivate() 0 4 1
A isProtected() 0 4 1
A isPublic() 0 4 1
A __toString() 0 13 1
1
<?php
2
/**
3
 * Parser Reflection API
4
 *
5
 * @copyright Copyright 2019, Lisachenko Alexander <[email protected]>
6
 *
7
 * This source file is subject to the license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
declare(strict_types=1);
11
12
namespace Go\ParserReflection;
13
14
use Go\ParserReflection\ValueResolver\NodeExpressionResolver;
15
use PhpParser\Node\Const_;
16
use PhpParser\Node\Stmt\ClassConst;
17
use PhpParser\Node\Stmt\ClassLike;
18
use \ReflectionClassConstant as BaseReflectionClassConstant;
19
20
class ReflectionClassConstant extends BaseReflectionClassConstant
21
{
22
    /**
23
     * Concrete class constant node
24
     *
25
     * @var ClassConst
26
     */
27
    private $classConstantNode;
28
29
    /**
30
     * @var Const_
31
     */
32
    private $constNode;
33
34
    /**
35
     * Name of the class
36
     *
37
     * @var string
38
     */
39
    private $className;
40
41
    /**
42
     * Parses class constants from the concrete class node
43
     *
44
     * @param ClassLike $classLikeNode Class-like node
45
     * @param string $reflectionClassName FQN of the class
46
     *
47
     * @return array|ReflectionClassConstant[]
48
     */
49 7
    public static function collectFromClassNode(ClassLike $classLikeNode, string $reflectionClassName): array
50
    {
51 7
        $classConstants = [];
52
53 7
        foreach ($classLikeNode->stmts as $classLevelNode) {
54 7
            if ($classLevelNode instanceof ClassConst) {
55 6
                foreach ($classLevelNode->consts as $const) {
56 6
                    $classConstName = $const->name->toString();
57 6
                    $classConstants[$classConstName] = new ReflectionClassConstant(
58 6
                        $reflectionClassName,
59 6
                        $classConstName,
60 6
                        $classLevelNode,
61 7
                        $const
62
                    );
63
                }
64
            }
65
        }
66
67 7
        return $classConstants;
68
    }
69
70
    /**
71
     * Initializes a reflection for the class constant
72
     *
73
     * @param string $className Name of the class
74
     * @param string $classConstantName Name of the class constant to reflect
75
     * @param ClassConst $classConstNode ClassConstant definition node
76
     * @param Const_|null $constNode Concrete const definition node
77
     */
78 6
    public function __construct(
79
        string $className,
80
        string $classConstantName,
81
        ClassConst $classConstNode = null,
82
        Const_ $constNode = null
83
    ) {
84 6
        $this->className = ltrim($className, '\\');
85
86 6
        if (!$classConstNode || !$constNode) {
87
            list($classConstNode, $constNode) = ReflectionEngine::parseClassConstant($className, $classConstantName);
88
        }
89
90 6
        $this->classConstantNode = $classConstNode;
91 6
        $this->constNode = $constNode;
92 6
    }
93
94
    /**
95
     * @inheritDoc
96
     */
97 2
    public function getDeclaringClass()
98
    {
99 2
        return new ReflectionClass($this->className);
100
    }
101
102
    /**
103
     * @inheritDoc
104
     */
105 1
    public function getDocComment()
106
    {
107 1
        $docBlock = $this->classConstantNode->getDocComment();
108
109 1
        return $docBlock ? $docBlock->getText() : false;
110
    }
111
112
    /**
113
     * @inheritDoc
114
     */
115 2
    public function getModifiers()
116
    {
117 2
        $modifiers = 0;
118 2
        if ($this->isPublic()) {
119 2
            $modifiers += ReflectionMethod::IS_PUBLIC;
120
        }
121 2
        if ($this->isProtected()) {
122 1
            $modifiers += ReflectionMethod::IS_PROTECTED;
123
        }
124 2
        if ($this->isPrivate()) {
125 1
            $modifiers += ReflectionMethod::IS_PRIVATE;
126
        }
127
128 2
        return $modifiers;
129
    }
130
131
    /**
132
     * @inheritDoc
133
     */
134 3
    public function getName()
135
    {
136 3
        return $this->constNode->name->toString();
137
    }
138
139
    /**
140
     * @inheritDoc
141
     */
142 2
    public function getValue()
143
    {
144 2
        $solver = new NodeExpressionResolver($this->getDeclaringClass());
145 2
        $solver->process($this->constNode->value);
146 2
        return $solver->getValue();
147
    }
148
149
    /**
150
     * @inheritDoc
151
     */
152 2
    public function isPrivate()
153
    {
154 2
        return $this->classConstantNode->isPrivate();
155
    }
156
157
    /**
158
     * @inheritDoc
159
     */
160 2
    public function isProtected()
161
    {
162 2
        return $this->classConstantNode->isProtected();
163
    }
164
165
    /**
166
     * @inheritDoc
167
     */
168 2
    public function isPublic()
169
    {
170 2
        return $this->classConstantNode->isPublic();
171
    }
172
173
    /**
174
     * @inheritDoc
175
     */
176 2
    public function __toString()
177
    {
178 2
        $value = $this->getValue();
179 2
        $valueType = new ReflectionType(gettype($value), null, true);
180
181 2
        return sprintf(
182 2
            "Constant [ %s %s %s ] { %s }\n",
183 2
            implode(' ', \Reflection::getModifierNames($this->getModifiers())),
184 2
            strtolower((string) ReflectionType::convertToDisplayType($valueType)),
185 2
            $this->getName(),
186 2
            (string) $value
187
        );
188
    }
189
}
190