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

Argument::allows()   B

Complexity

Conditions 8
Paths 7

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 10
nc 7
nop 2
dl 0
loc 21
rs 8.4444
c 0
b 0
f 0
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
    public function __construct(\ReflectionParameter $parameter)
49
    {
50
        $this->parameter = $parameter;
51
    }
52
53
    public function type(): ?string
54
    {
55
        return $this->hasType() ? \implode('|', $this->types()) : null;
56
    }
57
58
    /**
59
     * @return string[]
60
     */
61
    public function types(): array
62
    {
63
        return \array_map(static function(\ReflectionNamedType $type) { return $type->getName(); }, $this->reflectionTypes());
64
    }
65
66
    public function hasType(): bool
67
    {
68
        return !empty($this->types());
69
    }
70
71
    public function isUnionType(): bool
72
    {
73
        return \count($this->types()) > 1;
74
    }
75
76
    /**
77
     * @param string $type    The type to check if this argument supports
78
     * @param int    $options {@see COVARIANCE}, {@see CONTRAVARIANCE}
79
     *                        Bitwise disjunction of above is allowed
80
     */
81
    public function supports(string $type, int $options = self::COVARIANCE): bool
82
    {
83
        if (!$this->hasType()) {
84
            // no type-hint so any type is supported
85
            return true;
86
        }
87
88
        if ('null' === \mb_strtolower($type) && $this->parameter->allowsNull()) {
89
            return true;
90
        }
91
92
        $type = self::TYPE_NORMALIZE_MAP[$type] ?? $type;
93
94
        foreach ($this->types() as $supportedType) {
95
            if ($supportedType === $type) {
96
                return true;
97
            }
98
99
            if ($options & self::COVARIANCE && \is_a($type, $supportedType, true)) {
100
                return true;
101
            }
102
103
            if ($options & self::CONTRAVARIANCE && \is_a($supportedType, $type, true)) {
104
                return true;
105
            }
106
107
            if ($options & self::VERY_STRICT) {
108
                continue;
109
            }
110
111
            if ('float' === $supportedType && 'int' === $type) {
112
                // strict typing allows int to pass a float validation
113
                return true;
114
            }
115
116
            if ($options & self::STRICT) {
117
                continue;
118
            }
119
120
            if (\in_array($type, self::ALLOWED_TYPE_MAP[$supportedType] ?? [], true)) {
121
                return true;
122
            }
123
124
            if (\method_exists($type, '__toString')) {
125
                return true;
126
            }
127
        }
128
129
        return false;
130
    }
131
132
    /**
133
     * @param mixed $value
134
     * @param bool  $strict {@see STRICT}
135
     */
136
    public function allows($value, bool $strict = false): bool
137
    {
138
        if (!$this->hasType()) {
139
            // no type-hint so any type is supported
140
            return true;
141
        }
142
143
        $type = \is_object($value) ? \get_class($value) : \gettype($value);
144
        $type = self::TYPE_NORMALIZE_MAP[$type] ?? $type;
145
        $supports = $this->supports($type, $strict ? self::COVARIANCE|self::STRICT : self::COVARIANCE);
146
147
        if (!$supports) {
148
            return false;
149
        }
150
151
        if ('string' === $type && !\is_numeric($value) && !\in_array('string', $this->types(), true)) {
152
            // non-numeric strings cannot be used for float/int
153
            return false;
154
        }
155
156
        return true;
157
    }
158
159
    /**
160
     * @return \ReflectionNamedType[]
161
     */
162
    private function reflectionTypes(): array
163
    {
164
        if (!$type = $this->parameter->getType()) {
165
            return [];
166
        }
167
168
        if ($type instanceof \ReflectionNamedType) {
169
            return [$type];
170
        }
171
172
        /** @var \ReflectionUnionType $type */
173
        return $type->getTypes();
174
    }
175
}
176