ArrowFuncNode::compile()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 7
cts 7
cp 1
rs 9.7333
c 0
b 0
f 0
cc 3
nc 2
nop 1
crap 3
1
<?php
2
3
namespace uuf6429\ExpressionLanguage\Node;
4
5
use Symfony\Component\ExpressionLanguage\Node\NameNode;
6
use Symfony\Component\ExpressionLanguage\Node\Node;
7
use Symfony\Component\ExpressionLanguage\Compiler;
8
use uuf6429\ExpressionLanguage\SafeCallable;
9
10
/**
11
 * @author Christian Sciberras <[email protected]>
12
 *
13
 * @internal
14
 */
15
class ArrowFuncNode extends Node
16
{
17
    /**
18
     * @var SafeCallable
19
     */
20
    private static $noopSafeCallable;
21
22
    /**
23
     * @param NameNode[] $parameters
24
     * @param Node|null $body
25
     */
26
    public function __construct(array $parameters, Node $body = null)
27
    {
28
        parent::__construct(
29
            array(
30
                'parameters' => $parameters,
31
                'body' => $body,
32
            )
33
        );
34 3
35
        if (!self::$noopSafeCallable) {
36 3
            self::$noopSafeCallable = new SafeCallable(static function () {
37
            });
38 3
        }
39 3
    }
40
41
    public function compile(Compiler $compiler): void
42
    {
43 3
        $arguments = array();
44
45 1
        foreach ($this->nodes['parameters'] as $parameterNode) {
46
            $arguments[] = $compiler->subcompile($parameterNode);
47 3
        }
48
49 3
        $compiler->raw(
50
            sprintf(
51 3
                'function (%s) { return %s; }',
52
                implode(', ', $arguments),
53 3
                $this->nodes['body'] ? $compiler->subcompile($this->nodes['body']) : 'null'
54 2
            )
55
        );
56
    }
57 3
58
    public function evaluate($functions, $values)
59 3
    {
60 3
        /** @var Node|null $bodyNode */
61 3
        $bodyNode = $this->nodes['body'];
62
63
        if (!$bodyNode) {
64 3
            return self::$noopSafeCallable;
65
        }
66 5
67
        $paramNames = array();
68
69 5
        foreach ($this->nodes['parameters'] as $parameterNode) {
70
            /** @var NameNode $parameterNode */
71 5
            $nodeData = $parameterNode->toArray();
72 1
            $paramNames[] = $nodeData[0];
73
        }
74
75 4
        return new SafeCallable(
76
            static function () use ($functions, $paramNames, $bodyNode) {
77 4
                $passedValues = array_combine($paramNames, func_get_args());
78
79 4
                return $bodyNode->evaluate($functions, $passedValues);
80 4
            }
81
        );
82
    }
83 4
84 4
    public function toArray()
85 4
    {
86
        $array = array();
87 4
88 4
        foreach ($this->nodes['parameters'] as $node) {
89
            $array[] = ', ';
90
            $array[] = $node;
91
        }
92 4
        $array[0] = '(';
93
        $array[] = ') -> {';
94 4
        if ($this->nodes['body']) {
95
            $array[] = $this->nodes['body'];
96 4
        }
97 3
        $array[] = '}';
98 3
99
        return $array;
100 4
    }
101
}
102