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

[DowngradePhp70][Transform] Add #[\ReturnTypeWillChange] on Downgrade + transform ArrayObject::getIterator() to keep working on php 8.1 #1711

Merged
merged 4 commits into from
Jan 21, 2022

Conversation

samsonasik
Copy link
Member

@samsonasik samsonasik commented Jan 21, 2022

Given the following code, on DowngradeScalarTypeDeclarationRector:

class ArrayStack extends \ArrayObject
{
    public function getIterator(): \Iterator
    {
        $array = $this->getArrayCopy();
        return new ArrayIterator(array_reverse($array));
    }
}

It only remove \Iterator and add @return \Iterator, and if the code is using in php 8.1, it will got notice:

Deprecated: Return type of ArrayStack::getIterator() should either be compatible with ArrayObject::getIterator(): Iterator, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/uZl8U on line 8

ref https://3v4l.org/uZl8U#v8.1.2

This PR fix it by register it to PhpDocFromTypeDeclarationDecorator::ADD_RETURN_TYPE_WILL_CHANGE constant value.

ref laminas/laminas-stdlib#54 for inspiration.

@samsonasik samsonasik changed the title [DowngradePhp70] Add #[\ReturnTypeWillChange] on Downgrade ArrayObject::getIterator() to keep working on php 8.1 [DowngradePhp70][Transform] Add #[\ReturnTypeWillChange] on Downgrade ArrayObject::getIterator() to keep working on php 8.1 Jan 21, 2022
@samsonasik
Copy link
Member Author

Tested in 2 rules:

Rector\DowngradePhp70\Rector\FunctionLike\DowngradeScalarTypeDeclarationRector;
Rector\Transform\Rector\ClassMethod\ReturnTypeWillChangeRector;

@samsonasik
Copy link
Member Author

All checks have passed 🎉 @TomasVotruba it is ready for review.

@samsonasik samsonasik changed the title [DowngradePhp70][Transform] Add #[\ReturnTypeWillChange] on Downgrade ArrayObject::getIterator() to keep working on php 8.1 [DowngradePhp70][Transform] Add #[\ReturnTypeWillChange] on Downgrade + transform ArrayObject::getIterator() to keep working on php 8.1 Jan 21, 2022
@TomasVotruba TomasVotruba merged commit cb96224 into main Jan 21, 2022
@TomasVotruba TomasVotruba deleted the return-type-iterator branch January 21, 2022 23:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants