ConditionNode::compile()   C
last analyzed

Complexity

Conditions 16
Paths 30

Size

Total Lines 29
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 5.0151
c 0
b 0
f 0
cc 16
eloc 22
nc 30
nop 3

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the ILess
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace ILess\Node;
11
12
use ILess\Context;
13
use ILess\Node;
14
use ILess\Util;
15
use ILess\Visitor\VisitorInterface;
16
17
/**
18
 * Condition.
19
 */
20
class ConditionNode extends Node
21
{
22
    /**
23
     * Node type.
24
     *
25
     * @var string
26
     */
27
    protected $type = 'Condition';
28
29
    /**
30
     * The operator.
31
     *
32
     * @var string
33
     */
34
    private $op;
35
36
    /**
37
     * The left operand.
38
     *
39
     * @var Node
40
     */
41
    private $lvalue;
42
43
    /**
44
     * The right operand.
45
     *
46
     * @var Node
47
     */
48
    private $rvalue;
49
50
    /**
51
     * Current index.
52
     *
53
     * @var int
54
     */
55
    private $index = 0;
56
57
    /**
58
     * Negate the result?
59
     *
60
     * @var bool
61
     */
62
    private $negate = false;
63
64
    /**
65
     * Constructor.
66
     *
67
     * @param string $op The operator
68
     * @param Node $l The left operand
69
     * @param Node $r The right operand
70
     * @param int $i
71
     * @param bool $negate
72
     */
73
    public function __construct($op, Node $l, Node $r, $i = 0, $negate = false)
74
    {
75
        $this->op = trim($op);
76
        $this->lvalue = $l;
77
        $this->rvalue = $r;
78
        $this->index = $i;
79
        $this->negate = (boolean) $negate;
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function accept(VisitorInterface $visitor)
86
    {
87
        $this->lvalue = $visitor->visit($this->lvalue);
88
        $this->rvalue = $visitor->visit($this->rvalue);
89
    }
90
91
    /**
92
     * Compiles the node.
93
     *
94
     * @param Context $context The context
95
     * @param array|null $arguments Array of arguments
96
     * @param bool|null $important Important flag
97
     *
98
     * @return bool
99
     */
100
    public function compile(Context $context, $arguments = null, $important = null)
101
    {
102
        $a = $this->lvalue->compile($context);
103
        $b = $this->rvalue->compile($context);
104
105
        switch ($this->op) {
106
            case 'and':
107
                $result = $a && $b;
108
                break;
109
            case 'or':
110
                $result = $a || $b;
111
                break;
112
            default:
113
                $compared = Util::compareNodes($a, $b);
114
                // strict comparison, we cannot use switch here
115
                if ($compared === -1) {
116
                    $result = $this->op === '<' || $this->op === '=<' || $this->op === '<=';
117
                } elseif ($compared === 0) {
118
                    $result = $this->op === '=' || $this->op === '>=' || $this->op === '=<' || $this->op === '<=';
119
                } elseif ($compared === 1) {
120
                    $result = $this->op === '>' || $this->op === '>=' || $this->op === '=>';
121
                } else {
122
                    $result = false;
123
                }
124
                break;
125
        }
126
127
        return $this->negate ? !$result : $result;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->negate ? !$result : $result; (boolean) is incompatible with the return type declared by the interface ILess\Node\CompilableInterface::compile of type ILess\Node.

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...
128
    }
129
}
130