1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace RulerZ\Target; |
6
|
|
|
|
7
|
|
|
use Hoa\Ruler\Model as AST; |
8
|
|
|
use Hoa\Visitor\Element as VisitorElement; |
9
|
|
|
|
10
|
|
|
use RulerZ\Compiler\RuleVisitor; |
11
|
|
|
use RulerZ\Exception\OperatorNotFoundException; |
12
|
|
|
use RulerZ\Model; |
13
|
|
|
use RulerZ\Target\Operators\Definitions as OperatorsDefinitions; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Generic visitor intended to be extended. |
17
|
|
|
*/ |
18
|
|
|
abstract class GenericVisitor implements RuleVisitor |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var OperatorsDefinitions |
22
|
|
|
*/ |
23
|
|
|
protected $operators; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
* |
28
|
|
|
* @note The aim of this method is to be overriden. |
29
|
|
|
*/ |
30
|
|
|
public function getCompilationData(): array |
31
|
|
|
{ |
32
|
|
|
return []; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function __construct(OperatorsDefinitions $operators) |
36
|
|
|
{ |
37
|
|
|
$this->operators = $operators; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
|
View Code Duplication |
public function visit(VisitorElement $element, &$handle = null, $eldnah = null) |
|
|
|
|
44
|
|
|
{ |
45
|
|
|
if ($element instanceof Model\Rule) { |
46
|
|
|
return $this->visitModel($element, $handle, $eldnah); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if ($element instanceof AST\Operator) { |
50
|
|
|
return $this->visitOperator($element, $handle, $eldnah); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if ($element instanceof AST\Bag\Scalar) { |
54
|
|
|
return $this->visitScalar($element, $handle, $eldnah); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if ($element instanceof AST\Bag\RulerArray) { |
58
|
|
|
return $this->visitArray($element, $handle, $eldnah); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if ($element instanceof AST\Bag\Context) { |
62
|
|
|
return $this->visitAccess($element, $handle, $eldnah); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if ($element instanceof Model\Parameter) { |
66
|
|
|
return $this->visitParameter($element, $handle, $eldnah); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
throw new \LogicException(sprintf('Element of type "%s" not handled', get_class($element))); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
|
|
public function visitModel(AST\Model $element, &$handle = null, $eldnah = null) |
76
|
|
|
{ |
77
|
|
|
return $element->getExpression()->accept($this, $handle, $eldnah); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
|
|
public function visitScalar(AST\Bag\Scalar $element, &$handle = null, $eldnah = null) |
84
|
|
|
{ |
85
|
|
|
return var_export($element->getValue(), true); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
|
|
*/ |
91
|
|
|
public function visitArray(AST\Bag\RulerArray $element, &$handle = null, $eldnah = null) |
92
|
|
|
{ |
93
|
|
|
return array_map(function ($item) use (&$handle, $eldnah) { |
94
|
|
|
return $item->accept($this, $handle, $eldnah); |
95
|
|
|
}, $element->getArray()); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
*/ |
101
|
|
|
public function visitOperator(AST\Operator $element, &$handle = null, $eldnah = null) |
102
|
|
|
{ |
103
|
|
|
$operatorName = $element->getName(); |
104
|
|
|
|
105
|
|
|
// the operator does not exist at all, throw an error before doing anything else. |
106
|
|
|
if (!$this->operators->hasInlineOperator($operatorName) && !$this->operators->hasOperator($operatorName)) { |
107
|
|
|
throw new OperatorNotFoundException($operatorName, sprintf('Operator "%s" does not exist.', $operatorName)); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
// expand the arguments |
111
|
|
|
$arguments = array_map(function ($argument) use (&$handle, $eldnah) { |
112
|
|
|
return $argument->accept($this, $handle, $eldnah); |
113
|
|
|
}, $element->getArguments()); |
114
|
|
|
|
115
|
|
|
// and either inline the operator call |
116
|
|
|
if ($this->operators->hasInlineOperator($operatorName)) { |
117
|
|
|
$callable = $this->operators->getInlineOperator($operatorName); |
118
|
|
|
|
119
|
|
|
return call_user_func_array($callable, $arguments); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$inlinedArguments = empty($arguments) ? '' : ', '.implode(', ', $arguments); |
123
|
|
|
|
124
|
|
|
// or defer it. |
125
|
|
|
return sprintf('call_user_func($operators["%s"]%s)', $operatorName, $inlinedArguments); |
|
|
|
|
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
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.