FunctionExpression::__toString()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 0
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace PeacefulBit\Slate\Parser\Nodes;
4
5
use function Nerd\Common\Strings\indent;
6
7
use PeacefulBit\Slate\Core\Frame;
8
9
class FunctionExpression extends LambdaExpression
10
{
11
    /**
12
     * @var string
13
     */
14
    private $id;
15
16
    /**
17
     * @param string $id
18
     * @param array $params
19
     * @param Node $body
20
     */
21
    public function __construct(string $id, array $params, Node $body)
22
    {
23
        $this->id = $id;
24
25
        parent::__construct($params, $body);
26
    }
27
28
    /**
29
     * @return string
30
     */
31
    public function getId(): string
32
    {
33
        return $this->id;
34
    }
35
36
    /**
37
     * @return string
38
     */
39
    public function __toString()
40
    {
41
        $prefix = '(def ';
42
        $suffix = ')';
43
44
        $signature = array_merge([$this->getId()], $this->getParams());
45
        $signatureString = '(' . implode(' ', array_map('strval', $signature)) . ')';
46
47
        $body = strval($this->getBody());
48
49
        $indentedBody = strlen($body) <= self::INLINE_THRESHOLD ? ' '  . $body : PHP_EOL . indent(2, $body);
50
51
        return $prefix . $signatureString . $indentedBody . $suffix;
52
    }
53
54
    /**
55
     * @param Frame $frame
56
     * @return null
57
     */
58
    public function evaluate(Frame $frame)
59
    {
60
        $frame->set($this->getId(), $this);
61
62
        return null;
63
    }
64
}
65