Tag::build()   B
last analyzed

Complexity

Conditions 10
Paths 8

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 29.4003

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 25
ccs 8
cts 19
cp 0.4211
rs 7.6666
c 0
b 0
f 0
cc 10
nc 8
nop 1
crap 29.4003

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\Tag\TagFactory;
7
use Dallgoot\Yaml\Types\Tagged;
8
use Dallgoot\Yaml\Regex;
9
use Dallgoot\Yaml\Nodes\Generic\NodeGeneric;
10
use Dallgoot\Yaml\Nodes\Generic\Actions;
11
12
/**
13
 *
14
 * @author  Stéphane Rebai <[email protected]>
15
 * @license Apache 2.0
16
 * @link    https://github.com/dallgoot/yaml
17
 * @todo    handle tags like  <tag:clarkevans.com,2002:invoice>
18
 */
19
class Tag extends Actions
20
{
21
22 1
    public function isAwaitingChild(NodeGeneric $node): bool
23
    {
24 1
        return is_null($this->value);
25
    }
26
27 1
    public function getTargetOnEqualIndent(NodeGeneric &$node): NodeGeneric
28
    {
29 1
        if (is_null($this->value) && $this->indent > 0) {
30
            return $this;
31
        } else {
32 1
            return $this->getParent();
33
        }
34
    }
35
36
    /**
37
     * Builds a tag and its value (also built) and encapsulates them in a Tag object.
38
     *
39
     * @param array|object|null $parent The parent
40
     *
41
     * @return Tagged|mixed The tag object of class Dallgoot\Yaml\Types\Tagged.
42
     */
43 1
    public function build(&$parent = null)
44
    {
45 1
        if (is_null($this->value) && $this->getParent() instanceof Root) {
46
            if (!preg_match(Regex::TAG_PARTS, (string) $this->tag, $matches)) {
47
                throw new \UnexpectedValueException("Tag '$this->tag' is invalid", 1);
48
            }
49
            $handle = $matches['handle'];
50
            $tagname = $matches['tagname'];
51
            $this->getRoot()->getYamlObject()->addTag($handle, $tagname);
52
            return;
53
        }
54 1
        $value = $this->value;
55 1
        if (is_null($parent) && $value instanceof NodeGeneric && $value->isOneOf('Item', 'Key')) {
56
            $value = new NodeList(
57
                /** @scrutinizer ignore-type */
58
                $value
59
            );
60
        }
61
        try {
62 1
            $transformed = TagFactory::transform((string) $this->tag, $value, $parent);
63 1
            return $transformed;
64 1
        } catch (\UnexpectedValueException $e) {
65 1
            return new Tagged((string) $this->tag, is_null($value) ? null : ((object) $this->value)->build($parent));
66
        } catch (\Throwable $e) {
67
            throw new \Exception("Tagged value could not be transformed for tag '$this->tag'", 1, $e);;
68
        }
69
    }
70
}
71