Completed
Push — master ( 5be53c...92f243 )
by Thomas
03:28
created

ValueParserPart   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 95.65%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 23
c 1
b 1
f 0
lcom 1
cbo 7
dl 0
loc 117
ccs 44
cts 46
cp 0.9565
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A parseValue() 0 10 4
B isPrimitive() 0 7 5
A getPrimitiveValue() 0 11 3
A isBool() 0 10 3
A isNull() 0 6 2
B getExpression() 0 23 6
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 5
			} else {
36 3
				$obj->setExpression($this->getExpression($value));
37
			}
38 5
		}
39 7
	}
40
	
41
	/**
42
	 * Returns whether this node is a primitive value
43
	 * 
44
	 * @param Node $node
45
	 * @return boolean
46
	 */
47 5
	private function isPrimitive(Node $node) {
48
		return $node instanceof String_
49 5
			|| $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 boolean
78
	 */
79 5
	private function isBool(Node $node) {
80 5
		if ($node instanceof ConstFetch) {
81 3
			$const = $node->name->parts[0];
0 ignored issues
show
Deprecated Code introduced by
The property PhpParser\Node\Name::$parts has been deprecated with message: Avoid directly accessing $parts, use methods instead.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
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 boolean
95
	 */
96 5
	private function isNull(Node $node) {
97 5
		if ($node instanceof ConstFetch) {
98 2
			$const = $node->name->parts[0];
0 ignored issues
show
Deprecated Code introduced by
The property PhpParser\Node\Name::$parts has been deprecated with message: Avoid directly accessing $parts, use methods instead.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
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];
0 ignored issues
show
Deprecated Code introduced by
The property PhpParser\Node\Name::$parts has been deprecated with message: Avoid directly accessing $parts, use methods instead.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
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
}