ValueNode::compile()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 3
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\Output\OutputInterface;
15
use ILess\Visitor\VisitorInterface;
16
17
/**
18
 * Value.
19
 */
20
class ValueNode extends Node
21
{
22
    /**
23
     * Node type.
24
     *
25
     * @var string
26
     */
27
    protected $type = 'Value';
28
29
    /**
30
     * Constructor.
31
     *
32
     * @param array $value Array of value
33
     */
34
    public function __construct(array $value)
35
    {
36
        parent::__construct($value);
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function accept(VisitorInterface $visitor)
43
    {
44
        $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...
45
    }
46
47
    /**
48
     * Compiles the node.
49
     *
50
     * @param Context $context The context
51
     * @param array|null $arguments Array of arguments
52
     * @param bool|null $important Important flag
53
     *
54
     * @return ValueNode
55
     */
56
    public function compile(Context $context, $arguments = null, $important = null)
57
    {
58
        if (count($this->value) == 1) {
59
            return $this->value[0]->compile($context);
60
        }
61
62
        $return = [];
63
        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...
64
            $return[] = $v->compile($context);
65
        }
66
67
        return new self($return);
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function generateCSS(Context $context, OutputInterface $output)
74
    {
75
        for ($i = 0, $count = count($this->value); $i < $count; ++$i) {
76
            $this->value[$i]->generateCSS($context, $output);
77
            if ($i + 1 < $count) {
78
                $output->add($context->compress ? ',' : ', ');
79
            }
80
        }
81
    }
82
}
83