Passed
Push — master ( 3aa121...7cc354 )
by stéphane
03:44
created

Tag::build()   B

Complexity

Conditions 9
Paths 10

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 10.0551

Importance

Changes 0
Metric Value
cc 9
eloc 16
nc 10
nop 1
dl 0
loc 23
ccs 13
cts 17
cp 0.7647
crap 10.0551
rs 8.0555
c 0
b 0
f 0
1
<?php
2
3
namespace Dallgoot\Yaml\Nodes;
4
5
use Dallgoot\Yaml\NodeList;
6
use Dallgoot\Yaml\TagFactory;
7
use Dallgoot\Yaml\Tagged;
8
use Dallgoot\Yaml\Regex;
9
10
/**
11
 *
12
 * @author  Stéphane Rebai <[email protected]>
13
 * @license Apache 2.0
14
 * @link    https://github.com/dallgoot/yaml
15
 * @todo    handle tags like  <tag:clarkevans.com,2002:invoice>
16
 */
17
class Tag extends Actions
18
{
19
20 1
    public function isAwaitingChild(NodeGeneric $node):bool
21
    {
22 1
        return is_null($this->value);
23
    }
24
25 1
    public function getTargetOnEqualIndent(NodeGeneric &$node):NodeGeneric
26
    {
27 1
        if (is_null($this->value)) {
28 1
            return $this;
29
        } else {
30 1
            return $this->getParent();
31
        }
32
    }
33
34
    /**
35
     * Builds a tag and its value (also built) and encapsulates them in a Tag object.
36
     *
37
     * @param array|object|null $parent The parent
38
     *
39
     * @return Tagged|mixed The tag object of class Dallgoot\Yaml\Tagged.
40
     */
41 1
    public function build(&$parent = null)
42
    {
43 1
        if (is_null($this->value) && $this->getParent() instanceof Root) {
44 1
            if (!preg_match(Regex::TAG_PARTS, $this->tag, $matches)) {
45
                throw new \UnexpectedValueException("Tag '$this->tag' is invalid", 1);
46
            }
47 1
            $handle = $matches['handle'];
48 1
            $tagname = $matches['tagname'];
49 1
            $this->getRoot()->getYamlObject()->addTag($handle, $tagname);
50 1
            return;
51
        }
52 1
        $value = $this->value;
53 1
        if (is_null($parent) && $value->isOneOf('Item', 'Key')) {
0 ignored issues
show
Bug introduced by
The method isOneOf() 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

53
        if (is_null($parent) && $value->/** @scrutinizer ignore-call */ isOneOf('Item', 'Key')) {

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 isOneOf() 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

53
        if (is_null($parent) && $value->/** @scrutinizer ignore-call */ isOneOf('Item', 'Key')) {

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...
54
            $value = new NodeList(/** @scrutinizer ignore-type */ $value);
55
        }
56 1
        if ($value instanceof Literals) {
57
            $value = $value->value;
58
        }
59 1
        $transformed = TagFactory::transform((string) $this->tag, $value);
60 1
        if ($transformed instanceof NodeGeneric || $transformed instanceof NodeList) {
61
            return $transformed->build($parent);
62
        }
63 1
        return $transformed;
64
    }
65
}