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

AbstractScalarCompiler   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 45
ccs 0
cts 9
cp 0
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A assertExpression() 0 6 2
A pass() 0 5 1
A getName() 0 4 1
compile() 0 1 ?
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