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
|
10 |
|
private function parseValue(ValueInterface $obj, Node $node) { |
24
|
10 |
|
$value = $node instanceof Const_ ? $node->value : $node->default; |
|
|
|
|
25
|
10 |
|
if ($value !== null) { |
26
|
7 |
|
if ($this->isPrimitive($value)) { |
27
|
7 |
|
$obj->setValue($this->getPrimitiveValue($value)); |
28
|
7 |
|
} else { |
29
|
4 |
|
$obj->setExpression($this->getExpression($value)); |
30
|
|
|
} |
31
|
7 |
|
} |
32
|
10 |
|
} |
33
|
|
|
|
34
|
7 |
|
private function isPrimitive(Node $node) { |
35
|
|
|
return $node instanceof String_ |
36
|
7 |
|
|| $node instanceof LNumber |
37
|
6 |
|
|| $node instanceof DNumber |
38
|
6 |
|
|| $this->isBool($node) |
39
|
7 |
|
|| $this->isNull($node); |
40
|
|
|
} |
41
|
|
|
|
42
|
7 |
|
private function getPrimitiveValue(Node $node) { |
|
|
|
|
43
|
7 |
|
if ($this->isBool($node)) { |
44
|
5 |
|
return (bool) $this->getExpression($node); |
45
|
|
|
} |
46
|
|
|
|
47
|
7 |
|
if ($this->isNull($node)) { |
48
|
3 |
|
return null; |
49
|
|
|
} |
50
|
|
|
|
51
|
7 |
|
return $node->value; |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
7 |
|
private function isBool(Node $node) { |
55
|
7 |
|
if ($node instanceof ConstFetch) { |
56
|
5 |
|
$const = $node->name->parts[0]; |
57
|
5 |
|
if (isset($this->constMap[$const])) { |
58
|
5 |
|
return is_bool($this->constMap[$const]); |
59
|
|
|
} |
60
|
|
|
|
61
|
3 |
|
return is_bool($const); |
62
|
|
|
} |
63
|
7 |
|
} |
64
|
|
|
|
65
|
7 |
|
private function isNull(Node $node) { |
66
|
7 |
|
if ($node instanceof ConstFetch) { |
67
|
3 |
|
$const = $node->name->parts[0]; |
68
|
3 |
|
return $const === 'null'; |
69
|
|
|
} |
70
|
7 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Returns the value from a node |
74
|
|
|
* |
75
|
|
|
* @param Node $node |
76
|
|
|
* @return mixed |
77
|
|
|
*/ |
78
|
6 |
|
private function getExpression(Node $node) { |
79
|
6 |
|
if ($node instanceof ConstFetch) { |
80
|
5 |
|
$const = $node->name->parts[0]; |
81
|
5 |
|
if (isset($this->constMap[$const])) { |
82
|
5 |
|
return $this->constMap[$const]; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $const; |
86
|
|
|
} |
87
|
|
|
|
88
|
4 |
|
if ($node instanceof ClassConstFetch) { |
89
|
3 |
|
return $node->class->parts[0] . '::' . $node->name; |
90
|
|
|
} |
91
|
|
|
|
92
|
3 |
|
if ($node instanceof MagicConst) { |
93
|
1 |
|
return $node->getName(); |
94
|
|
|
} |
95
|
|
|
|
96
|
3 |
|
if ($node instanceof Array_) { |
97
|
3 |
|
$prettyPrinter = new PrettyPrinter(); |
98
|
3 |
|
return $prettyPrinter->prettyPrintExpr($node); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
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: