Completed
Pull Request — master (#251)
by
unknown
05:24
created

Scalar   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 79.07%

Importance

Changes 0
Metric Value
dl 0
loc 78
ccs 34
cts 43
cp 0.7907
rs 10
c 0
b 0
f 0
wmc 20
lcom 1
cbo 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A compile() 0 16 2
D factory() 0 29 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 36
    public function __construct(Context $context, EventManager $eventManager)
29
    {
30 36
        $this->context = $context;
31 36
        $this->eventManager = $eventManager;
32 36
    }
33
34
    /**
35
     * @param $scalar
36
     * @return CompiledExpression
37
     */
38 36
    public function compile($scalar)
39
    {
40
        try {
41 36
            $this->eventManager->fire(
42 36
                Event\ScalarBeforeCompile::EVENT_NAME,
43 36
                new Event\ScalarBeforeCompile(
44 36
                    $scalar,
45 36
                    $this->context
46 36
                )
47 36
            );
48
49 36
            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 36
    protected function factory(Node\Scalar $scalar)
61
    {
62 36
        switch (get_class($scalar)) {
63 36
            case \PHPSA\Node\Scalar\Nil::class:
64
                return new CompiledExpression(CompiledExpression::NULL);
65 36
            case \PHPSA\Node\Scalar\Boolean::class:
66
                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 36
            case \PHPSA\Node\Scalar\Fake::class:
68
                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 36
            case Node\Scalar\LNumber::class:
70 32
                return new CompiledExpression(CompiledExpression::INTEGER, $scalar->value);
71 16
            case Node\Scalar\DNumber::class:
72 2
                return new CompiledExpression(CompiledExpression::DOUBLE, $scalar->value);
73 15
            case Node\Scalar\String_::class:
74 15
            case Node\Scalar\EncapsedStringPart::class:
75 15
            case Node\Scalar\Encapsed::class:
76 15
            case Node\Scalar\MagicConst\Trait_::class:
77 15
            case Node\Scalar\MagicConst\Namespace_::class:
78 15
            case Node\Scalar\MagicConst\Class_::class:
79 15
            case Node\Scalar\MagicConst\Dir::class:
80 15
            case Node\Scalar\MagicConst\File::class:
81 15
            case Node\Scalar\MagicConst\Function_::class:
82 15
            case Node\Scalar\MagicConst\Line::class:
83 15
            case Node\Scalar\MagicConst\Method::class:
84 15
                return new CompiledExpression(CompiledExpression::STRING, $scalar->value);
85
            default:
86
                throw new RuntimeException('Unknown scalar: ' . get_class($scalar));
87
        }
88
    }
89
}
90