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 stub for RecursiveIterator, return value of getChildren() should be nullable #10327

Closed
sad-spirit opened this issue Oct 28, 2023 · 2 comments

Comments

@sad-spirit
Copy link
Contributor

The docs for RecursiveIterator and stubs in PHP source specify the return value of getChildren() as ?RecursiveIterator, while psalm complains in case of null return value:

https://psalm.dev/r/602a63575d

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/602a63575d
<?php
class Foo {}

class ManyFoos extends Foo
{
    /** @var Foo[] */
    public array $children = [];
}

/**
 * @extends RecursiveArrayIterator<array-key, Foo>
 */
class RecursiveFoos extends RecursiveArrayIterator
{
    public function hasChildren(): bool
    {
        return $this->current() instanceof ManyFoos;
    }
    
    public function getChildren(): ?self
    {
        $current = $this->current();
        return $current instanceof ManyFoos ? new self($current->children) : null;
    }
}
Psalm output (using commit 147505c):

ERROR: NullableReturnStatement - 23:16 - The declared return type 'RecursiveFoos' for RecursiveFoos::getChildren is not nullable, but the function returns 'RecursiveFoos|null'

ERROR: InvalidNullableReturnType - 20:36 - The declared return type 'RecursiveFoos' for RecursiveFoos::getChildren is not nullable, but 'RecursiveFoos|null' contains null

@weirdan
Copy link
Collaborator

weirdan commented Jan 28, 2024

Appears to be fixed in #10340

@weirdan weirdan closed this as completed Jan 28, 2024
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

2 participants