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

AbstractScalarCompiler::pass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace PHPSA\Compiler\Scalar;
4
5
use PHPSA\CompiledExpression;
6
use PHPSA\Context;
7
use PHPSA\Compiler\ScalarCompilerInterface;
8
use RuntimeException;
9
10
abstract class AbstractScalarCompiler implements ScalarCompilerInterface
11
{
12
    /**
13
     * @abstract
14
     * @var string $name
15
     */
16
    protected $name = 'unknown';
17
18
    /**
19
     * @param $expression
20
     * @throws RuntimeException when param does not match $this->name
21
     */
22
    protected function assertExpression($expression)
23
    {
24
        if (!$expression instanceof $this->name) {
25
            throw new RuntimeException('Passed $expression must be instance of ' . $this->name);
26
        }
27
    }
28
29
    /**
30
     * @param $stmt
31
     * @param Context $context
32
     * @return CompiledExpression
33
     */
34
    public function pass($stmt, Context $context)
35
    {
36
        $this->assertExpression($stmt);
37
        return $this->compile($stmt, $context);
38
    }
39
40
    /**
41
     * @return string
42
     */
43
    public function getName()
44
    {
45
        return $this->name;
46
    }
47
48
    /**
49
     * @param $expr
50
     * @param Context $context
51
     * @return mixed
52
     */
53
    abstract protected function compile($expr, Context $context);
54
}
55