Skip to content

Commit a46f664

Browse files
herndlmondrejmirtes
authored andcommitted
Support isAOf
1 parent b9233c3 commit a46f664

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ This extension specifies types of values passed to:
4646
* `Assert::isCountable`
4747
* `Assert::isInstanceOf`
4848
* `Assert::notInstanceOf`
49+
* `Assert::isAOf`
4950
* `Assert::subclassOf`
5051
* `Assert::true`
5152
* `Assert::false`

src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php

+10
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
use PHPStan\Type\MixedType;
3939
use PHPStan\Type\ObjectType;
4040
use PHPStan\Type\StaticMethodTypeSpecifyingExtension;
41+
use PHPStan\Type\StringType;
4142
use PHPStan\Type\Type;
4243
use PHPStan\Type\TypeCombinator;
4344
use PHPStan\Type\TypeUtils;
@@ -382,6 +383,15 @@ private static function getExpressionResolvers(): array
382383
)
383384
);
384385
},
386+
'isAOf' => static function (Scope $scope, Arg $expr, Arg $class): Expr {
387+
$exprType = $scope->getType($expr->value);
388+
$allowString = (new StringType())->isSuperTypeOf($exprType)->yes();
389+
390+
return new FuncCall(
391+
new Name('is_a'),
392+
[$expr, $class, new Arg(new ConstFetch(new Name($allowString ? 'true' : 'false')))]
393+
);
394+
},
385395
'implementsInterface' => static function (Scope $scope, Arg $expr, Arg $class): ?Expr {
386396
$classType = $scope->getType($class->value);
387397
if (!$classType instanceof ConstantStringType) {

tests/Type/WebMozartAssert/data/type.php

+22
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,24 @@ public function notInstanceOf($a, $b, $c): void
191191
\PHPStan\Testing\assertType('PHPStan\Type\WebMozartAssert\Bar|PHPStan\Type\WebMozartAssert\Foo', $c);
192192
}
193193

194+
public function isAOf($a, $b, string $c): void
195+
{
196+
Assert::isAOf($a, stdClass::class);
197+
\PHPStan\Testing\assertType('stdClass', $a);
198+
199+
Assert::isAOf(Foo::class, stdClass::class);
200+
\PHPStan\Testing\assertType('*NEVER*', Foo::class);
201+
202+
Assert::isAOf(Bar::class, stdClass::class);
203+
\PHPStan\Testing\assertType('\'PHPStan\\\Type\\\WebMozartAssert\\\Bar\'', Bar::class);
204+
205+
Assert::nullOrIsAOf($b, stdClass::class);
206+
\PHPStan\Testing\assertType('stdClass|null', $b);
207+
208+
Assert::isAOf($c, stdClass::class);
209+
\PHPStan\Testing\assertType('class-string<stdClass>', $c);
210+
}
211+
194212
public function isArrayAccessible($a, $b): void
195213
{
196214
Assert::isArrayAccessible($a);
@@ -200,3 +218,7 @@ public function isArrayAccessible($a, $b): void
200218
\PHPStan\Testing\assertType('array|ArrayAccess|null', $b);
201219
}
202220
}
221+
222+
class Foo {}
223+
224+
class Bar extends stdClass {}

0 commit comments

Comments
 (0)