Skip to content

Commit f53f3cb

Browse files
herndlmondrejmirtes
authored andcommitted
Support validArrayKey
1 parent 83546e1 commit f53f3cb

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ This extension specifies types of values passed to:
5555
* `Assert::implementsInterface`
5656
* `Assert::classExists`
5757
* `Assert::interfaceExists`
58+
* `Assert::validArrayKey`
5859
* `Assert::minCount`
5960
* `Assert::inArray`
6061
* `Assert::oneOf`

src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php

+12
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,18 @@ private static function getExpressionResolvers(): array
320320
[$key, $array]
321321
);
322322
},
323+
'validArrayKey' => function (Scope $scope, Arg $value): \PhpParser\Node\Expr {
324+
return new \PhpParser\Node\Expr\BinaryOp\BooleanOr(
325+
new \PhpParser\Node\Expr\FuncCall(
326+
new \PhpParser\Node\Name('is_int'),
327+
[$value]
328+
),
329+
new \PhpParser\Node\Expr\FuncCall(
330+
new \PhpParser\Node\Name('is_string'),
331+
[$value]
332+
)
333+
);
334+
},
323335
'true' => function (Scope $scope, Arg $expr): \PhpParser\Node\Expr {
324336
return new \PhpParser\Node\Expr\BinaryOp\Identical(
325337
$expr->value,

tests/Type/WebMozartAssert/AssertTypeSpecifyingExtensionTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class AssertTypeSpecifyingExtensionTest extends TypeInferenceTestCase
1212
*/
1313
public function dataFileAsserts(): iterable
1414
{
15+
yield from $this->gatherAssertTypes(__DIR__ . '/data/array.php');
1516
yield from $this->gatherAssertTypes(__DIR__ . '/data/data.php');
1617
}
1718

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Webmozart\Assert\Assert;
6+
7+
class Foo
8+
{
9+
/**
10+
* @param mixed $a
11+
*/
12+
public function validArrayKey($a, bool $b): void
13+
{
14+
Assert::validArrayKey($a);
15+
\PHPStan\Testing\assertType('int|string', $a);
16+
17+
Assert::validArrayKey($b);
18+
\PHPStan\Testing\assertType('*NEVER*', $b);
19+
}
20+
}

0 commit comments

Comments
 (0)