Completed
Push — master ( b7cb0e...ffcfdb )
by Дмитрий
02:53
created

AbstractCompiler   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 66.67%
Metric Value
dl 0
loc 41
ccs 6
cts 9
cp 0.6667
rs 10
wmc 4
lcom 1
cbo 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 4 1
compile() 0 1 ?
A assertExpression() 0 6 2
A pass() 0 5 1
1
<?php
2
/**
3
 * @author Patsura Dmitry https://github.com/ovr <[email protected]>
4
 */
5
6
namespace PHPSA\Compiler\Statement;
7
8
use PHPSA\CompiledExpression;
9
use PHPSA\Context;
10
use PHPSA\Compiler\StatementCompilerInterface;
11
use RuntimeException;
12
13
abstract class AbstractCompiler implements StatementCompilerInterface
14
{
15
    /**
16
     * @abstract
17
     * @var string
18
     */
19
    protected $name = 'unknown';
20
21 6
    protected function assertExpression($expression)
22
    {
23 6
        if (!$expression instanceof $this->name) {
24
            throw new RuntimeException('Passed $expression must be instance of ' . $this->name);
25
        }
26 6
    }
27
28
    /**
29
     * @param $stmt
30
     * @param Context $context
31
     * @return CompiledExpression
32
     */
33 6
    public function pass($stmt, Context $context)
34
    {
35 6
        $this->assertExpression($stmt);
36 6
        return $this->compile($stmt, $context);
37
    }
38
39
    /**
40
     * @return string
41
     */
42
    public function getName()
43
    {
44
        return $this->name;
45
    }
46
47
    /**
48
     * @param $expr
49
     * @param Context $context
50
     * @return mixed
51
     */
52
    abstract protected function compile($expr, Context $context);
53
}
54