Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect PHPStan Error for Method Return Type Declaration #544

Open
bedfir opened this issue Feb 13, 2024 · 7 comments
Open

Incorrect PHPStan Error for Method Return Type Declaration #544

bedfir opened this issue Feb 13, 2024 · 7 comments

Comments

@bedfir
Copy link

bedfir commented Feb 13, 2024

Hi All,

I encountered an issue where PHPStan threw an incorrect error regarding method return type declaration. Here's the scenario:

Problem:
I have a method getData() in my codebase which fetches data from the database and returns an array of associative arrays. The return type declaration of this method is specified accurately. However, PHPStan still throws this error suggesting that the return type is incorrect.

 Method                                                                                                           

         App\Repository\FakeRepository::getData()  

         should return array<array{key1: string, key2: string, key3: string,                   
         key4: int, key5: int}> but returns array.  
/**
 * @return array<array{
 *     "key1": string,
 *     "key2": string,
 *     "key3": string,
 *     "key4": int,
 *     "key5": int
 * }>
 */
public function getData(): array
{
    $result = $this->createQueryBuilder('a')
        ->select(.....)
        ->where('a.status = :validated')
        ->groupBy(.....)
        ->setParameter('validated', Status::VALIDATED);

    return $result
        ->getQuery()
        ->getArrayResult();
}

Additional Information:
"php": ">=8.3",
"phpstan/extension-installer": "^1.3.1",
"phpstan/phpstan": "^1.10.47",
"phpstan/phpstan-deprecation-rules": "^1.1.4",
"phpstan/phpstan-doctrine": "^1.3.53",
"phpstan/phpstan-phpunit": "^1.3.15",
"phpstan/phpstan-symfony": "^1.3.5",

Thank you for your attention to this matter. Please let me know if you need any further information or clarification.

@ondrejmirtes
Copy link
Member

Please make sure you have the latest versions of PHPStan and phpstan-doctrine installed. And please make sure you have configured objectManagerLoader. See the README here.

@VincentLanglet
Copy link
Contributor

I think this is because getArrayResult is not supported yet.
You may avoid the issue with getResult() until #520 is merged.

@ThomasLandauer
Copy link
Contributor

#520 is now merged, but I'm still seeing this issue.
In short:

/**
 * @return array<int, array{'id': int}>
 */
public function foo(): array
{
    $queryBuilder = $this->createQueryBuilder('e')
    $queryBuilder->select('e.id');
    return $queryBuilder->getQuery()->getArrayResult();
}

PHPStan is reporting:

Method ... should return array<int, array{id: int}> but returns array.

@ondrejmirtes
Copy link
Member

@ThomasLandauer getArrayResult has been reverted again in phpstan-doctrine 1.4.4.

@ThomasLandauer
Copy link
Contributor

OK, so the relevant PR is now #593 ?

@ondrejmirtes
Copy link
Member

Yes

@VincentLanglet
Copy link
Contributor

Currently you can resolve your issue by using getResult

/**
 * @return array<int, array{'id': int}>
 */
public function foo(): array
{
    $queryBuilder = $this->createQueryBuilder('e')
    $queryBuilder->select('e.id');
    return $queryBuilder->getQuery()->getResult();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants