Skip to content

Commit 7c51357

Browse files
committed
Assert::subclassOf
1 parent 9d4c39c commit 7c51357

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ This extension specifies types of values passed to:
2323
* `Assert::isCountable`
2424
* `Assert::isInstanceOf`
2525
* `Assert::notInstanceOf`
26+
* `Assert::subclassOf`
2627
* `Assert::true`
2728
* `Assert::false`
2829
* `Assert::null`

src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,15 @@ private static function getExpressionResolvers(): array
303303
$value2->value
304304
);
305305
},
306+
'subclassOf' => function (Scope $scope, Arg $expr, Arg $class): ?\PhpParser\Node\Expr {
307+
return new \PhpParser\Node\Expr\FuncCall(
308+
new \PhpParser\Node\Name('is_subclass_of'),
309+
[
310+
new Arg($expr->value),
311+
$class,
312+
]
313+
);
314+
},
306315
];
307316
}
308317

tests/Type/WebMozartAssert/AssertTypeSpecifyingExtensionTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,14 @@ public function testExtension(): void
137137
'Variable $z is: array<0|1|2, 1>',
138138
107,
139139
],
140+
[
141+
'Variable $aa is: PHPStan\Type\WebMozartAssert\Foo',
142+
110,
143+
],
144+
[
145+
'Variable $ab is: array<PHPStan\Type\WebMozartAssert\Foo>',
146+
113,
147+
],
140148
]);
141149
}
142150

tests/Type/WebMozartAssert/data/data.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
class Foo
88
{
99

10-
public function doFoo($a, $b, array $c, iterable $d, $e, $f, $g, $h, $i, $j, $k, $l, $m, $n, $o, $p, $r, $s, ?int $t, ?int $u, $x)
10+
public function doFoo($a, $b, array $c, iterable $d, $e, $f, $g, $h, $i, $j, $k, $l, $m, $n, $o, $p, $r, $s, ?int $t, ?int $u, $x, $aa, array $ab)
1111
{
1212
$a;
1313

@@ -105,6 +105,12 @@ public function doFoo($a, $b, array $c, iterable $d, $e, $f, $g, $h, $i, $j, $k,
105105
}
106106
Assert::allNotSame($z, -1);
107107
$z;
108+
109+
Assert::subclassOf($aa, self::class);
110+
$aa;
111+
112+
Assert::allSubclassOf($ab, self::class);
113+
$ab;
108114
}
109115

110116
}

0 commit comments

Comments
 (0)