Completed
Pull Request — master (#256)
by Enrico
11:29
created

Scalar   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 63.64%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 88
ccs 28
cts 44
cp 0.6364
rs 10
wmc 19
lcom 1
cbo 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A compile() 0 16 2
C factory() 0 39 16
1
<?php
2
3
namespace PHPSA\Compiler;
4
5
use PHPSA\CompiledExpression;
6
use PHPSA\Context;
7
use PhpParser\Node;
8
use PHPSA\Compiler;
9
use RuntimeException;
10
use Webiny\Component\EventManager\EventManager;
11
12
class Scalar
13
{
14
    /**
15
     * @var Context
16
     */
17
    private $context;
18
19
    /**
20
     * @var EventManager
21
     */
22
    private $eventManager;
23
24
    /**
25
     * @param Context $context
26
     * @param EventManager $eventManager
27
     */
28 802
    public function __construct(Context $context, EventManager $eventManager)
29
    {
30 802
        $this->context = $context;
31 802
        $this->eventManager = $eventManager;
32 802
    }
33
34
    /**
35
     * @param Node\Scalar $scalar
36
     * @return CompiledExpression
37
     */
38 802
    public function compile(Node\Scalar $scalar)
39
    {
40
        try {
41 802
            $this->eventManager->fire(
42 802
                Event\ScalarBeforeCompile::EVENT_NAME,
43 802
                new Event\ScalarBeforeCompile(
44 802
                    $scalar,
45 802
                    $this->context
46 802
                )
47 802
            );
48
49 802
            return $this->factory($scalar);
50
        } catch (\Exception $e) {
51
            $this->context->debug('ScalarCompiler is not implemented for ' . get_class($scalar));
52
        }
53
    }
54
55
    /**
56
     * @param Node\Scalar $scalar
57
     * @return CompiledExpression
58
     * @throws RuntimeException
59
     */
60 802
    protected function factory(Node\Scalar $scalar)
61
    {
62 802
        switch (get_class($scalar)) {
63 802
            case \PHPSA\Node\Scalar\Nil::class:
64 25
                return new CompiledExpression(CompiledExpression::NULL);
65 793
            case \PHPSA\Node\Scalar\Boolean::class:
66 241
                return new CompiledExpression(CompiledExpression::BOOLEAN, $scalar->value);
0 ignored issues
show
Bug introduced by
The property value does not seem to exist in PhpParser\Node\Scalar.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
67 663
            case \PHPSA\Node\Scalar\Fake::class:
68 79
                return new CompiledExpression($scalar->type, $scalar->value);
0 ignored issues
show
Bug introduced by
The property type does not seem to exist in PhpParser\Node\Scalar.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
69
70
            /**
71
             * Numbers
72
             */
73 655
            case Node\Scalar\MagicConst\Line::class:
74
                return new CompiledExpression(CompiledExpression::INTEGER, 0); //wrong value
75 655
            case Node\Scalar\LNumber::class:
76 523
                return new CompiledExpression(CompiledExpression::INTEGER, $scalar->value);
77 265
            case Node\Scalar\DNumber::class:
78 213
                return new CompiledExpression(CompiledExpression::DOUBLE, $scalar->value);
79
80
            /**
81
             * Strings
82
             */
83 53
            case Node\Scalar\String_::class:
84 53
                return new CompiledExpression(CompiledExpression::STRING, $scalar->value);
85
            case Node\Scalar\Encapsed::class:
86
            case Node\Scalar\MagicConst\Trait_::class:
87
            case Node\Scalar\MagicConst\Namespace_::class:
88
            case Node\Scalar\MagicConst\Class_::class:
89
            case Node\Scalar\MagicConst\Dir::class:
90
            case Node\Scalar\MagicConst\File::class:
91
            case Node\Scalar\MagicConst\Function_::class:
92
            case Node\Scalar\MagicConst\Method::class:
93
                return new CompiledExpression(CompiledExpression::STRING, ""); //wrong value
94
95
            default:
96
                throw new RuntimeException('Unknown scalar: ' . get_class($scalar));
97
        }
98
    }
99
}
100