ValueParserPart::isNull()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
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 7
	private function parseValue(ValueInterface $obj, Node $node) {
31 7
		$value = $node instanceof Const_ ? $node->value : $node->default;
32 7
		if ($value !== null) {
33 5
			if ($this->isPrimitive($value)) {
34 5
				$obj->setValue($this->getPrimitiveValue($value));
35
			} else {
36 3
				$obj->setExpression($this->getExpression($value));
37
			}
38
		}
39 7
	}
40
41
	/**
42
	 * Returns whether this node is a primitive value
43
	 * 
44
	 * @param Node $node
45
	 * @return bool
46
	 */
47 5
	private function isPrimitive(Node $node) {
48 5
		return $node instanceof String_
49 4
			|| $node instanceof LNumber
50 4
			|| $node instanceof DNumber
51 4
			|| $this->isBool($node)
52 5
			|| $this->isNull($node);
53
	}
54
55
	/**
56
	 * Returns the primitive value
57
	 * 
58
	 * @param Node $node
59
	 * @return mixed
60
	 */
61 5
	private function getPrimitiveValue(Node $node) {
62 5
		if ($this->isBool($node)) {
63 3
			return (bool) $this->getExpression($node);
64
		}
65
66 5
		if ($this->isNull($node)) {
67 2
			return null;
68
		}
69
70 5
		return $node->value;
71
	}
72
73
	/**
74
	 * Returns whether this node is a boolean value
75
	 * 
76
	 * @param Node $node
77
	 * @return bool
78
	 */
79 5
	private function isBool(Node $node) {
80 5
		if ($node instanceof ConstFetch) {
81 3
			$const = $node->name->parts[0];
82 3
			if (isset($this->constMap[$const])) {
83 3
				return is_bool($this->constMap[$const]);
84
			}
85
86 2
			return is_bool($const);
87
		}
88 5
	}
89
90
	/**
91
	 * Returns whether this node is a null value
92
	 * 
93
	 * @param Node $node
94
	 * @return bool
95
	 */
96 5
	private function isNull(Node $node) {
97 5
		if ($node instanceof ConstFetch) {
98 2
			$const = $node->name->parts[0];
99 2
			return $const === 'null';
100
		}
101 5
	}
102
103
	/**
104
	 * Returns the value from a node
105
	 *
106
	 * @param Node $node
107
	 * @return mixed
108
	 */
109 4
	private function getExpression(Node $node) {
110 4
		if ($node instanceof ConstFetch) {
111 3
			$const = $node->name->parts[0];
112 3
			if (isset($this->constMap[$const])) {
113 3
				return $this->constMap[$const];
114
			}
115
116
			return $const;
117
		}
118
119 3
		if ($node instanceof ClassConstFetch) {
120 2
			return $node->class->parts[0] . '::' . $node->name;
121
		}
122
123 2
		if ($node instanceof MagicConst) {
124 1
			return $node->getName();
125
		}
126
127 2
		if ($node instanceof Array_) {
128 2
			$prettyPrinter = new PrettyPrinter();
129 2
			return $prettyPrinter->prettyPrintExpr($node);
130
		}
131
	}
132
}
133