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

GenericParser::lookupOneType()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 27
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 8.8142

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 27
ccs 8
cts 12
cp 0.6667
rs 8.8333
c 0
b 0
f 0
cc 7
nc 6
nop 1
crap 8.8142
1
<?php declare(strict_types = 1);
2
namespace TheSeer\phpDox\DocBlock;
3
4
class GenericParser {
5
    protected $factory;
6
7
    protected $aliasMap;
8
9
    protected $name;
10
11
    protected $payload;
12
13
    private $types = [
14
        '', 'null', 'mixed', '{unknown}', 'object', 'array', 'integer', 'int',
15
        'float', 'string', 'boolean', 'resource'
16
    ];
17
18 25
    public function __construct(Factory $factory, $name) {
19 25
        $this->factory = $factory;
20 25
        $this->name    = $name;
21 25
    }
22
23 25
    public function setAliasMap(array $map): void {
24 25
        $this->aliasMap = $map;
25 25
    }
26
27 23
    public function setPayload($payload): void {
28 23
        $this->payload = \trim($payload);
29 23
    }
30
31 10
    public function getObject(array $buffer) {
32 10
        $this->payload .= ' ' . \implode(' ', \array_map('trim', $buffer));
33 10
        $obj = $this->buildObject('generic', []);
34 10
        $obj->setValue(\trim($this->payload));
35
36 10
        return $obj;
37
    }
38
39 25
    protected function buildObject($classname, array $buffer) {
40 25
        $obj = $this->factory->getElementInstanceFor($classname, $this->name);
41
42 25
        if (\count($buffer)) {
43 6
            $obj->setBody(\trim(\implode("\n", $buffer)));
44
        }
45
46 25
        return $obj;
47
    }
48
49 13
    protected function lookupType($type) {
50 13
        $types = explode('|', $type);
51
52 13
        foreach ($types as &$oneType) {
53 13
            $isArray = false;
54 13
            if (substr($oneType, -2, 2) === '[]') {
55
                $isArray = true;
56
                $oneType = substr($oneType, 0, -2);
57
            }
58
59 13
            $oneType = $this->lookupOneType($oneType);
60
61 13
            if ($isArray) {
62 13
                $oneType .= '[]';
63
            }
64
        }
65
66 13
        return implode('|', $types);
67
    }
68
69 13
    protected function lookupOneType($type) {
70 13
        if ($type === 'self' || $type === 'static') {
71
            return $this->aliasMap['::unit'];
72
        }
73
74
        // Do not mess with scalar and fixed types
75 13
        if (\in_array($type, $this->types)) {
76 12
            return $type;
77
        }
78
79
        // absolute definition?
80 1
        if ($type[0] == '\\') {
81
            return $type;
82
        }
83
84
        // alias?
85 1
        if (isset($this->aliasMap[$type])) {
86
            return $this->aliasMap[$type];
87
        }
88
89
        // relative to local namespace?
90 1
        if (isset($this->aliasMap['::context'])) {
91
            return $this->aliasMap['::context'] . '\\' . $type;
92
        }
93
94
        // don't know any better ..
95 1
        return $type;
96
    }
97
}
98