ExpressionNode::markReferenced()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 3
nop 0
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\Exception\Exception;
14
use ILess\Node;
15
use ILess\Output\OutputInterface;
16
use ILess\Visitor\VisitorInterface;
17
18
/**
19
 * Expression.
20
 */
21
class ExpressionNode extends Node implements MarkableAsReferencedInterface
22
{
23
    /**
24
     * Node type.
25
     *
26
     * @var string
27
     */
28
    protected $type = 'Expression';
29
30
    /**
31
     * Parens flag.
32
     *
33
     * @var bool
34
     */
35
    public $parens = false;
36
37
    /**
38
     * Parens in operator flag.
39
     *
40
     * @var bool
41
     */
42
    public $parensInOp = false;
43
44
    /**
45
     * Constructor.
46
     *
47
     * @param array $value
48
     *
49
     * @throws Exception
50
     */
51
    public function __construct(array $value)
52
    {
53
        parent::__construct($value);
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function accept(VisitorInterface $visitor)
60
    {
61
        $this->value = $visitor->visitArray($this->value);
0 ignored issues
show
Documentation introduced by
$this->value is of type object<ILess\Node>|string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
62
    }
63
64
    /**
65
     * Compiles the node.
66
     *
67
     * @param Context $context The context
68
     * @param array|null $arguments Array of arguments
69
     * @param bool|null $important Important flag
70
     *
71
     * @return ParenNode|ExpressionNode|Node
72
     */
73
    public function compile(Context $context, $arguments = null, $important = null)
74
    {
75
        $inParenthesis = $this->parens && !$this->parensInOp;
76
        $doubleParen = false;
77
78
        if ($inParenthesis) {
79
            $context->inParenthesis();
80
        }
81
        $count = count($this->value);
82
        if ($count > 1) {
83
            $compiled = [];
84
            foreach ($this->value as $v) {
0 ignored issues
show
Bug introduced by
The expression $this->value of type object<ILess\Node>|string is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
85
                /* @var $v Node */
86
                $compiled[] = $v->compile($context);
87
            }
88
            $return = new self($compiled);
89
        } elseif ($count === 1) {
90
            if (property_exists($this->value[0], 'parens') && $this->value[0]->parens
91
                && property_exists($this->value[0], 'parensInOp') && !$this->value[0]->parensInOp
92
            ) {
93
                $doubleParen = true;
94
            }
95
            $return = $this->value[0]->compile($context);
96
        } else {
97
            $return = $this;
98
        }
99
100
        if ($inParenthesis) {
101
            $context->outOfParenthesis();
102
        }
103
104
        if ($this->parens && $this->parensInOp && !($context->isMathOn()) && !$doubleParen) {
105
            $return = new ParenNode($return);
106
        }
107
108
        return $return;
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114
    public function generateCSS(Context $context, OutputInterface $output)
115
    {
116
        for ($i = 0, $count = count($this->value); $i < $count; ++$i) {
117
            $this->value[$i]->generateCSS($context, $output);
118
            if ($i + 1 < $count) {
119
                $output->add(' ');
120
            }
121
        }
122
    }
123
124
    public function throwAwayComments()
125
    {
126
        if (is_array($this->value)) {
127
            $new = [];
128
            foreach ($this->value as $v) {
129
                if ($v instanceof CommentNode) {
130
                    continue;
131
                }
132
                $new[] = $v;
133
            }
134
            $this->value = $new;
0 ignored issues
show
Documentation Bug introduced by
It seems like $new of type array is incompatible with the declared type object<ILess\Node>|string of property $value.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
135
        }
136
    }
137
138
    /**
139
     * Marks as referenced.
140
     */
141
    public function markReferenced()
142
    {
143
        foreach ($this->value as $value) {
0 ignored issues
show
Bug introduced by
The expression $this->value of type object<ILess\Node>|string is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
144
            if ($value instanceof MarkableAsReferencedInterface) {
145
                $value->markReferenced();
146
            }
147
        }
148
    }
149
}
150