Completed
Push — master ( 68be21...073c54 )
by Kévin
02:41
created

GenericVisitor::visitOperator()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 26
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 26
rs 8.439
cc 5
eloc 12
nc 4
nop 3
1
<?php
2
3
namespace RulerZ\Target;
4
5
use Hoa\Ruler\Model as AST;
6
use Hoa\Visitor\Element as VisitorElement;
7
8
use RulerZ\Compiler\RuleVisitor;
9
use RulerZ\Exception\OperatorNotFoundException;
10
use RulerZ\Model;
11
use RulerZ\Target\Operators\Definitions as OperatorsDefinitions;
12
13
/**
14
 * Generic visitor intended to be extended.
15
 */
16
abstract class GenericVisitor implements RuleVisitor
17
{
18
    /**
19
     * @var OperatorsDefinitions
20
     */
21
    protected $operators;
22
23
    /**
24
     * @inheritdoc
25
     *
26
     * @note The aim of this method is to be overriden.
27
     */
28
    public function getCompilationData()
29
    {
30
        return [];
31
    }
32
33
    public function __construct(OperatorsDefinitions $operators)
34
    {
35
        $this->operators = $operators;
36
    }
37
38
    /**
39
     * {@inheritDoc}
40
     */
41 View Code Duplication
    public function visit(VisitorElement $element, &$handle = null, $eldnah = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42
    {
43
        if ($element instanceof Model\Rule) {
44
            return $this->visitModel($element, $handle, $eldnah);
45
        }
46
47
        if ($element instanceof AST\Operator) {
48
            return $this->visitOperator($element, $handle, $eldnah);
49
        }
50
51
        if ($element instanceof AST\Bag\Scalar) {
52
            return $this->visitScalar($element, $handle, $eldnah);
53
        }
54
55
        if ($element instanceof AST\Bag\RulerArray) {
56
            return $this->visitArray($element, $handle, $eldnah);
57
        }
58
59
        if ($element instanceof AST\Bag\Context) {
60
            return $this->visitAccess($element, $handle, $eldnah);
61
        }
62
63
        if ($element instanceof Model\Parameter) {
64
            return $this->visitParameter($element, $handle, $eldnah);
65
        }
66
67
        throw new \LogicException(sprintf('Element of type "%s" not handled', get_class($element)));
68
    }
69
70
    /**
71
     * {@inheritDoc}
72
     */
73
    public function visitModel(AST\Model $element, &$handle = null, $eldnah = null)
74
    {
75
        return $element->getExpression()->accept($this, $handle, $eldnah);
76
    }
77
78
    /**
79
     * {@inheritDoc}
80
     */
81
    public function visitScalar(AST\Bag\Scalar $element, &$handle = null, $eldnah = null)
82
    {
83
        return var_export($element->getValue(), true);
84
    }
85
86
    /**
87
     * {@inheritDoc}
88
     */
89
    public function visitArray(AST\Bag\RulerArray $element, &$handle = null, $eldnah = null)
90
    {
91
        return array_map(function ($item) use (&$handle, $eldnah) {
92
            return $item->accept($this, $handle, $eldnah);
93
        }, $element->getArray());
94
    }
95
96
    /**
97
     * {@inheritDoc}
98
     */
99
    public function visitOperator(AST\Operator $element, &$handle = null, $eldnah = null)
100
    {
101
        $operatorName = $element->getName();
102
103
        // the operator does not exist at all, throw an error before doing anything else.
104
        if (!$this->operators->hasInlineOperator($operatorName) && !$this->operators->hasOperator($operatorName)) {
105
            throw new OperatorNotFoundException($operatorName, sprintf('Operator "%s" does not exist.', $operatorName));
106
        }
107
108
        // expand the arguments
109
        $arguments = array_map(function ($argument) use (&$handle, $eldnah) {
110
            return $argument->accept($this, $handle, $eldnah);
111
        }, $element->getArguments());
112
113
        // and either inline the operator call
114
        if ($this->operators->hasInlineOperator($operatorName)) {
115
            $callable = $this->operators->getInlineOperator($operatorName);
116
117
            return call_user_func_array($callable, $arguments);
118
        }
119
120
        $inlinedArguments = empty($arguments) ? '' : ', ' . implode(', ', $arguments);
121
122
        // or defer it.
123
        return sprintf('call_user_func($operators["%s"]%s)', $operatorName, $inlinedArguments);
124
    }
125
}
126