Completed
Push — master ( 8bad52...c13fe7 )
by Дмитрий
03:41
created

AbstractExpressionCompiler::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
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\Expression;
7
8
use InvalidArgumentException;
9
use PHPSA\CompiledExpression;
10
use PHPSA\Context;
11
use PHPSA\Compiler\ExpressionCompilerInterface;
12
13
abstract class AbstractExpressionCompiler implements ExpressionCompilerInterface
14
{
15
    /**
16
     * @abstract
17
     * @var string
18
     */
19
    protected $name = 'unknown';
20
21
    /**
22
     * @param $expression
23
     * @throws InvalidArgumentException when param does not match $this->name
24
     */
25 870
    protected function assertExpression($expression)
26
    {
27 870
        if (!$expression instanceof $this->name) {
28 1
            throw new InvalidArgumentException('Passed $expression must be instance of ' . $this->name);
29 16
        }
30 869
    }
31
32
    /**
33
     * @param  $expr
34
     * @param Context $context
35
     * @return CompiledExpression
36
     */
37 870
    public function pass($expr, Context $context)
38
    {
39 870
        $this->assertExpression($expr);
40 869
        return $this->compile($expr, $context);
41
    }
42
43
    /**
44
     * @return string
45
     */
46 1
    public function getName()
47
    {
48 1
        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