MacroNode::getNativeValue()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
/**
3
 * This file is part of NACL.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @copyright 2019 Nuglif (2018) Inc.
9
 * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
10
 * @author    Pierrick Charron <[email protected]>
11
 * @author    Charle Demers <[email protected]>
12
 */
13
14
declare(strict_types=1);
15
16
namespace Nuglif\Nacl;
17
18
class MacroNode extends Node
19
{
20
    private $callback;
21
    private mixed $param;
22
    private ObjectNode $options;
23
    private mixed $value;
24
    private bool $isResolved = false;
25
26 10
    public function __construct(callable $callback, mixed $param, ObjectNode $options)
27
    {
28 10
        $this->callback = $callback;
29 10
        $this->param    = $param;
30 10
        $this->options  = $options;
31
    }
32
33 6
    public function execute(): mixed
34
    {
35 6
        $result = $this->getNativeValue();
36
37 6
        if (is_array($result)) {
38 3
            $result = is_int(key($result)) ? new ArrayNode(array_values($result)) : new ObjectNode($result);
39
        }
40
41 6
        return $result;
42
    }
43
44 9
    public function getNativeValue(): mixed
45
    {
46 9
        if (!$this->isResolved) {
47 9
            $this->resolve();
48
        }
49
50 9
        return $this->value;
51
    }
52
53 9
    private function resolve(): void
54
    {
55 9
        $callback    = $this->callback;
56 9
        $this->value = $callback(
57 9
            $this->param instanceof Node ? $this->param->getNativeValue() : $this->param,
58 9
            $this->options->getNativeValue()
59 9
        );
60 9
        $this->isResolved = true;
61
    }
62
63 6
    public function setParent(Node $parent): void
64
    {
65 6
        if ($this->param instanceof Node) {
66 3
            $this->param->setParent($parent);
67
        }
68
    }
69
}
70