Skip to content

[PHP 8] PDOStatement::getIterator: returned value detected as Traverable and not Iterator #8886

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

Open
srbetasign opened this issue Feb 13, 2023 · 4 comments · May be fixed by phpstan/phpstan-src#3974
Labels
Milestone

Comments

@srbetasign
Copy link

srbetasign commented Feb 13, 2023

Bug report

Hello,

in my current work (PHP 8.2), i have used (for the first time) PDOStatement::getIterator() for a custom Repository :

protected function executeIterator(string $sql, array $params = []): \Iterator
{
    $stmt = $this->dbConn->prepare($sql);
    $stmt->execute($params);
    
    return $stmt->getIterator();
}

The following error occurs when i run phpstan:

AbstractRepository::executeIterator() should return Iterator but returns Traversable<mixed, array<int|string, mixed>>

But according to the documentation, PDOStatement::getIterator returns an Iterator:

public PDOStatement::getIterator(): Iterator

Code snippet that reproduces the problem

Here the URL of a snippet : https://phpstan.org/r/1043c9a2-812c-4a95-bdac-e89b4c865656

Expected output

This error should not be displayed.

Did PHPStan help you today? Did it make you happy in any way?

Ho yes it helps me a lot ! And not only today !

I had once a migration from an older PHP version to PHP 8 and PHPStan was fundamental in my work.
I am no sure i could have done this migration without it!

So many thanks for your work !!

Your work is so helpful that i cannot imagine working without PHPStan !

Thank you again !

@VincentLanglet
Copy link
Contributor

@srbetasign
Copy link
Author

Thank you @VincentLanglet.

If i am not wrong, i should submit a PR with the add of the getIterator() into https://github.com/phpstan/phpstan-src/blob/3103141be78f902ff7caa207d41cc7e80d528e19/stubs/PDOStatement.stub#L8 to solve the issue ?

Thank you again :)

@VincentLanglet
Copy link
Contributor

If i am not wrong, i should submit a PR with the add of the getIterator() into phpstan/phpstan-src@3103141/stubs/PDOStatement.stub#L8 to solve the issue ?

A nice way to check it solve the issue is to add a test to the NodeScopeResolver, like here:
https://github.com/phpstan/phpstan-src/blob/3103141be78f902ff7caa207d41cc7e80d528e19/tests/PHPStan/Analyser/NodeScopeResolverTest.php#L1174

You create a bug-8886 file with your issue and an

assertType()

call on $stmt->getIterator().

@srbetasign
Copy link
Author

Ok, thank you @VincentLanglet :)

@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone Feb 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants