Skip to content

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

Merged
kocsismate merged 4 commits into
php:masterfrom
arshidkv12:spl-6
Aug 15, 2026
Merged

ext/spl: Narrow tentative return types for DirectoryIterator methods#22642
kocsismate merged 4 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) ?

Comment thread ext/spl/spl_directory.stub.php Outdated
ext/spl: Narrow tentative return types for DirectoryIterator methods

Add tentative return type to Directory::current()

Add tentative return type to Directory::current()

Add tentative return type to Directory::current()

@DanielEScherzer DanielEScherzer left a comment

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.

okay for ext/reflection, didn't check the other parts

@kocsismate

Copy link
Copy Markdown
Member

@arshidkv12 Can you please write a short upgrading note? I'll merge your changes afterwards :)

@kocsismate
kocsismate merged commit 1f0e6ef into php:master Aug 15, 2026
17 of 18 checks passed
@kocsismate

Copy link
Copy Markdown
Member

thank you, very nice attempt to cleanup the todos! :)

@arshidkv12

Copy link
Copy Markdown
Contributor Author

thank you, very nice attempt to cleanup the todos! :)

You are welcome

@arshidkv12
arshidkv12 deleted the spl-6 branch August 15, 2026 17:25
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.

5 participants