Skip to content

Commit a570df6

Browse files
committed
Type::allows() accepts Type
1 parent 70082cd commit a570df6

2 files changed

Lines changed: 9 additions & 2 deletions

File tree

src/Utils/Type.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,13 +233,13 @@ public function isClassKeyword(): bool
233233
/**
234234
* Verifies type compatibility. For example, it checks if a value of a certain type could be passed as a parameter.
235235
*/
236-
public function allows(string $type): bool
236+
public function allows(string|self $type): bool
237237
{
238238
if ($this->types === ['mixed']) {
239239
return true;
240240
}
241241

242-
$type = self::fromString($type);
242+
$type = is_string($type) ? self::fromString($type) : $type;
243243
return $type->isUnion()
244244
? Arrays::every($type->types, fn($t) => $this->allowsAny($t instanceof self ? $t->types : [$t]))
245245
: $this->allowsAny($type->types);

tests/Utils/Type.allows.phpt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,10 @@ Assert::true($type->allows('Foo&Bar'));
9292
Assert::true($type->allows('FooChild&Bar'));
9393
Assert::true($type->allows('Foo&Bar&Baz'));
9494
Assert::true($type->allows('(Foo&Bar&Baz)|null'));
95+
96+
97+
// allows() with Type object
98+
$type = Type::fromString('string|int');
99+
Assert::true($type->allows(Type::fromString('string')));
100+
Assert::true($type->allows(Type::fromString('int')));
101+
Assert::false($type->allows(Type::fromString('bool')));

0 commit comments

Comments
 (0)