AbstractCompiler::compile()
last analyzed

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
nc 1
dl 0
loc 1
ccs 0
cts 0
cp 0
c 0
b 0
f 0
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 $name
18
     */
19
    protected $name = 'unknown';
20
21
    /**
22
     * @param $expression
23
     * @throws RuntimeException when param does not match $this->name
24
     */
25
    protected function assertExpression($expression)
26
    {
27
        if (!$expression instanceof $this->name) {
28
            throw new RuntimeException('Passed $expression must be instance of ' . $this->name);
29
        }
30
    }
31
32
    /**
33
     * @param $stmt
34
     * @param Context $context
35
     * @return CompiledExpression
36
     */
37
    public function pass($stmt, Context $context)
38
    {
39
        $this->assertExpression($stmt);
40
        return $this->compile($stmt, $context);
41
    }
42
43
    /**
44
     * @return string
45
     */
46
    public function getName()
47
    {
48
        return $this->name;
49
    }
50
51
    /**
52
     * @param $expr
53
     * @param Context $context
54
     * @return mixed
55
     */
56
    abstract protected function compile($expr, Context $context);
57
}
58