Passed
Push — master ( f81cc4...5281ab )
by stéphane
04:50
created

Tag::build()   C

Complexity

Conditions 12
Paths 26

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 19.964

Importance

Changes 0
Metric Value
cc 12
eloc 21
nc 26
nop 1
dl 0
loc 32
ccs 13
cts 21
cp 0.619
crap 19.964
rs 6.9666
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\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
                    // var_dump($matches['handle'], $matches['tagname']);
48 1
            $handle = $matches['handle'];
49 1
            $tagname = $matches['tagname'];
50 1
            $this->getRoot()->getYamlObject()->addTag($handle, $tagname);
0 ignored issues
show
Unused Code introduced by
The call to Dallgoot\Yaml\YamlObject::addTag() has too many arguments starting with $tagname. ( Ignorable by Annotation )

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

50
            $this->getRoot()->getYamlObject()->/** @scrutinizer ignore-call */ addTag($handle, $tagname);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
51 1
            return;
52
        }
53 1
        $value = $this->value;
54 1
        if (is_null($parent) && $value->isOneOf('Item', 'Key')) {
0 ignored issues
show
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

54
        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 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

54
        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...
55
            $value = new NodeList(/** @scrutinizer ignore-type */ $value);
56
        }
57
        // if (TagFactory::isKnown((string) $this->tag)) {
58
        try {
59 1
            if ($value instanceof Literals) {
60
                $value = $value->value;
61
            }
62 1
            $transformed = TagFactory::transform((string) $this->tag, $value);
63 1
            if ($transformed instanceof NodeGeneric || $transformed instanceof NodeList) {
64
                return $transformed->build($parent);
65
            }
66 1
            return $transformed;
67
        // } else {
68
        } catch (\Throwable $e) {
69
            if ($e instanceof \UnexpectedValueException) {
70
                return new Tagged($this->tag, is_null($value) ? null : $value->build($parent));
0 ignored issues
show
Bug introduced by
It seems like $this->tag can also be of type null; however, parameter $tagName of Dallgoot\Yaml\Tagged::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

70
                return new Tagged(/** @scrutinizer ignore-type */ $this->tag, is_null($value) ? null : $value->build($parent));
Loading history...
71
            }
72
            throw new \Exception("Can NOT build Tag", 1, $e);
73
        }
74
75
    }
76
}