Passed
Push — master ( dd457c...82211b )
by Arne
03:19
created

TypeAwareTrait::isBuiltInType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 2
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
namespace TheSeer\phpDox;
3
4
trait TypeAwareTrait
5
{
6 15
    public function getBuiltInTypes(int $context = TypeAwareInterface::TYPE_ALL): array {
7
        /*
8
         * From:
9
         * http://docs.phpdoc.org/guides/types.html#primitives
10
         * http://docs.phpdoc.org/guides/types.html#keywords
11
         */
12 15
        $docblockType = ['string', 'int', 'integer', 'float', 'boolean', 'bool', 'array', 'resource', 'null', 'callable',
13
            'mixed', 'void', 'object', 'true', 'false', 'self', 'static', '$this'];
14
15 15
        $phpdoxType = ['', '{unknown}'];
16
17
        /*
18
         * From:
19
         * https://www.php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration
20
         * + callable (https://www.php.net/manual/en/migration70.new-features.php#migration70.new-features.scalar-type-declarations)
21
         */
22 15
        $variableType = ['object', 'array', 'int',
23
            'float', 'string', 'callable', 'iterable', 'object', 'self', 'bool'];
24
        /*
25
         * From:
26
         * https://www.php.net/manual/en/functions.returning-values.php#functions.returning-values.type-declaration
27
         * + void (https://www.php.net/manual/en/migration71.new-features.php#migration71.new-features.void-functions)
28
         */
29 15
        $returnType = $variableType + ['void'];
30
31 15
        $type = [];
32
33
        switch (true) {
34 15
            case ($context & TypeAwareInterface::TYPE_RETURN) !== 0:
35 15
                $type = array_merge($type, $returnType);
36
                // no-break
37 15
            case ($context & TypeAwareInterface::TYPE_PHPDOC) !== 0:
38 15
                $type = array_merge($type, $docblockType);
39
                // no-break
40
            case ($context & TypeAwareInterface::TYPE_BUILTIN) !== 0:
41 15
                $type = array_merge($type, $variableType);
42
                // no-break
43
            case ($context & TypeAwareInterface::TYPE_PHPDOX) !== 0:
44 15
                $type = array_merge($type, $phpdoxType);
45
                // no-break
46
        }
47
48 15
        return array_unique($type);
49
    }
50 15
    public function isBuiltInType(string $type, int $context = TypeAwareInterface::TYPE_ALL): bool {
51 15
        return \in_array(\mb_strtolower($type), $this->getBuiltInTypes($context), true);
52
    }
53
}
54