Skip to content

ext/spl: Narrow tentative return types for DirectoryIterator methods#22642

Open
arshidkv12 wants to merge 3 commits into
php:masterfrom
arshidkv12:spl-6
Open

ext/spl: Narrow tentative return types for DirectoryIterator methods#22642
arshidkv12 wants to merge 3 commits into
php:masterfrom
arshidkv12:spl-6

Conversation

@arshidkv12

Copy link
Copy Markdown
Contributor

Update the DirectoryIterator stubs to narrow the tentative return types
of key() and current(), and regenerate the generated arginfo.

Comment thread ext/spl/spl_directory.c Outdated

CHECK_DIRECTORY_ITERATOR_IS_INITIALIZED(intern);
RETURN_LONG(intern->u.dir.index);
RETURN_STRINGL(intern->u.dir.entry.d_name, strlen(intern->u.dir.entry.d_name));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you change the return data here? It's BC break.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed it to match the TODO in the stub, but you're right—it changes the runtime behavior and is a BC break.

@devnexen devnexen Jul 8, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just few things.

  • It breaks the "agreement" between spl_filesystem_dir_current_key that uses the integer index and this ::key() method. Maybe the stub should be fixed as to return int ?
  • If you want the current entry name, it seems ::getBasename() (::getFilename() eventually ?) is already doing it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

class FilesystemIterator extends DirectoryIterator{
    public function key(): string {}
}

class DirectoryIterator extends SplFileInfo implements SeekableIterator{

    public function key(): int {}
}

How to solve it?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

class FilesystemIterator extends DirectoryIterator{
    public function key(): string {}
}

class DirectoryIterator extends SplFileInfo implements SeekableIterator{

    public function key(): int {}
}

How to solve it?

then int|string might work (current() would be string|SplFileInfo) ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants