yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.24k stars 6.91k forks source link

Return type of yii\web\SessionIterator::current() should either be compatible with Iterator::current() #19398

Closed dicrtarasov closed 2 years ago

dicrtarasov commented 2 years ago
yii\base\ErrorException: During inheritance of Iterator: Uncaught yii\base\ErrorException: Return type of yii\web\SessionIterator::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/eurotools/public_html/vendor/yiisoft/yii2/web/SessionIterator.php:60

Stack trace:
#0 /home/eurotools/public_html/vendor/yiisoft/yii2/web/SessionIterator.php(16): yii\base\ErrorHandler->handleError()
#1 /home/eurotools/public_html/vendor/yiisoft/yii2/BaseYii.php(293): include('...')
#2 /home/eurotools/public_html/vendor/yiisoft/yii2/web/Session.php(667): yii\BaseYii::autoload()
#3 /home/eurotools/public_html/vendor/yiisoft/yii2/base/Component.php(139): yii\web\Session->getIterator()

Additional info

This is because of missed return types of SessionIterator implements \Iterator:

public function current()

But \Iterator declare:

public function current(): mixed;
Q A
Yii version 2.0.45
PHP version 8.1
Operating system debian
WinterSilence commented 2 years ago

Hm.. I'm test it and can't find any problems https://3v4l.org/7ldOG#v8.1.6

dicrtarasov commented 2 years ago

This is the syntax error, it can not be no problem definitely.

WinterSilence commented 2 years ago

@dicrtarasov no syntax or any other errors are not detected. Extended class can omit parent's types.

dicrtarasov commented 2 years ago

Yes, generally children method can make return type narrow, but not wider. When no return type specified, it assume "mixed", which is widelly, then Iterator.

dicrtarasov commented 2 years ago

You can test it :) Parent -> child: mixed -> Iterator - Ok Iterator -> mixed - Error

Child can return only subclass of parent type compatibility.

dicrtarasov commented 2 years ago

As you can see, Error in this report is real. Try to google it.

dicrtarasov commented 2 years ago

PHP 8.1

WinterSilence commented 2 years ago

@dicrtarasov can you add tests to detect your error?

When no return type specified, it assume "mixed", which is widelly, then Iterator.

Большинство неокончательных внутренних методов теперь требуют переопределяющих методов для объявления совместимого возвращаемого типа, в противном случае во время проверки наследования выдаётся уведомление об устаревании возможности. В случае, если тип возвращаемого значения не может быть объявлен для метода переопределения из-за проблем совместимости версий PHP, можно добавить атрибут #[ReturnTypeWillChange], чтобы заглушить уведомление об устаревании. https://www.php.net/manual/ru/migration81.incompatible.php

dicrtarasov commented 2 years ago

Атрибут #[ReturnTypeWillChange] решит эту проблему.

WinterSilence commented 2 years ago

@dicrtarasov думаю тебе стоит обновится https://github.com/yiisoft/yii2/blob/6599713d2bcf4440190cec2b950b1738d7ddfae4/framework/web/SessionIterator.php#L62

dicrtarasov commented 2 years ago

Yii 2.0.45

WinterSilence commented 2 years ago

@bizley can be close, already fixed

bizley commented 2 years ago

Great.