Passed
Push — master ( abd6e9...1b457a )
by Arne
02:53
created

VarElement::typeResolver()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 22
ccs 0
cts 17
cp 0
rs 9.7333
c 0
b 0
f 0
cc 4
nc 4
nop 2
crap 20
1
<?php declare(strict_types = 1);
2
namespace TheSeer\phpDox\DocBlock;
3
4
class VarElement extends GenericElement {
5
    public const XMLNS = 'http://xml.phpdox.net/src';
6
7
    /**
8
     * @var string[]
9
     */
10
    private $types = [
11
        '', 'null', 'mixed', '{unknown}', 'object', 'array', 'integer', 'int',
12
        'float', 'string', 'boolean', 'resource'
13
    ];
14
15 13
    public function asDom(\TheSeer\fDOM\fDOMDocument $ctx) {
16 13
        $node = parent::asDom($ctx);
17 13
        $type = $node->getAttribute('type');
18
19 13
        $types = explode('|', $type);
20
21 13
        if (count($types) > 1) {
22
            $node->setAttribute('type', 'mixed');
23
24
            foreach ($types as $oneType) {
25
                $this->typeResolver($node, $oneType);
26
            }
27
28
            return $node;
29
        }
30
31 13
        if (\strpos($type, '[]')) {
32
            $type = \mb_substr($type, 0, -2);
33
            $node->setAttribute('type', 'array');
34
            $node->setAttribute('of', $type);
35
        }
36
37 13
        if (!\in_array($type, $this->types)) {
38 1
            if (!$node->hasAttribute('of')) {
39 1
                $node->setAttribute('type', 'object');
40
            } else {
41
                $node->setAttribute('of', 'object');
42
            }
43 1
            $parts     = \explode('\\', $type);
44 1
            $local     = \array_pop($parts);
45 1
            $namespace = \implode('\\', $parts);
46
47 1
            $class = $node->appendElementNS(self::XMLNS, 'type');
48 1
            $class->setAttribute('full', $type);
49 1
            $class->setAttribute('namespace', $namespace);
50 1
            $class->setAttribute('name', $local);
51
        }
52
53 13
        return $node;
54
    }
55
56
    protected function typeResolver(\TheSeer\fDOM\fDOMElement $node, string $type) {
57
        $isArray = false;
58
        if (substr($type, -2, 2) === '[]') {
59
            $isArray = true;
60
            $type = substr($type, 0, -2);
61
        }
62
        $nodeType = $node->appendElementNS(self::XMLNS, 'type');
63
        $nodeType->setAttribute('full', $type);
64
        $nodeType->setAttribute('array', $isArray?'true':'false');
65
66
        if (\in_array($type, $this->types, true)) {
67
            $nodeType->setAttribute('name', $type);
68
            $nodeType->setAttribute('namespace', '');
69
            return;
70
        }
71
72
        $parts     = \explode('\\', $type);
73
        $local     = \array_pop($parts);
74
        $namespace = \implode('\\', $parts);
75
76
        $nodeType->setAttribute('namespace', $namespace);
77
        $nodeType->setAttribute('name', $local);
78
    }
79
}
80