1
|
|
|
<?php |
2
|
|
|
namespace gossi\codegen\parser\visitor\parts; |
3
|
|
|
|
4
|
|
|
use gossi\codegen\model\ValueInterface; |
5
|
|
|
use gossi\codegen\parser\PrettyPrinter; |
6
|
|
|
use PhpParser\Node; |
7
|
|
|
use PhpParser\Node\Const_; |
8
|
|
|
use PhpParser\Node\Expr\Array_; |
9
|
|
|
use PhpParser\Node\Expr\ClassConstFetch; |
10
|
|
|
use PhpParser\Node\Expr\ConstFetch; |
11
|
|
|
use PhpParser\Node\Scalar\DNumber; |
12
|
|
|
use PhpParser\Node\Scalar\LNumber; |
13
|
|
|
use PhpParser\Node\Scalar\MagicConst; |
14
|
|
|
use PhpParser\Node\Scalar\String_; |
15
|
|
|
|
16
|
|
|
trait ValueParserPart { |
17
|
|
|
|
18
|
|
|
private $constMap = [ |
19
|
|
|
'false' => false, |
20
|
|
|
'true' => true |
21
|
|
|
]; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Parses the value of a node into the model |
25
|
|
|
* |
26
|
|
|
* @param ValueInterface $obj |
27
|
|
|
* @param Node $node |
28
|
|
|
* @return void |
29
|
|
|
*/ |
30
|
10 |
|
private function parseValue(ValueInterface $obj, Node $node) { |
31
|
10 |
|
$value = $node instanceof Const_ ? $node->value : $node->default; |
32
|
10 |
|
if ($value !== null) { |
33
|
7 |
|
if ($this->isPrimitive($value)) { |
34
|
7 |
|
$obj->setValue($this->getPrimitiveValue($value)); |
35
|
7 |
|
} else { |
36
|
4 |
|
$obj->setExpression($this->getExpression($value)); |
37
|
|
|
} |
38
|
7 |
|
} |
39
|
10 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Returns whether this node is a primitive value |
43
|
|
|
* |
44
|
|
|
* @param Node $node |
45
|
|
|
* @return boolean |
46
|
|
|
*/ |
47
|
7 |
|
private function isPrimitive(Node $node) { |
48
|
|
|
return $node instanceof String_ |
49
|
7 |
|
|| $node instanceof LNumber |
50
|
6 |
|
|| $node instanceof DNumber |
51
|
6 |
|
|| $this->isBool($node) |
52
|
7 |
|
|| $this->isNull($node); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Returns the primitive value |
57
|
|
|
* |
58
|
|
|
* @param Node $node |
59
|
|
|
* @return mixed |
60
|
|
|
*/ |
61
|
7 |
|
private function getPrimitiveValue(Node $node) { |
62
|
7 |
|
if ($this->isBool($node)) { |
63
|
5 |
|
return (bool) $this->getExpression($node); |
64
|
|
|
} |
65
|
|
|
|
66
|
7 |
|
if ($this->isNull($node)) { |
67
|
3 |
|
return null; |
68
|
|
|
} |
69
|
|
|
|
70
|
7 |
|
return $node->value; |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Returns whether this node is a boolean value |
75
|
|
|
* |
76
|
|
|
* @param Node $node |
77
|
|
|
* @return boolean |
78
|
|
|
*/ |
79
|
7 |
|
private function isBool(Node $node) { |
80
|
7 |
|
if ($node instanceof ConstFetch) { |
81
|
5 |
|
$const = $node->name->parts[0]; |
82
|
5 |
|
if (isset($this->constMap[$const])) { |
83
|
5 |
|
return is_bool($this->constMap[$const]); |
84
|
|
|
} |
85
|
|
|
|
86
|
3 |
|
return is_bool($const); |
87
|
|
|
} |
88
|
7 |
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Returns whether this node is a null value |
92
|
|
|
* |
93
|
|
|
* @param Node $node |
94
|
|
|
* @return boolean |
95
|
|
|
*/ |
96
|
7 |
|
private function isNull(Node $node) { |
97
|
7 |
|
if ($node instanceof ConstFetch) { |
98
|
3 |
|
$const = $node->name->parts[0]; |
99
|
3 |
|
return $const === 'null'; |
100
|
|
|
} |
101
|
7 |
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Returns the value from a node |
105
|
|
|
* |
106
|
|
|
* @param Node $node |
107
|
|
|
* @return mixed |
108
|
|
|
*/ |
109
|
6 |
|
private function getExpression(Node $node) { |
110
|
6 |
|
if ($node instanceof ConstFetch) { |
111
|
5 |
|
$const = $node->name->parts[0]; |
112
|
5 |
|
if (isset($this->constMap[$const])) { |
113
|
5 |
|
return $this->constMap[$const]; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $const; |
117
|
|
|
} |
118
|
|
|
|
119
|
4 |
|
if ($node instanceof ClassConstFetch) { |
120
|
3 |
|
return $node->class->parts[0] . '::' . $node->name; |
121
|
|
|
} |
122
|
|
|
|
123
|
3 |
|
if ($node instanceof MagicConst) { |
124
|
1 |
|
return $node->getName(); |
125
|
|
|
} |
126
|
|
|
|
127
|
3 |
|
if ($node instanceof Array_) { |
128
|
3 |
|
$prettyPrinter = new PrettyPrinter(); |
129
|
3 |
|
return $prettyPrinter->prettyPrintExpr($node); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: