Completed
Pull Request — master (#90)
by Kévin
06:52
created

GenericVisitor::compileRuntimeOperator()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 2
eloc 6
nc 2
nop 4
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
        if ($this->operators->hasInlineOperator($operatorName)) {
109
            return $this->compileInlineOperator($operatorName, $element->getArguments(), $handle, $eldnah);
110
        }
111
112
        return $this->compileRuntimeOperator($operatorName, $element->getArguments(), $handle, $eldnah);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->compileRun...s(), $handle, $eldnah); (string) is incompatible with the return type declared by the interface RulerZ\Compiler\RuleVisitor::visitOperator of type Hoa\Consistency\Xcallable.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
113
    }
114
115
    private function compileRuntimeOperator($operatorName, array $arguments, &$handle = null, $eldnah = null)
116
    {
117
        $compiledArguments = array_map(function ($argument) use (&$handle, $eldnah) {
118
            return sprintf('$this->unwrapArgument(%s)', $argument->accept($this, $handle, $eldnah));
119
        }, $arguments);
120
        $inlinedArguments = empty($arguments) ? '' : ', '.implode(', ', $compiledArguments);
121
122
        return sprintf('call_user_func($operators["%s"]%s)', $operatorName, $inlinedArguments);
123
    }
124
125
    private function compileInlineOperator($operatorName, array $arguments, &$handle = null, $eldnah = null)
126
    {
127
        $operatorCallable = $this->operators->getInlineOperator($operatorName);
128
        $compiledArguments = array_map(function ($argument) use (&$handle, $eldnah) {
129
            return $argument->accept($this, $handle, $eldnah);
130
        }, $arguments);
131
132
        return call_user_func_array($operatorCallable, $compiledArguments);
133
    }
134
}
135