Completed
Push — master ( 8cc02b...5173bc )
by Christian
02:41
created

ArrowFuncNode   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 3
dl 0
loc 87
ccs 40
cts 40
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A compile() 0 16 3
B evaluate() 0 25 3
A toArray() 0 17 3
A __construct() 0 14 2
1
<?php
2
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace uuf6429\ExpressionLanguage\Node;
13
14
use Symfony\Component\ExpressionLanguage\Node\Node;
15
use Symfony\Component\ExpressionLanguage\Compiler;
16
use uuf6429\ExpressionLanguage\SafeCallable;
17
18
/**
19
 * @author Christian Sciberras <[email protected]>
20
 *
21
 * @internal
22
 */
23
class ArrowFuncNode extends Node
24
{
25
    /**
26
     * @var SafeCallable
27
     */
28
    private static $noopSafeCallable;
29
30
    /**
31
     * @param NameNode[] $parameters
32
     * @param Node|null  $body
33
     */
34 3
    public function __construct(array $parameters, Node $body = null)
35
    {
36 3
        parent::__construct(
37
            array(
38 3
                'parameters' => $parameters,
39 3
                'body' => $body,
40
            )
41
        );
42
43 3
        if (!self::$noopSafeCallable) {
44
            self::$noopSafeCallable = new SafeCallable(function () {
45 1
            });
46
        }
47 3
    }
48
49 3
    public function compile(Compiler $compiler)
50
    {
51 3
        $arguments = array();
52
53 3
        foreach ($this->nodes['parameters'] as $parameterNode) {
54 2
            $arguments[] = $compiler->subcompile($parameterNode);
55
        }
56
57 3
        $compiler->raw(
58
            sprintf(
59 3
                'function (%s) { return %s; }',
60 3
                implode(', ', $arguments),
61 3
                $this->nodes['body'] ? $compiler->subcompile($this->nodes['body']) : 'null'
62
            )
63
        );
64 3
    }
65
66 5
    public function evaluate($functions, $values)
67
    {
68
        /** @var Node|null $bodyNode */
69 5
        $bodyNode = $this->nodes['body'];
70
71 5
        if (!$bodyNode) {
72 1
            return self::$noopSafeCallable;
73
        }
74
75 4
        $paramNames = array();
76
77 4
        foreach ($this->nodes['parameters'] as $parameterNode) {
78
            /** @var NameNode $parameterNode */
79 4
            $nodeData = $parameterNode->toArray();
80 4
            $paramNames[] = $nodeData[0];
81
        }
82
83 4
        return new SafeCallable(
84 4
            function () use ($functions, $paramNames, $bodyNode) {
85 4
                $passedValues = array_combine($paramNames, func_get_args());
86
87 4
                return $bodyNode->evaluate($functions, $passedValues);
88 4
            }
89
        );
90
    }
91
92 4
    public function toArray()
93
    {
94 4
        $array = array();
95
96 4
        foreach ($this->nodes['parameters'] as $node) {
97 3
            $array[] = ', ';
98 3
            $array[] = $node;
99
        }
100 4
        $array[0] = '(';
101 4
        $array[] = ') -> {';
102 4
        if ($this->nodes['body']) {
103 3
            $array[] = $this->nodes['body'];
104
        }
105 4
        $array[] = '}';
106
107 4
        return $array;
108
    }
109
}
110