diff --git a/src/Type/PHPUnit/Assert/AssertTypeSpecifyingExtensionHelper.php b/src/Type/PHPUnit/Assert/AssertTypeSpecifyingExtensionHelper.php index 2e8e828..a7edf21 100644 --- a/src/Type/PHPUnit/Assert/AssertTypeSpecifyingExtensionHelper.php +++ b/src/Type/PHPUnit/Assert/AssertTypeSpecifyingExtensionHelper.php @@ -262,8 +262,14 @@ private static function getExpressionResolvers(): array ] ); }, - 'ArrayHasKey' => static function (Scope $scope, Arg $key, Arg $array): FuncCall { - return new FuncCall(new Name('array_key_exists'), [$key, $array]); + 'ArrayHasKey' => static function (Scope $scope, Arg $key, Arg $array): Expr { + return new Expr\BinaryOp\BooleanOr( + new Expr\BinaryOp\BooleanAnd( + new Expr\Instanceof_($array->value, new Name('ArrayAccess')), + new Expr\MethodCall($array->value, 'offsetExists', [$key]) + ), + new FuncCall(new Name('array_key_exists'), [$key, $array]) + ); }, 'ObjectHasAttribute' => static function (Scope $scope, Arg $property, Arg $object): FuncCall { return new FuncCall(new Name('property_exists'), [$object, $property]); diff --git a/tests/Type/PHPUnit/data/assert-function.php b/tests/Type/PHPUnit/data/assert-function.php index f3222f3..9af1d26 100644 --- a/tests/Type/PHPUnit/data/assert-function.php +++ b/tests/Type/PHPUnit/data/assert-function.php @@ -31,15 +31,21 @@ public function assertInstanceOfWorksWithTemplate($o, $class): void assertType(\DateTimeInterface::class, $o); } - public function arrayHasNumericKey(array $a): void { + public function arrayHasNumericKey(array $a, \ArrayAccess $b): void { assertArrayHasKey(0, $a); assertType('array&hasOffset(0)', $a); + + assertArrayHasKey(0, $b); + assertType('ArrayAccess', $b); } - public function arrayHasStringKey(array $a): void + public function arrayHasStringKey(array $a, \ArrayAccess $b): void { assertArrayHasKey('key', $a); assertType("array&hasOffset('key')", $a); + + assertArrayHasKey('key', $b); + assertType("ArrayAccess", $b); } public function objectHasAttribute(object $a): void