Completed
Pull Request — master (#232)
by
unknown
05:28
created

Scalar::__construct()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 3
nop 2
dl 0
loc 19
ccs 0
cts 13
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace PHPSA\Compiler;
4
5
use PHPSA\Context;
6
use PhpParser\Node;
7
use PHPSA\Compiler;
8
use PHPSA\Compiler\Scalar\AbstractScalarCompiler;
9
use RuntimeException;
10
11
class Scalar
12
{
13
    /**
14
     * @param Node\Scalar $scalar
15
     * @throws RuntimeException
16
     * @return AbstractScalarCompiler
17
     */
18
    protected function factory(Node\Scalar $scalar)
19
    {
20
        switch (get_class($scalar)) {
21
            case Node\Scalar\LNumber::class:
22
                return new Compiler\Scalar\LNum();
23
        }
24
25
        throw new RuntimeException('Unknown scalar: ' . get_class($scalar));
26
    }
27
28
    /**
29
     * @param Node\Scalar $scalar
30
     * @param Context $context
31
     */
32
    public function __construct(Node\Scalar $scalar, Context $context)
33
    {
34
        try {
35
            $context->getEventManager()->fire(
36
                Event\ScalarBeforeCompile::EVENT_NAME,
37
                new Event\ScalarBeforeCompile(
38
                    $scalar,
39
                    $context
40
                )
41
            );
42
43
            $compiler = $this->factory($scalar);
44
        } catch (\Exception $e) {
45
            $context->debug('ScalarCompiler is not implemented for ' . get_class($scalar));
46
            return;
47
        }
48
49
        $compiler->pass($scalar, $context);
50
    }
51
}
52