You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi @ondrejmirtes, I just got an issue with my tests saying that an offset does not exist in an array.
I tried to add a assertArrayHasKey check, without success.
I think this assert is not handle by this plugin, am I wrong ?
@ondrejmirtes I'm not sure it the implementation in commit 6aaff11 is complete. assertArrayHasKey accepts an ArrayObject as well and PHPUnit will use offsetExists if an object is provided, but in the commit PHPStan will only allow arrays (as it uses array_key_exists).
In version 0.12.21 PHPStan now reports the error for the PHPUnit assertion ($object), but no error for the conditional ($object2): Call to static method PHPUnit\Framework\Assert::assertArrayHasKey() with 'test' and ArrayObject<string, string> will always evaluate to false.
Activity
ondrejmirtes commentedon Jul 1, 2021
Yes, this should be easy enough.
Add support for assertArrayHasKey (#100)
micheh commentedon Jul 15, 2021
@ondrejmirtes I'm not sure it the implementation in commit 6aaff11 is complete.
assertArrayHasKeyaccepts anArrayObjectas well and PHPUnit will useoffsetExistsif an object is provided, but in the commit PHPStan will only allow arrays (as it usesarray_key_exists).ondrejmirtes commentedon Jul 15, 2021
@micheh Do you have a false-positive that stems from the current implementation?
micheh commentedon Jul 15, 2021
I think at least that it stems from the current implementation.
Unit Test:
In version 0.12.21 PHPStan now reports the error for the PHPUnit assertion (
$object), but no error for the conditional ($object2):Call to static method PHPUnit\Framework\Assert::assertArrayHasKey() with 'test' and ArrayObject<string, string> will always evaluate to false.VincentLanglet commentedon Jul 15, 2021
According to the phpdoc
https://github.com/sebastianbergmann/phpunit/blob/adeede29ca716930efb49ebe0fe1a5cab38af9c4/src/Framework/Assert.php#L95
ArrayAccess has to be supported too indeed.
Can you provide a fix @micheh ?
villfa commentedon May 2, 2023
To support ArrayAccess properly I think this issue has to be fixed first: phpstan/phpstan#3323
VincentLanglet commentedon Aug 9, 2024
With the current implementation,
phpstan-phpunit/src/Type/PHPUnit/Assert/AssertTypeSpecifyingExtensionHelper.php
Lines 292 to 300 in f3ea021
AssertArrayHasKeydon't work anymore witharraybecause of the issue phpstan/phpstan#11276github-actions commentedon Nov 30, 2025
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.