Passed
Pull Request — 1.x (#3)
by Kevin
01:22
created

Argument   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 53
c 1
b 0
f 0
dl 0
loc 154
rs 10
wmc 28

8 Methods

Rating   Name   Duplication   Size   Complexity  
A isUnionType() 0 3 1
A types() 0 3 1
C supports() 0 49 16
A __construct() 0 3 1
A type() 0 3 2
A hasType() 0 3 1
A allows() 0 5 3
A reflectionTypes() 0 12 3
1
<?php
2
3
namespace Zenstruck\Callback;
4
5
/**
6
 * @author Kevin Bond <[email protected]>
7
 */
8
final class Argument
9
{
10
    /**
11
     * If type is class, parent classes are supported.
12
     */
13
    public const COVARIANCE = 2;
14
15
    /**
16
     * If type is class, child classes are supported.
17
     */
18
    public const CONTRAVARIANCE = 4;
19
20
    /**
21
     * If type is string, do not support other scalar types. Follows
22
     * same logic as "declare(strict_types=1)".
23
     */
24
    public const STRICT = 8;
25
26
    /**
27
     * If type is float, do not support int (implies {@see STRICT).
28
     */
29
    public const VERY_STRICT = 16;
30
31
    private const TYPE_NORMALIZE_MAP = [
32
        'boolean' => 'bool',
33
        'integer' => 'int',
34
        'double' => 'float',
35
        'resource (closed)' => 'resource',
36
    ];
37
38
    private const ALLOWED_TYPE_MAP = [
39
        'string' => ['bool', 'int', 'float'],
40
        'bool' => ['string', 'int', 'float'],
41
        'float' => ['string', 'int', 'bool'],
42
        'int' => ['string', 'float', 'bool'],
43
    ];
44
45
    /** @var \ReflectionParameter */
46
    private $parameter;
47
48
    /** @var \ReflectionNamedType[] */
49
    private $types = [];
0 ignored issues
show
introduced by
The private property $types is not used, and could be removed.
Loading history...
50
51
    public function __construct(\ReflectionParameter $parameter)
52
    {
53
        $this->parameter = $parameter;
54
    }
55
56
    public function type(): ?string
57
    {
58
        return $this->hasType() ? \implode('|', $this->types()) : null;
59
    }
60
61
    /**
62
     * @return string[]
63
     */
64
    public function types(): array
65
    {
66
        return \array_map(static function(\ReflectionNamedType $type) { return $type->getName(); }, $this->reflectionTypes());
67
    }
68
69
    public function hasType(): bool
70
    {
71
        return !empty($this->types());
72
    }
73
74
    public function isUnionType(): bool
75
    {
76
        return \count($this->types()) > 1;
77
    }
78
79
    /**
80
     * @param string $type    The type to check if this argument supports
81
     * @param int    $options {@see COVARIANCE}, {@see CONTRAVARIANCE}
82
     *                        Bitwise disjunction of above is allowed
83
     */
84
    public function supports(string $type, int $options = self::COVARIANCE): bool
85
    {
86
        if (!$this->hasType()) {
87
            // no type-hint so any type is supported
88
            return true;
89
        }
90
91
        if ('null' === \mb_strtolower($type) && $this->parameter->allowsNull()) {
92
            return true;
93
        }
94
95
        $type = self::TYPE_NORMALIZE_MAP[$type] ?? $type;
96
97
        foreach ($this->types() as $supportedType) {
98
            if ($supportedType === $type) {
99
                return true;
100
            }
101
102
            if ($options & self::COVARIANCE && \is_a($type, $supportedType, true)) {
103
                return true;
104
            }
105
106
            if ($options & self::CONTRAVARIANCE && \is_a($supportedType, $type, true)) {
107
                return true;
108
            }
109
110
            if ($options & self::VERY_STRICT) {
111
                continue;
112
            }
113
114
            if ('float' === $supportedType && 'int' === $type) {
115
                // strict typing allows int to pass a float validation
116
                return true;
117
            }
118
119
            if ($options & self::STRICT) {
120
                continue;
121
            }
122
123
            if (\in_array($type, self::ALLOWED_TYPE_MAP[$supportedType] ?? [], true)) {
124
                return true;
125
            }
126
127
            if (\method_exists($type, '__toString')) {
128
                return true;
129
            }
130
        }
131
132
        return false;
133
    }
134
135
    /**
136
     * @param mixed $value
137
     * @param bool  $strict {@see STRICT}
138
     */
139
    public function allows($value, bool $strict = false): bool
140
    {
141
        return $this->supports(
142
            \is_object($value) ? \get_class($value) : \gettype($value),
143
            $strict ? self::COVARIANCE|self::STRICT : self::COVARIANCE
144
        );
145
    }
146
147
    /**
148
     * @return \ReflectionNamedType[]
149
     */
150
    private function reflectionTypes(): array
151
    {
152
        if (!$type = $this->parameter->getType()) {
153
            return [];
154
        }
155
156
        if ($type instanceof \ReflectionNamedType) {
157
            return [$type];
158
        }
159
160
        /** @var \ReflectionUnionType $type */
161
        return $type->getTypes();
162
    }
163
}
164