Completed
Push — master ( 795841...f754fe )
by Дмитрий
11:10
created

Scalar::factory()   C

Complexity

Conditions 17
Paths 17

Size

Total Lines 52
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 56.1308

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 17
eloc 36
c 3
b 0
f 0
nc 17
nop 1
dl 0
loc 52
ccs 18
cts 37
cp 0.4865
crap 56.1308
rs 5.909

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 817
    public function __construct(Context $context, EventManager $eventManager)
29
    {
30 817
        $this->context = $context;
31 817
        $this->eventManager = $eventManager;
32 817
    }
33
34
    /**
35
     * @param Node\Scalar $scalar
36
     * @return CompiledExpression
37
     */
38 817
    public function compile(Node\Scalar $scalar)
39
    {
40
        try {
41 817
            $this->eventManager->fire(
42 817
                Event\ScalarBeforeCompile::EVENT_NAME,
43 817
                new Event\ScalarBeforeCompile(
44 817
                    $scalar,
45 817
                    $this->context
46 817
                )
47 817
            );
48
49 817
            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 817
    protected function factory(Node\Scalar $scalar)
61
    {
62 817
        switch (get_class($scalar)) {
63
            // Native Scalars
64 817
            case Node\Scalar\LNumber::class:
65 528
                return new CompiledExpression(CompiledExpression::INTEGER, $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...
66 561
            case Node\Scalar\DNumber::class:
67 213
                return new CompiledExpression(CompiledExpression::DOUBLE, $scalar->value);
68 384
            case Node\Scalar\String_::class:
69 54
                return new CompiledExpression(CompiledExpression::STRING, $scalar->value);
70
71
            // @todo Review this and implement support
72 339
            case Node\Scalar\EncapsedStringPart::class:
73
                return new CompiledExpression(CompiledExpression::STRING);
74 339
            case Node\Scalar\Encapsed::class:
75
                return $this->compileUncapsedString($scalar);
0 ignored issues
show
Compatibility introduced by
$scalar of type object<PhpParser\Node\Scalar> is not a sub-type of object<PhpParser\Node\Scalar\Encapsed>. It seems like you assume a child class of the class PhpParser\Node\Scalar to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
76
77
            /**
78
             * Fake scalars for testing
79
             */
80 339
            case \PHPSA\Node\Scalar\Nil::class:
81 25
                return new CompiledExpression(CompiledExpression::NULL);
82 330
            case \PHPSA\Node\Scalar\Boolean::class:
83 241
                return new CompiledExpression(CompiledExpression::BOOLEAN, $scalar->value);
84 89
            case \PHPSA\Node\Scalar\Fake::class:
85 88
                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...
86
87
            /**
88
             * MagicConst
89
             * @todo Review this and implement support, now We return only type
90
             */
91 1
            case Node\Scalar\MagicConst\Line::class:
92 1
                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