Skip to content

Commit 522c761

Browse files
kaynethondrejmirtes
authored andcommitted
Add support for keyExists and keyNotExists assertions
1 parent 5b6b255 commit 522c761

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/Type/BeberleiAssert/AssertHelper.php

+14
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,20 @@ private static function getExpressionResolvers(): array
375375
[$value]
376376
);
377377
},
378+
'keyExists' => static function (Scope $scope, Arg $array, Arg $key): Expr {
379+
return new FuncCall(
380+
new Name('array_key_exists'),
381+
[$key, $array]
382+
);
383+
},
384+
'keyNotExists' => static function (Scope $scope, Arg $array, Arg $key): Expr {
385+
return new BooleanNot(
386+
new FuncCall(
387+
new Name('array_key_exists'),
388+
[$key, $array]
389+
)
390+
);
391+
},
378392
];
379393
}
380394

tests/Type/BeberleiAssert/data/data.php

+8
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,14 @@ public function doFoo($a, $b, array $c, iterable $d, $e, $f, $g, $h, $i, $j, $k,
111111

112112
Assertion::isJsonString($ac);
113113
\PHPStan\Testing\assertType('string', $ac);
114+
115+
/** @var array{a?: string, b?: int} $ad */
116+
$ad = doFoo();
117+
Assertion::keyExists($ad, 'a');
118+
\PHPStan\Testing\assertType("array{a: string, b?: int}", $ad);
119+
120+
Assertion::keyNotExists($ad, 'b');
121+
\PHPStan\Testing\assertType("array{a: string}", $ad);
114122
}
115123

116124
public function doBar(?int $a, $b, $c, array $d, iterable $e, $g)

0 commit comments

Comments
 (0)