Completed
Pull Request — master (#251)
by
unknown
11:10
created

Scalar::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
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 $scalar
36
     * @return CompiledExpression
37
     */
38 802
    public function compile($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 655
            case Node\Scalar\LNumber::class:
70 523
                return new CompiledExpression(CompiledExpression::INTEGER, $scalar->value);
71 265
            case Node\Scalar\DNumber::class:
72 213
                return new CompiledExpression(CompiledExpression::DOUBLE, $scalar->value);
73 53
            case Node\Scalar\String_::class:
74 53
            case Node\Scalar\EncapsedStringPart::class:
75 53
            case Node\Scalar\Encapsed::class:
76 53
            case Node\Scalar\MagicConst\Trait_::class:
77 53
            case Node\Scalar\MagicConst\Namespace_::class:
78 53
            case Node\Scalar\MagicConst\Class_::class:
79 53
            case Node\Scalar\MagicConst\Dir::class:
80 53
            case Node\Scalar\MagicConst\File::class:
81 53
            case Node\Scalar\MagicConst\Function_::class:
82 53
            case Node\Scalar\MagicConst\Line::class:
83 53
            case Node\Scalar\MagicConst\Method::class:
84 53
                return new CompiledExpression(CompiledExpression::STRING, $scalar->value);
85
            default:
86
                throw new RuntimeException('Unknown scalar: ' . get_class($scalar));
87
        }
88
    }
89
}
90