CompactMapping   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
eloc 15
dl 0
loc 25
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A build() 0 11 3
1
<?php
2
3
namespace Dallgoot\Yaml\Nodes;
4
5
use Dallgoot\Yaml\NodeFactory;
6
use Dallgoot\Yaml\NodeList;
7
use Dallgoot\Yaml\Types\Compact;
8
use Dallgoot\Yaml\Regex;
9
use Dallgoot\Yaml\Nodes\Generic\NodeGeneric;
10
11
/**
12
 *
13
 * @author  Stéphane Rebai <[email protected]>
14
 * @license Apache 2.0
15
 * @link    https://github.com/dallgoot/yaml
16
 */
17
class CompactMapping extends NodeGeneric
18
{
19 2
    public function __construct(string $nodeString, ?int $line)
20
    {
21 2
        parent::__construct($nodeString, $line);
22 2
        preg_match_all(Regex::MAPPING_VALUES, trim(substr(trim($nodeString), 1, -1)), $matches);
23 2
        foreach ($matches['k'] as $index => $property) {
24 2
            $pair = $property . ': ' . trim($matches['v'][$index]);
25 2
            $child = NodeFactory::get($pair, (int) $line);
26 2
            $child->indent = null;
27 2
            $this->add($child);
28
        }
29
    }
30
31 1
    public function build(&$parent = null): ?Compact
32
    {
33 1
        if (is_null($this->value)) {
34 1
            return null;
35
        }
36 1
        if ($this->value instanceof NodeGeneric) {
37 1
            $this->value = new NodeList($this->value);
38 1
            $this->value->type = NodeList::MAPPING;
39
        }
40 1
        $obj = (object) $this->value->build();
41 1
        return new Compact($obj);
42
    }
43
}
44