GenericVisitor::visitScalar()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 4
rs 10
c 0
b 0
f 0
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)
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...
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);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return sprintf('call_use...me, $inlinedArguments); (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...
126
    }
127
}
128