Completed
Push — master ( 8afd90...4bf010 )
by Kirill
03:48
created

InputValueNode::getValues()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 7
ccs 0
cts 6
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of Railt package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Railt\SDL\Compiler\Ast\Value;
11
12
use Railt\Parser\Ast\LeafInterface;
13
use Railt\Parser\Ast\NodeInterface;
14
use Railt\Parser\Ast\Rule;
15
use Railt\Parser\Ast\RuleInterface;
16
17
/**
18
 * Class InputValueNode
19
 */
20
class InputValueNode extends Rule implements ValueInterface
21
{
22
    /**
23
     * @return string
24
     */
25
    public function toString(): string
26
    {
27
        $result = [];
28
29
        foreach ($this->getValues() as $key => $value) {
30
            $result[] = $key->getValue() . ': ' . $value->toString();
0 ignored issues
show
Bug introduced by
The method getValue cannot be called on $key (of type integer|string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
31
        }
32
33
        return \sprintf('{%s}', \implode(', ', $result));
34
    }
35
36
    /**
37
     * @return iterable|ValueInterface[]
38
     */
39
    public function toPrimitive(): iterable
40
    {
41
        $result = [];
42
43
        foreach ($this->getValues() as $key => $value) {
44
            $result[$key->getValue()] = $value->toPrimitive();
0 ignored issues
show
Bug introduced by
The method getValue cannot be called on $key (of type integer|string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
45
        }
46
47
        return $result;
48
    }
49
50
    /**
51
     * @return iterable|ValueInterface[]
52
     */
53
    public function getValues(): iterable
54
    {
55
        /** @var RuleInterface $child */
56
        foreach ($this->getChildren() as $child) {
57
            yield $this->key($child) => $this->value($child)->getInnerValue();
58
        }
59
    }
60
61
    /**
62
     * @param RuleInterface $rule
63
     * @return LeafInterface
64
     */
65
    private function key(RuleInterface $rule): LeafInterface
66
    {
67
        /** @var RuleInterface $key */
68
        $key = $rule->first('Key', 1);
69
70
        return $key->getChild(0);
71
    }
72
73
    /**
74
     * @param RuleInterface $rule
75
     * @return ValueInterface|NodeInterface
76
     */
77
    private function value(RuleInterface $rule): ValueInterface
78
    {
79
        /** @var ValueNode $value */
80
        return $rule->first('Value', 1);
81
    }
82
}
83