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); |
|
|
|
|
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) { |
|
|
|
|
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; |
|
|
|
|
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Marks as referenced. |
140
|
|
|
*/ |
141
|
|
|
public function markReferenced() |
142
|
|
|
{ |
143
|
|
|
foreach ($this->value as $value) { |
|
|
|
|
144
|
|
|
if ($value instanceof MarkableAsReferencedInterface) { |
145
|
|
|
$value->markReferenced(); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
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: