CallNode::compile()   B
last analyzed

Complexity

Conditions 4
Paths 12

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 14
nc 12
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\Exception\FunctionException;
14
use ILess\FileInfo;
15
use ILess\FunctionRegistry;
16
use ILess\Node;
17
use ILess\Output\OutputInterface;
18
use ILess\Visitor\VisitorInterface;
19
20
/**
21
 * Function call.
22
 */
23
class CallNode extends Node
24
{
25
    /**
26
     * Node type.
27
     *
28
     * @var string
29
     */
30
    protected $type = 'Call';
31
32
    /**
33
     * The function name.
34
     *
35
     * @var string
36
     */
37
    protected $name;
38
39
    /**
40
     * Array of arguments.
41
     *
42
     * @var array
43
     */
44
    protected $args = [];
45
46
    /**
47
     * The index.
48
     *
49
     * @var int
50
     */
51
    protected $index = 0;
52
53
    /**
54
     * Constructor.
55
     *
56
     * @param string $name Name of the function
57
     * @param array $args Array of arguments
58
     * @param int $index The current index
59
     * @param FileInfo $currentFileInfo The current file info
60
     */
61
    public function __construct($name, array $args, $index = 0, FileInfo $currentFileInfo = null)
62
    {
63
        $this->name = $name;
64
        $this->args = $args;
65
        $this->index = $index;
66
        $this->currentFileInfo = $currentFileInfo;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function accept(VisitorInterface $visitor)
73
    {
74
        if ($this->args) {
75
            $this->args = $visitor->visitArray($this->args);
0 ignored issues
show
Documentation Bug introduced by
It seems like $visitor->visitArray($this->args) of type * is incompatible with the declared type array of property $args.

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...
76
        }
77
    }
78
79
    /**
80
     * Compiles the node.
81
     *
82
     * @param Context $context The context
83
     * @param array|null $arguments Array of arguments
84
     * @param bool|null $important Important flag
85
     *
86
     * @return CallNode|Node
87
     *
88
     * @throws FunctionException
89
     */
90
    public function compile(Context $context, $arguments = null, $important = null)
91
    {
92
        // prepare arguments
93
        $args = [];
94
        foreach ($this->args as $arg) {
95
            /* @var $arg Node */
96
            $args[] = $arg->compile($context);
97
        }
98
99
        try {
100
            $registry = $context->frames[0]->functionRegistry;
101
            /* @var $registry FunctionRegistry */
102
            $registry->setCurrentFile($this->currentFileInfo);
103
            $result = $registry->call($this->name, $args);
104
105
            if ($result === null) {
106
                $result = new self($this->name, $args, $this->index, $this->currentFileInfo);
107
            }
108
        } catch (\Exception $e) {
109
            throw new FunctionException(sprintf('Error evaluating function `%s`', $this->name), $this->index,
110
                $this->currentFileInfo, $e);
111
        }
112
113
        return $result;
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119
    public function generateCSS(Context $context, OutputInterface $output)
120
    {
121
        // handle IE filters, cannot accept shortened colors
122
        if (strpos($this->name, 'progid:') === 0) {
123
            $canShortenColors = $context->canShortenColors;
124
            $context->canShortenColors = false;
125
        }
126
127
        $output->add($this->name . '(', $this->currentFileInfo, $this->index);
128
        for ($i = 0, $count = count($this->args); $i < $count; ++$i) {
129
            $this->args[$i]->generateCSS($context, $output);
130
            if ($i + 1 < $count) {
131
                $output->add(', ');
132
            }
133
        }
134
135
        if (isset($canShortenColors)) {
136
            // put it back
137
            $context->canShortenColors = $canShortenColors;
138
        }
139
140
        $output->add(')');
141
    }
142
}
143