Skip to content

Commit 45b115f

Browse files
herndlmondrejmirtes
authored andcommitted
Support keyNotExists
1 parent 9bdc258 commit 45b115f

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ This extension specifies types of values passed to:
5757
* `Assert::implementsInterface`
5858
* `Assert::classExists`
5959
* `Assert::interfaceExists`
60+
* `Assert::keyExists`
61+
* `Assert::keyNotExists`
6062
* `Assert::validArrayKey`
6163
* `Assert::count`
6264
* `Assert::minCount`

src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php

+8
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,14 @@ private static function getExpressionResolvers(): array
344344
[$key, $array]
345345
);
346346
},
347+
'keyNotExists' => function (Scope $scope, Arg $array, Arg $key): \PhpParser\Node\Expr {
348+
return new \PhpParser\Node\Expr\BooleanNot(
349+
new \PhpParser\Node\Expr\FuncCall(
350+
new \PhpParser\Node\Name('array_key_exists'),
351+
[$key, $array]
352+
)
353+
);
354+
},
347355
'validArrayKey' => function (Scope $scope, Arg $value): \PhpParser\Node\Expr {
348356
return new \PhpParser\Node\Expr\BinaryOp\BooleanOr(
349357
new \PhpParser\Node\Expr\FuncCall(

tests/Type/WebMozartAssert/data/array.php

+11
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@
66

77
class ArrayTest
88
{
9+
10+
/**
11+
* @param array{foo?: string, bar?: string} $a
12+
*/
13+
public function keyNotExists(array $a): void
14+
{
15+
Assert::keyNotExists($a, 'bar');
16+
\PHPStan\Testing\assertType('array{foo?: string}', $a);
17+
}
18+
919
/**
1020
* @param mixed $a
1121
*/
@@ -17,4 +27,5 @@ public function validArrayKey($a, bool $b): void
1727
Assert::validArrayKey($b);
1828
\PHPStan\Testing\assertType('*NEVER*', $b);
1929
}
30+
2031
}

0 commit comments

Comments
 (0)