DirectiveNode::__construct()   B
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 36
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 27
nc 3
nop 8

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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\DebugInfo;
14
use ILess\FileInfo;
15
use ILess\Node;
16
use ILess\Output\OutputInterface;
17
use ILess\Visitor\VisitorInterface;
18
19
/**
20
 * Directive.
21
 */
22
class DirectiveNode extends Node implements
23
    MarkableAsReferencedInterface, ReferencedInterface
24
{
25
    /**
26
     * The type.
27
     *
28
     * @var string
29
     */
30
    protected $type = 'Directive';
31
32
    /**
33
     * The directive name.
34
     *
35
     * @var string
36
     */
37
    public $name;
38
39
    /**
40
     * Array of rules.
41
     *
42
     * @var array
43
     */
44
    public $rules = [];
45
46
    /**
47
     * Current index.
48
     *
49
     * @var int
50
     */
51
    public $index = 0;
52
53
    /**
54
     * Array of variables.
55
     *
56
     * @var array
57
     */
58
    protected $variables;
59
60
    /**
61
     * Is referenced?
62
     *
63
     * @var bool
64
     */
65
    public $isReferenced = false;
66
67
    /**
68
     * @var bool
69
     */
70
    public $isRooted = false;
71
72
    /**
73
     * @var array
74
     */
75
    public $allExtends = [];
76
77
    /**
78
     * Constructor.
79
     *
80
     * @param string $name The name
81
     * @param array|string $value The value
82
     * @param RulesetNode|null|array $rules
83
     * @param int $index The index
84
     * @param FileInfo $currentFileInfo Current file info
85
     * @param DebugInfo $debugInfo The debug information
86
     * @param bool $isReferenced Is referenced?
87
     * @param bool $isRooted Is rooted?
88
     */
89
    public function __construct(
90
        $name,
91
        $value,
92
        $rules = null,
93
        $index = 0,
94
        FileInfo $currentFileInfo = null,
95
        DebugInfo $debugInfo = null,
96
        $isReferenced = false,
97
        $isRooted = false
98
    ) {
99
        $this->name = $name;
100
101
        parent::__construct($value);
102
103
        if (null !== $rules) {
104
            if (is_array($rules)) {
105
                $this->rules = $rules;
106
            } else {
107
                $this->rules = [$rules];
108
                $selectors = new SelectorNode([], [], null, $index, $currentFileInfo);
109
                $this->rules[0]->selectors = $selectors->createEmptySelectors();
110
            }
111
            for ($i = 0; $i < count($this->rules); ++$i) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
112
                $this->rules[$i]->allowImports = true;
113
            }
114
        } else {
115
            // null
116
            $this->rules = $rules;
0 ignored issues
show
Documentation Bug introduced by
It seems like $rules of type null is incompatible with the declared type array of property $rules.

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...
117
        }
118
119
        $this->index = $index;
120
        $this->currentFileInfo = $currentFileInfo;
121
        $this->debugInfo = $debugInfo;
122
        $this->isReferenced = $isReferenced;
123
        $this->isRooted = $isRooted;
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129
    public function accept(VisitorInterface $visitor)
130
    {
131
        if ($this->rules) {
132
            $this->rules = $visitor->visitArray($this->rules);
0 ignored issues
show
Documentation Bug introduced by
It seems like $visitor->visitArray($this->rules) of type * is incompatible with the declared type array of property $rules.

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...
133
        }
134
135
        if ($this->value) {
136
            $this->value = $visitor->visit($this->value);
137
        }
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143
    public function generateCSS(Context $context, OutputInterface $output)
144
    {
145
        $value = $this->value;
146
        $rules = $this->rules;
147
148
        $output->add($this->name, $this->currentFileInfo, $this->index);
149
150
        if ($value) {
151
            $output->add(' ');
152
            $value->generateCSS($context, $output);
153
        }
154
155
        if (is_array($rules)) {
156
            $this->outputRuleset($context, $output, $rules);
157
        } else {
158
            $output->add(';');
159
        }
160
    }
161
162
    /**
163
     * Compiles the node.
164
     *
165
     * @param Context $context The context
166
     * @param array|null $arguments Array of arguments
167
     * @param bool|null $important Important flag
168
     *
169
     * @return DirectiveNode
170
     */
171
    public function compile(Context $context, $arguments = null, $important = null)
172
    {
173
        $rules = $this->rules;
174
        $value = $this->value;
175
176
        // media stored inside other directive should not bubble over it
177
        // backup media bubbling information
178
        $mediaPathBackup = $context->mediaPath;
179
        $mediaBlocksBackup = $context->mediaBlocks;
180
181
        $context->mediaPath = [];
182
        $context->mediaBlocks = [];
183
184
        if ($value) {
185
            $value = $value->compile($context);
186
        }
187
188
        if ($rules) {
189
            $rules = [$rules[0]->compile($context)];
190
            $rules[0]->root = true;
191
        }
192
193
        $context->mediaPath = $mediaPathBackup;
194
        $context->mediaBlocks = $mediaBlocksBackup;
195
196
        return new self($this->name, $value, $rules, $this->index, $this->currentFileInfo,
197
            $this->debugInfo, $this->isReferenced, $this->isRooted);
198
    }
199
200
    /**
201
     * Returns the variable.
202
     *
203
     * @param string $name
204
     *
205
     * @return RuleNode|null
206
     */
207
    public function variable($name)
208
    {
209
        if ($this->rules) {
210
            // assuming that there is only one rule at this point - that is how parser constructs the rule
211
            return $this->rules[0]->variable($name);
212
        }
213
    }
214
215
    /**
216
     * Finds a selector.
217
     *
218
     * @param Node $selector
219
     * @param Context $context
220
     * @param null $filter
221
     *
222
     * @return mixed
223
     */
224
    public function find(Node $selector, Context $context, $filter = null)
225
    {
226
        if ($this->rules) {
227
            // assuming that there is only one rule at this point - that is how parser constructs the rule
228
            return $this->rules[0]->find($selector, $context, $this, $filter);
229
        }
230
    }
231
232
    /**
233
     * Returns the rulesets.
234
     */
235
    public function rulesets()
236
    {
237
        if ($this->rules) {
238
            // assuming that there is only one rule at this point - that is how parser constructs the rule
239
            return $this->rules[0]->rulesets();
240
        }
241
    }
242
243
    /**
244
     * Mark the directive as referenced.
245
     */
246
    public function markReferenced()
247
    {
248
        $this->isReferenced = true;
249
        if ($this->rules) {
250
            for ($i = 0; $i < count($this->rules); ++$i) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
251
                if ($this->rules[$i] instanceof MarkableAsReferencedInterface) {
252
                    $this->rules[$i]->markReferenced();
253
                }
254
            }
255
        }
256
    }
257
258
    /**
259
     * Is the directive charset directive?
260
     *
261
     * @return bool
262
     */
263
    public function isCharset()
264
    {
265
        return $this->name === '@charset';
266
    }
267
268
    /**
269
     * @return bool
270
     */
271
    public function isRulesetLike()
272
    {
273
        return is_array($this->rules) && !$this->isCharset();
274
    }
275
276
    /**
277
     * @return bool
278
     */
279
    public function getIsReferenced()
280
    {
281
        return !$this->currentFileInfo || !$this->currentFileInfo->reference || $this->isReferenced;
282
    }
283
}
284