Skip to content

AssertArrayHasKey seems to be not supported #100

Closed
phpstan/phpstan-src
#4473

Description

@VincentLanglet

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 ?

I'll try to do a PR in order to add the support, I think I'll be able to copy the way isArray works.
https://github.com/phpstan/phpstan-phpunit/blob/master/src/Type/PHPUnit/Assert/AssertTypeSpecifyingExtensionHelper.php#L158

Activity

  1. ondrejmirtes commented on Jul 1, 2021

    @ondrejmirtes
    Member

    Yes, this should be easy enough.

  2. micheh commented on Jul 15, 2021

    @micheh

    @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).

  3. ondrejmirtes commented on Jul 15, 2021

    @ondrejmirtes
    Member

    @micheh Do you have a false-positive that stems from the current implementation?

  4. micheh commented on Jul 15, 2021

    @micheh

    I think at least that it stems from the current implementation.

    Unit Test:

    public function testArrayHasKey(): void
    {
        $object = new ArrayObject(['test' => 'hello']);
        self::assertArrayHasKey('test', $object);
    
        $object2 = new ArrayObject(['test' => 'hello']);
        if ($object2->offsetExists('test') === false) {
            echo 'hello';
        }
    }

    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.

  5. VincentLanglet commented on Jul 15, 2021

    @VincentLanglet
    ContributorAuthor

    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 ?

  6. villfa commented on May 2, 2023

    @villfa
    Contributor

    To support ArrayAccess properly I think this issue has to be fixed first: phpstan/phpstan#3323

  7. VincentLanglet commented on Aug 9, 2024

    @VincentLanglet
    ContributorAuthor

    With the current implementation,

    '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])
    );
    },

    AssertArrayHasKey don't work anymore with array because of the issue phpstan/phpstan#11276

  8. github-actions commented on Nov 30, 2025

    @github-actions

    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.

  9. locked as resolved and limited conversation to collaborators on Nov 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions