ReflectionType   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 95.45%

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 0
dl 0
loc 91
ccs 21
cts 22
cp 0.9545
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A allowsNull() 0 4 1
A isBuiltin() 0 4 1
A __toString() 0 4 1
A convertToDisplayType() 0 26 5
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 ReflectionNamedType;
14
use ReflectionType as BaseReflectionType;
15
16
/**
17
 * ReflectionType implementation
18
 */
19
class ReflectionType extends BaseReflectionType
20
{
21
    /**
22
     * If type allows null or not
23
     *
24
     * @var boolean
25
     */
26
    private $allowsNull;
27
28
    /**
29
     * Is type built-in or not
30
     *
31
     * @var
32
     */
33
    private $isBuiltin;
34
35
    /**
36
     * @var string Type name
37
     */
38
    private $type;
39
40
    /**
41
     * Initializes reflection data
42
     */
43 34
    public function __construct($type, $allowsNull, $isBuiltin)
44
    {
45 34
        $this->type       = $type;
46 34
        $this->allowsNull = $allowsNull;
47 34
        $this->isBuiltin  = $isBuiltin;
48 34
    }
49
50
    /**
51
     * @inheritDoc
52
     */
53 34
    public function allowsNull()
54
    {
55 34
        return $this->allowsNull;
56
    }
57
58
    /**
59
     * @inheritDoc
60
     */
61 2
    public function isBuiltin()
62
    {
63 2
        return $this->isBuiltin;
64
    }
65
66
    /**
67
     * @inheritDoc
68
     */
69 34
    public function __toString()
70
    {
71 34
        return $this->type;
72
    }
73
74
    /**
75
     * PHP reflection has it's own rules, so 'int' type will be displayed as 'integer', etc...
76
     *
77
     * @see https://3v4l.org/nZFiT
78
     *
79
     * @param ReflectionType $type Type to display
80
     *
81
     * @return string
82
     */
83 35
    public static function convertToDisplayType(\ReflectionType $type)
84
    {
85 35
        static $typeMap = [
86
            'int'    => 'integer',
87
            'bool'   => 'boolean',
88
            'double' => 'float',
89
        ];
90
91 35
        if ($type instanceof ReflectionNamedType) {
0 ignored issues
show
Bug introduced by Lisachenko Alexander
The class ReflectionNamedType does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
92 3
            $displayType = $type->getName();
93
        } else {
94 32
            $displayType = (string) $type;
95
        };
96
97 35
        if (PHP_VERSION_ID < 70300 && isset($typeMap[$displayType])) {
98
            $displayType = $typeMap[$displayType];
99
        }
100
101 35
        $displayType = ltrim($displayType, '\\');
102
103 35
        if ($type->allowsNull()) {
104 16
            $displayType .= ' or NULL';
105
        }
106
107 35
        return $displayType;
108
    }
109
}
110