Passed
Push — 7.x ( 42e260...f51c13 )
by Adrien
09:26
created

SplReflectionNamedType::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Ducks\Component\SplTypes\Reflection;
4
5
/**
6
 * ReflectionNamedType mock Class
7
 *
8
 * @psalm-immutable
9
 */
10
class SplReflectionNamedType extends \ReflectionNamedType
11
{
12
    private const BUILT_IN_TYPES = [
13
        'int',
14
        'float',
15
        'string',
16
        'bool',
17
        'callable',
18
        'self',
19
        'parent',
20
        'array',
21
        'iterable',
22
        'object',
23
        'void',
24
        'mixed',
25
        'static',
26
        'null',
27
        'never',
28
        'false',
29
        'true',
30
    ];
31
32
    /**
33
     * The $name replacement
34
     *
35
     * @var string
36
     *
37
     * @phpstan-var non-empty-string
38
     */
39
    private string $naming;
40
41
    /**
42
     * The $allowsNull replacement
43
     *
44
     * @var boolean
45
     */
46
    private bool $nullable;
47
48
    /**
49
     * Build a fake \ReflectionNamedType
50
     *
51
     * @param string $name
52
     * @param boolean $nullable
53
     *
54
     * @phpstan-param non-empty-string $name
55
     * @phpstan-param boolean $nullable
56
     */
57
    public function __construct(string $name, bool $nullable = false)
58
    {
59
        $this->naming = $name;
60
61
        // Force nullable for native type name
62
        switch (\strtolower($name)) {
63
            case 'mixed':
64
            case 'null':
65
                $this->nullable = true;
66
                break;
67
            default:
68
                $this->nullable = $nullable;
69
                break;
70
        }
71
    }
72
73
    /**
74
     * @inheritDoc
75
     *
76
     * @see https://www.php.net/manual/en/reflectionnamedtype.getname.php
77
     */
78
    public function getName(): string
79
    {
80
        return $this->naming;
81
    }
82
83
    /**
84
     * @inheritDoc
85
     *
86
     * @see https://php.net/manual/en/reflectiontype.isbuiltin.php
87
     */
88
    public function isBuiltin(): bool
89
    {
90
        return \in_array(\strtolower($this->getName()), self::BUILT_IN_TYPES);
91
    }
92
93
    /**
94
     * @inheritDoc
95
     *
96
     * @see https://www.php.net/manual/fr/reflectiontype.allowsnull.php
97
     */
98
    public function allowsNull(): bool
99
    {
100
        return $this->nullable;
101
    }
102
103
    /**
104
     * @inheritDoc
105
     *
106
     * @see https://www.php.net/manual/fr/reflectiontype.tostring.php
107
     */
108
    public function __toString(): string
109
    {
110
        return $this->getName();
111
    }
112
}
113