Completed
Push — master ( 55b21a...aec22b )
by Thomas
06:20
created

ValueParserPart::getExpression()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6.105

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 23
ccs 12
cts 14
cp 0.8571
rs 8.5906
cc 6
eloc 13
nc 6
nop 1
crap 6.105
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;
1 ignored issue
show
Bug introduced by
Accessing default 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...
25 10
		if ($value !== null) {
26 7
			if ($this->isPrimitive($value)) {
27 7
				$obj->setValue($this->getPrimitiveValue($value));
28
			} else {
29 4
				$obj->setExpression($this->getExpression($value));
30
			}
31
		}
32 10
	}
33
	
34 7
	private function isPrimitive(Node $node) {
35 7
		return $node instanceof String_
36 6
			|| $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) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

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