Completed
Push — 1.x ( d5d19c...d72eab )
by Kevin
15s queued 13s
created

Argument::defaultValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
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
     * Allow exact type (always enabled).
12
     */
13
    public const EXACT = 0;
14
15
    /**
16
     * If type is class, parent classes are supported.
17
     */
18
    public const COVARIANCE = 2;
19
20
    /**
21
     * If type is class, child classes are supported.
22
     */
23
    public const CONTRAVARIANCE = 4;
24
25
    /**
26
     * If type is string, do not support other scalar types. Follows
27
     * same logic as "declare(strict_types=1)".
28
     */
29
    public const STRICT = 8;
30
31
    /**
32
     * If type is float, do not support int (implies {@see STRICT).
33
     */
34
    public const VERY_STRICT = 16;
35
36
    private const TYPE_NORMALIZE_MAP = [
37
        'boolean' => 'bool',
38
        'integer' => 'int',
39
        'double' => 'float',
40
        'resource (closed)' => 'resource',
41
    ];
42
43
    private const ALLOWED_TYPE_MAP = [
44
        'string' => ['bool', 'int', 'float'],
45
        'bool' => ['string', 'int', 'float'],
46
        'float' => ['string', 'int', 'bool'],
47
        'int' => ['string', 'float', 'bool'],
48
    ];
49
50
    /** @var \ReflectionParameter */
51
    private $parameter;
52
53
    public function __construct(\ReflectionParameter $parameter)
54
    {
55
        $this->parameter = $parameter;
56
    }
57
58
    public function type(): ?string
59
    {
60
        return $this->hasType() ? \implode('|', $this->types()) : null;
61
    }
62
63
    /**
64
     * @return string[]
65
     */
66
    public function types(): array
67
    {
68
        return \array_map(static function(\ReflectionNamedType $type) { return $type->getName(); }, $this->reflectionTypes());
69
    }
70
71
    public function hasType(): bool
72
    {
73
        return !empty($this->types());
74
    }
75
76
    public function isUnionType(): bool
77
    {
78
        return \count($this->types()) > 1;
79
    }
80
81
    public function isOptional(): bool
82
    {
83
        return $this->parameter->isOptional();
84
    }
85
86
    /**
87
     * @return mixed
88
     */
89
    public function defaultValue()
90
    {
91
        return $this->parameter->getDefaultValue();
92
    }
93
94
    /**
95
     * @param string $type    The type to check if this argument supports
96
     * @param int    $options {@see EXACT}, {@see COVARIANCE}, {@see CONTRAVARIANCE}
97
     *                        Bitwise disjunction of above is allowed
98
     */
99
    public function supports(string $type, int $options = self::EXACT|self::COVARIANCE): bool
100
    {
101
        if (!$this->hasType()) {
102
            // no type-hint so any type is supported
103
            return true;
104
        }
105
106
        if ('null' === \mb_strtolower($type) && $this->parameter->allowsNull()) {
107
            return true;
108
        }
109
110
        $type = self::TYPE_NORMALIZE_MAP[$type] ?? $type;
111
112
        foreach ($this->types() as $supportedType) {
113
            if ($supportedType === $type) {
114
                return true;
115
            }
116
117
            if ($options & self::COVARIANCE && \is_a($type, $supportedType, true)) {
118
                return true;
119
            }
120
121
            if ($options & self::CONTRAVARIANCE && \is_a($supportedType, $type, true)) {
122
                return true;
123
            }
124
125
            if ($options & self::VERY_STRICT) {
126
                continue;
127
            }
128
129
            if ('float' === $supportedType && 'int' === $type) {
130
                // strict typing allows int to pass a float validation
131
                return true;
132
            }
133
134
            if ($options & self::STRICT) {
135
                continue;
136
            }
137
138
            if (\in_array($type, self::ALLOWED_TYPE_MAP[$supportedType] ?? [], true)) {
139
                return true;
140
            }
141
142
            if (\method_exists($type, '__toString')) {
143
                return true;
144
            }
145
        }
146
147
        return false;
148
    }
149
150
    /**
151
     * @param mixed $value
152
     * @param bool  $strict {@see STRICT}
153
     */
154
    public function allows($value, bool $strict = false): bool
155
    {
156
        if (!$this->hasType()) {
157
            // no type-hint so any type is supported
158
            return true;
159
        }
160
161
        $type = \is_object($value) ? \get_class($value) : \gettype($value);
162
        $type = self::TYPE_NORMALIZE_MAP[$type] ?? $type;
163
        $options = $strict ? self::EXACT|self::COVARIANCE|self::STRICT : self::EXACT|self::COVARIANCE;
164
        $supports = $this->supports($type, $options);
165
166
        if (!$supports) {
167
            return false;
168
        }
169
170
        if ('string' === $type && !\is_numeric($value) && !\in_array('string', $this->types(), true)) {
171
            // non-numeric strings cannot be used for float/int
172
            return false;
173
        }
174
175
        return true;
176
    }
177
178
    /**
179
     * @return \ReflectionNamedType[]
180
     */
181
    private function reflectionTypes(): array
182
    {
183
        if (!$type = $this->parameter->getType()) {
184
            return [];
185
        }
186
187
        if ($type instanceof \ReflectionNamedType) {
188
            return [$type];
189
        }
190
191
        /** @var \ReflectionUnionType $type */
192
        return $type->getTypes();
193
    }
194
}
195