From 6aaff1196c4f808769774b49a94a60e5fdf18de7 Mon Sep 17 00:00:00 2001 From: Simon Date: Wed, 14 Jul 2021 12:42:31 +0200 Subject: [PATCH] Add support for assertArrayHasKey (#100) --- .../Assert/AssertTypeSpecifyingExtensionHelper.php | 3 +++ tests/Type/PHPUnit/data/assert-function.php | 13 +++++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/Type/PHPUnit/Assert/AssertTypeSpecifyingExtensionHelper.php b/src/Type/PHPUnit/Assert/AssertTypeSpecifyingExtensionHelper.php index 0ec8e87..8a56f32 100644 --- a/src/Type/PHPUnit/Assert/AssertTypeSpecifyingExtensionHelper.php +++ b/src/Type/PHPUnit/Assert/AssertTypeSpecifyingExtensionHelper.php @@ -252,6 +252,9 @@ private static function getExpressionResolvers(): array ] ); }, + 'ArrayHasKey' => function (Scope $scope, Arg $key, Arg $array): FuncCall { + return new \PhpParser\Node\Expr\FuncCall(new Name('array_key_exists'), [$key, $array]); + }, ]; } diff --git a/tests/Type/PHPUnit/data/assert-function.php b/tests/Type/PHPUnit/data/assert-function.php index 5c1cc96..f31fb37 100644 --- a/tests/Type/PHPUnit/data/assert-function.php +++ b/tests/Type/PHPUnit/data/assert-function.php @@ -3,6 +3,8 @@ namespace AssertFunction; use function PHPStan\Testing\assertType; +use function PHPUnit\Framework\assertArrayHasKey; +use function PHPUnit\Framework\assertArrayNotHasKey; use function PHPUnit\Framework\assertInstanceOf; class Foo @@ -17,4 +19,15 @@ public function doFoo($o): void assertType(self::class, $o); } + public function arrayHasNumericKey(array $a): void { + assertArrayHasKey(0, $a); + assertType('array&hasOffset(0)', $a); + } + + public function arrayHasStringKey(array $a): void + { + assertArrayHasKey('key', $a); + assertType("array&hasOffset('key')", $a); + } + }