1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace RulerZ\Visitor; |
6
|
|
|
|
7
|
|
|
use Hoa\Ruler\Model as AST; |
8
|
|
|
use Hoa\Visitor\Element as VisitorElement; |
9
|
|
|
|
10
|
|
|
use RulerZ\Compiler\RuleVisitor; |
11
|
|
|
use RulerZ\Model; |
12
|
|
|
|
13
|
|
|
abstract class Visitor implements RuleVisitor |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* {@inheritdoc} |
17
|
|
|
*/ |
18
|
|
View Code Duplication |
public function visit(VisitorElement $element, &$handle = null, $eldnah = null) |
|
|
|
|
19
|
|
|
{ |
20
|
|
|
if ($element instanceof Model\Rule) { |
21
|
|
|
return $this->visitModel($element, $handle, $eldnah); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
if ($element instanceof AST\Operator) { |
25
|
|
|
return $this->visitOperator($element, $handle, $eldnah); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
if ($element instanceof AST\Bag\Scalar) { |
29
|
|
|
return $this->visitScalar($element, $handle, $eldnah); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
if ($element instanceof AST\Bag\RulerArray) { |
33
|
|
|
return $this->visitArray($element, $handle, $eldnah); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
if ($element instanceof AST\Bag\Context) { |
37
|
|
|
return $this->visitAccess($element, $handle, $eldnah); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if ($element instanceof Model\Parameter) { |
41
|
|
|
return $this->visitParameter($element, $handle, $eldnah); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
throw new \LogicException(sprintf('Element of type "%s" not handled', get_class($element))); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
|
|
public function visitAccess(AST\Bag\Context $element, &$handle = null, $eldnah = null) |
51
|
|
|
{ |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
|
|
public function visitModel(AST\Model $element, &$handle = null, $eldnah = null) |
58
|
|
|
{ |
59
|
|
|
return $element->getExpression()->accept($this, $handle, $eldnah); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
|
|
public function visitScalar(AST\Bag\Scalar $element, &$handle = null, $eldnah = null) |
66
|
|
|
{ |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
|
|
public function visitArray(AST\Bag\RulerArray $element, &$handle = null, $eldnah = null) |
73
|
|
|
{ |
74
|
|
|
return array_map(function ($item) use (&$handle, $eldnah) { |
75
|
|
|
return $item->accept($this, $handle, $eldnah); |
76
|
|
|
}, $element->getArray()); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
*/ |
82
|
|
|
public function visitOperator(AST\Operator $element, &$handle = null, $eldnah = null) |
83
|
|
|
{ |
84
|
|
|
// visit the arguments |
85
|
|
|
array_map(function ($argument) use (&$handle, $eldnah) { |
86
|
|
|
return $argument->accept($this, $handle, $eldnah); |
87
|
|
|
}, $element->getArguments()); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
|
|
*/ |
93
|
|
|
public function visitParameter(Model\Parameter $element, &$handle = null, $eldnah = null) |
94
|
|
|
{ |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
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.