Scalar   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 114
ccs 0
cts 53
cp 0
rs 10
c 0
b 0
f 0
wmc 22
lcom 1
cbo 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A compileUncapsedString() 0 8 2
A __construct() 0 5 1
A compile() 0 16 2
C factory() 0 52 17
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
    public function __construct(Context $context, EventManager $eventManager)
29
    {
30
        $this->context = $context;
31
        $this->eventManager = $eventManager;
32
    }
33
34
    /**
35
     * @param Node\Scalar $scalar
36
     * @return CompiledExpression
37
     */
38
    public function compile(Node\Scalar $scalar)
39
    {
40
        try {
41
            $this->eventManager->fire(
42
                Event\ScalarBeforeCompile::EVENT_NAME,
43
                new Event\ScalarBeforeCompile(
44
                    $scalar,
45
                    $this->context
46
                )
47
            );
48
49
            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
    protected function factory(Node\Scalar $scalar)
61
    {
62
        switch (get_class($scalar)) {
63
            // Native Scalars
64
            case Node\Scalar\LNumber::class:
65
                return new CompiledExpression(CompiledExpression::INTEGER, $scalar->value);
66
            case Node\Scalar\DNumber::class:
67
                return new CompiledExpression(CompiledExpression::DOUBLE, $scalar->value);
68
            case Node\Scalar\String_::class:
69
                return new CompiledExpression(CompiledExpression::STRING, $scalar->value);
70
71
            // @todo Review this and implement support
72
            case Node\Scalar\EncapsedStringPart::class:
73
                return new CompiledExpression(CompiledExpression::STRING);
74
            case Node\Scalar\Encapsed::class:
75
                return $this->compileUncapsedString($scalar);
76
77
            /**
78
             * Fake scalars for testing
79
             */
80
            case \PHPSA\Node\Scalar\Nil::class:
81
                return new CompiledExpression(CompiledExpression::NULL);
82
            case \PHPSA\Node\Scalar\Boolean::class:
83
                return new CompiledExpression(CompiledExpression::BOOLEAN, $scalar->value);
84
            case \PHPSA\Node\Scalar\Fake::class:
85
                return new CompiledExpression($scalar->type, $scalar->value);
86
87
            /**
88
             * MagicConst
89
             * @todo Review this and implement support, now We return only type
90
             */
91
            case Node\Scalar\MagicConst\Line::class:
92
                return new CompiledExpression(CompiledExpression::INTEGER, $scalar->getAttribute('startLine'));
93
            case Node\Scalar\MagicConst\Trait_::class:
94
                return new CompiledExpression(CompiledExpression::STRING);
95
            case Node\Scalar\MagicConst\Namespace_::class:
96
                return new CompiledExpression(CompiledExpression::STRING);
97
            case Node\Scalar\MagicConst\Class_::class:
98
                return new CompiledExpression(CompiledExpression::STRING);
99
            case Node\Scalar\MagicConst\Dir::class:
100
                return new CompiledExpression(CompiledExpression::STRING);
101
            case Node\Scalar\MagicConst\File::class:
102
                return new CompiledExpression(CompiledExpression::STRING);
103
            case Node\Scalar\MagicConst\Function_::class:
104
                return new CompiledExpression(CompiledExpression::STRING);
105
            case Node\Scalar\MagicConst\Method::class:
106
                return new CompiledExpression(CompiledExpression::STRING);
107
108
            default:
109
                throw new RuntimeException('Unknown scalar: ' . get_class($scalar));
110
        }
111
    }
112
113
    /**
114
     * @param Node\Scalar\Encapsed $scalar
115
     * @return CompiledExpression
116
     */
117
    protected function compileUncapsedString(Node\Scalar\Encapsed $scalar)
118
    {
119
        foreach ($scalar->parts as $part) {
120
            $this->context->getExpressionCompiler()->compile($part);
121
        }
122
123
        return new CompiledExpression(CompiledExpression::STRING);
124
    }
125
}
126