OperationNode::accept()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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\CompilerException;
14
use ILess\Node;
15
use ILess\Output\OutputInterface;
16
use ILess\Visitor\VisitorInterface;
17
18
/**
19
 * Operation node.
20
 */
21
class OperationNode extends Node
22
{
23
    /**
24
     * Node type.
25
     *
26
     * @var string
27
     */
28
    protected $type = 'Operation';
29
30
    /**
31
     * Operator.
32
     *
33
     * @var string
34
     */
35
    protected $operator;
36
37
    /**
38
     * Array of operands.
39
     *
40
     * @var array
41
     */
42
    protected $operands;
43
44
    /**
45
     * Is spaced flag.
46
     *
47
     * @var bool
48
     */
49
    public $isSpaced = false;
50
51
    /**
52
     * Parens.
53
     *
54
     * @var bool
55
     */
56
    public $parensInOp = false;
57
58
    /**
59
     * Constructor.
60
     *
61
     * @param string $operator The operator
62
     * @param array $operands Array of operands
63
     * @param bool $isSpaced Is spaced?
64
     */
65
    public function __construct($operator, array $operands, $isSpaced = false)
66
    {
67
        $this->operator = trim($operator);
68
        $this->operands = $operands;
69
        $this->isSpaced = $isSpaced;
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function accept(VisitorInterface $visitor)
76
    {
77
        $this->operands = $visitor->visit($this->operands);
0 ignored issues
show
Documentation Bug introduced by
It seems like $visitor->visit($this->operands) of type * is incompatible with the declared type array of property $operands.

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...
78
    }
79
80
    /**
81
     * Compiles the node.
82
     *
83
     * @param Context $context The context
84
     * @param array|null $arguments Array of arguments
85
     * @param bool|null $important Important flag
86
     *
87
     * @throws CompilerException
88
     *
89
     * @return Node
90
     */
91
    public function compile(Context $context, $arguments = null, $important = null)
92
    {
93
        $a = $this->operands[0]->compile($context);
94
        $b = $this->operands[1]->compile($context);
95
96
        if ($context->isMathOn()) {
97
            if ($a instanceof DimensionNode && $b instanceof ColorNode) {
98
                $a = $a->toColor();
99
            }
100
101
            if ($b instanceof DimensionNode && $a instanceof ColorNode) {
102
                $b = $b->toColor();
103
            }
104
105
            if (!self::methodExists($a, 'operate')) {
106
                throw new CompilerException('Operation on an invalid type.');
107
            }
108
109
            return $a->operate($context, $this->operator, $b);
110
        } else {
111
            return new self($this->operator, [$a, $b], $this->isSpaced);
112
        }
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    public function generateCSS(Context $context, OutputInterface $output)
119
    {
120
        $this->operands[0]->generateCSS($context, $output);
121
122
        if ($this->isSpaced) {
123
            $output->add(' ');
124
        }
125
126
        $output->add($this->operator);
127
128
        if ($this->isSpaced) {
129
            $output->add(' ');
130
        }
131
132
        $this->operands[1]->generateCSS($context, $output);
133
    }
134
}
135