Passed
Push — master ( 42571c...f8295f )
by stéphane
09:20
created

Key::isAwaitingChild()   B

Complexity

Conditions 10
Paths 15

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 10.1626

Importance

Changes 0
Metric Value
cc 10
eloc 16
nc 15
nop 1
dl 0
loc 25
ccs 15
cts 17
cp 0.8824
crap 10.1626
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Dallgoot\Yaml\Nodes;
4
5
use Dallgoot\Yaml\NodeFactory;
6
use Dallgoot\Yaml\Regex;
7
8
/**
9
 *
10
 * @author  Stéphane Rebai <[email protected]>
11
 * @license Apache 2.0
12
 * @link    https://github.com/dallgoot/yaml
13
 */
14
class Key extends NodeGeneric
15
{
16
    const ERROR_NO_KEYNAME = self::class.": key has NO IDENTIFIER on line %d";
17
18 9
    public function __construct(string $nodeString, int $line, array $matches = null)
19
    {
20 9
        parent::__construct($nodeString, $line);
21 9
        if (is_null($matches)) {
22 9
            if (!((bool) preg_match(Regex::KEY, ltrim($nodeString), $matches))) {
23 1
                throw new \ParseError("Not a KEY:VALUE syntax ($nodeString)", 1);
24
            }
25
        }
26 9
        $this->setIdentifier($matches[1]);
27
28 9
        $value = isset($matches[2]) ? trim($matches[2]) : null;
29 9
        if (!empty($value)) {
30 9
            $child = NodeFactory::get($value, $line);
31 9
            $child->indent = null;
32 9
            $this->add($child);
33
        }
34 9
    }
35
36 9
    public function setIdentifier(string $keyString)
37
    {
38 9
        if ($keyString === '') {
39 1
           throw new \ParseError(sprintf(self::ERROR_NO_KEYNAME, $this->line));
40
        } else {
41 9
            $node = NodeFactory::get($keyString);
42 9
            if ($node->isOneOf('Tag', 'Quoted')) {
43 2
                $built = $node->build();
44 2
                if (is_object($built)) {
45
                    $this->identifier = $built->value;
46
                } else {
47 2
                    $this->identifier = (string) $node->build();
48
                }
49 9
            } elseif ($node instanceof Scalar) {
50 9
                $this->identifier = trim($node->raw);
51
            }
52
        }
53 9
    }
54
55 9
    public function add(NodeGeneric $child):NodeGeneric
56
    {
57 9
        if ($this->value instanceof NodeGeneric && $this->value->isOneOf('Literal','LiteralFolded', 'Anchor')) {
58 1
            return $this->value->add($child);
59
        } else {
60 9
            return parent::add($child);
61
        }
62
    }
63
64 1
    public function getTargetOnEqualIndent(NodeGeneric &$node):NodeGeneric
65
    {
66 1
        if ($node instanceof Item) {
67 1
            return $this;
68
        }
69 1
        return $this->getParent();
70
    }
71
72 1
    public function getTargetOnMoreIndent(NodeGeneric &$node):NodeGeneric
73
    {
74 1
        if (!is_null($this->value)) {
75 1
            if ($this->getDeepestNode()->isAwaitingChild($node)) {
76 1
                return $this->getDeepestNode();
77
            }
78
        }
79 1
        return $this;
80
    }
81
82
83 1
    public function isAwaitingChild(NodeGeneric $node):bool
84
    {
85 1
        if (is_null($this->value) || $node instanceof Comment) {
86
            return true;
87
        }
88 1
        $current = $this->value instanceof NodeGeneric ? $this->value : $this->value->current();
89 1
        if ($current instanceof Comment) {
90 1
            return true;
91
        }
92 1
        if($current instanceof Scalar) {
93 1
            return $node->isOneOf('Scalar', 'Blank');
94
        }
95 1
        if ($current instanceof Item) {
96 1
            return $node instanceof Item;
97
        }
98 1
        if ($current instanceof Key) {
99 1
            return $node instanceof Key;
100
        }
101 1
        if ($current instanceof Literals) {
102 1
            return $node->indent > $this->indent;
103
        }
104 1
        if ($current instanceof Anchor) {
105 1
            return $current->isAwaitingChild($node);
106
        }
107
        return false;
108
    }
109
110
    /**
111
     * Builds a key and set the property + value to the given parent
112
     *
113
     * @param object|array $parent The parent
114
     *
115
     * @throws \ParseError if Key has no name(identifier) Note: empty string is allowed
116
     * @return null|\StdClass
117
     */
118 1
    public function build(&$parent = null)
119
    {
120
        // var_dump("DEBUG KEY:".$this->identifier);
121 1
        if ($this->value instanceof Anchor) {
122
            $result = &$this->value->build();
123
        } else {
124 1
            $result = is_null($this->value) ? null : $this->value->build();
125
        }
126 1
        if (is_null($parent)) {
127 1
            $parent = new \StdClass;
128 1
            $parent->{$this->identifier} = &$result;
129 1
            return $parent;
130
        } else {
131 1
            $parent->{$this->identifier} = &$result;
132
        }
133
    }
134
}