Completed
Pull Request — master (#82)
by Loren
03:39
created

ReflectionType::wasIncluded()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
ccs 0
cts 5
cp 0
rs 9.2
cc 4
eloc 6
nc 4
nop 0
crap 20
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 implements ReflectionInterface
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 80
    public function __construct($type, $allowsNull, $isBuiltin)
43
    {
44 80
        $this->type       = $type;
45 80
        $this->allowsNull = $allowsNull;
46 80
        $this->isBuiltin  = $isBuiltin;
47 80
    }
48
49
    /**
50
     * @inheritDoc
51
     */
52 80
    public function allowsNull()
53
    {
54 80
        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 80
    public function __toString()
69
    {
70 80
        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 81
    public static function convertToDisplayType(\ReflectionType $type)
83
    {
84 81
        static $typeMap = [
85
            'int'  => 'integer',
86
            'bool' => 'boolean'
87
        ];
88 81
        $displayType = (string)$type;
89 81
        if (isset($typeMap[$displayType])) {
90 28
            $displayType = $typeMap[$displayType];
91
        }
92
93 81
        $displayType = ltrim($displayType, '\\');
94
95 81
        if ($type->allowsNull()) {
96 32
            $displayType .= ' or NULL';
97
        }
98
99 81
        return $displayType;
100
    }
101
102
    /**
103
     * Has class been loaded by PHP.
104
     *
105
     * @return bool
106
     *     If class file was included.
107
     */
108
    public function wasIncluded()
109
    {
110
        return
111
            $this->isBuiltin()                   ||
112
            interface_exists($this->type, false) ||
113
            trait_exists($this->type, false) ||
114
            class_exists($this->type, false);
115
    }
116
}
117