ReflectionType::allowsNull()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * Parser Reflection API
4
 *
5
 * @copyright Copyright 2015, Lisachenko Alexander <[email protected]>
6
 *
7
 * This source file is subject to the license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
11
namespace Go\ParserReflection;
12
13
use ReflectionType as BaseReflectionType;
14
15
/**
16
 * ReflectionType implementation
17
 */
18
class ReflectionType extends BaseReflectionType
19
{
20
    /**
21
     * If type allows null or not
22
     *
23
     * @var boolean
24
     */
25
    private $allowsNull;
26
27
    /**
28
     * Is type built-in or not
29
     *
30
     * @var
31
     */
32
    private $isBuiltin;
33
34
    /**
35
     * @var string Type name
36
     */
37
    private $type;
38
39
    /**
40
     * Initializes reflection data
41
     */
42 32
    public function __construct($type, $allowsNull, $isBuiltin)
43
    {
44 32
        $this->type       = $type;
45 32
        $this->allowsNull = $allowsNull;
46 32
        $this->isBuiltin  = $isBuiltin;
47 32
    }
48
49
    /**
50
     * @inheritDoc
51
     */
52 32
    public function allowsNull()
53
    {
54 32
        return $this->allowsNull;
55
    }
56
57
    /**
58
     * @inheritDoc
59
     */
60 2
    public function isBuiltin()
61
    {
62 2
        return $this->isBuiltin;
63
    }
64
65
    /**
66
     * @inheritDoc
67
     */
68 32
    public function __toString()
69
    {
70 32
        return $this->type;
71
    }
72
73
    /**
74
     * PHP reflection has it's own rules, so 'int' type will be displayed as 'integer', etc...
75
     *
76
     * @see https://3v4l.org/nZFiT
77
     *
78
     * @param ReflectionType $type Type to display
79
     *
80
     * @return string
81
     */
82 33
    public static function convertToDisplayType(\ReflectionType $type)
83
    {
84 33
        static $typeMap = [
85
            'int'  => 'integer',
86
            'bool' => 'boolean'
87
        ];
88 33
        $displayType = (string)$type;
89 33
        if (isset($typeMap[$displayType])) {
90 11
            $displayType = $typeMap[$displayType];
91
        }
92
93 33
        $displayType = ltrim($displayType, '\\');
94
95 33
        if ($type->allowsNull()) {
96 16
            $displayType .= ' or NULL';
97
        }
98
99 33
        return $displayType;
100
    }
101
}
102