Completed
Push — master ( d075bd...c77af7 )
by Thomas
03:19
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 0
Metric Value
wmc 23
c 0
b 0
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 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;
1 ignored issue
show
Bug introduced by
Accessing value on the interface PhpParser\Node suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
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
}