Scalar::factory()   C
last analyzed

Complexity

Conditions 17
Paths 17

Size

Total Lines 52

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 306

Importance

Changes 0
Metric Value
cc 17
nc 17
nop 1
dl 0
loc 52
ccs 0
cts 35
cp 0
crap 306
rs 5.2166
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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