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

Scalar   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 41
ccs 0
cts 19
cp 0
rs 10
c 0
b 0
f 0
wmc 4
lcom 0
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A factory() 0 9 2
A __construct() 0 19 2
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