Completed
Push — master ( c45521...844759 )
by stéphane
08:22
created

NodeItem::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 2
nop 2
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Dallgoot\Yaml;
4
5
/**
6
 *
7
 * @author  Stéphane Rebai <[email protected]>
8
 * @license Apache 2.0
9
 * @link    TODO : url to specific online doc
10
 */
11
class NodeItem extends Node
12
{
13
    public function __construct(string $nodeString, int $line)
14
    {
15
        parent::__construct($nodeString, $line);
16
        preg_match(Regex::ITEM, $nodeString, $matches);
17
        if (isset($matches[1]) && !empty(trim($matches[1]))) {
18
            // $n->indent = $indent + 2;
19
            $n = NodeFactory::get($matches[1], $line);
20
            $this->add($n);
21
        }
22
    }
23
    /**
24
     * Modify parent target when current Node indentation is equal to previous node indentation
25
     *
26
     * @param Node $previous The previous
27
     *
28
     * @return Node
29
     */
30
    public function getTargetonEqualIndent(Node &$previous):Node
31
    {
32
        $deepest = $previous->getDeepestNode();
33
        if ($deepest->isAwaitingChildren()) {
34
            return $deepest;
35
        } else {
36
            return parent::getTargetonEqualIndent($previous);
37
        }
38
    }
39
40
41
    /**
42
     * Builds an item. Adds the item value to the parent array|Iterator
43
     *
44
     * @param array|\Iterator|null $parent The parent
45
     *
46
     * @throws \Exception  if parent is another type than array or object Iterator
47
     * @return null
48
     */
49
    public function build(&$parent = null):void
50
    {
51
        // extract((array) $node, EXTR_REFS);
52
        if (!is_array($parent) && !($parent instanceof \ArrayIterator)) {
53
            throw new \Exception("parent must be an ArrayIterator not ".(is_object($parent) ? get_class($parent) : gettype($parent)));
54
        }
55
        $ref = $parent instanceof \ArrayIterator ? $parent->getArrayCopy() : $parent;
0 ignored issues
show
introduced by
$parent is never a sub-type of ArrayIterator.
Loading history...
56
        $numKeys = array_filter(array_keys($ref), 'is_int');
57
        $key = count($numKeys) > 0 ? max($numKeys) + 1 : 0;
58
        if ($this->value instanceof NodeKey) {
59
            $this->value->build($parent);
60
            return;
61
        }
62
        $parent[$key] = $this->value->build();
0 ignored issues
show
Bug introduced by
The method build() does not exist on Dallgoot\Yaml\NodeList. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
        /** @scrutinizer ignore-call */ 
63
        $parent[$key] = $this->value->build();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method build() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
        /** @scrutinizer ignore-call */ 
63
        $parent[$key] = $this->value->build();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
63
    }
64
65
}