akeneo / api-php-client

PHP client of Akeneo PIM API

Home Page:https://packagist.org/packages/akeneo/api-php-client

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add [\ReturnTypeWillChange] attribute to current and key methods in ResourceCursor class

JoshuaAkeneo opened this issue · comments

If building something with the PHP API Client with the latest version of PHP, you will likely get the following errors:

Deprecated: Return type of Akeneo\Pim\ApiClient\Pagination\ResourceCursor::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /Users/joshuaguinn/Akeneo/Custom_Development_and_POCs/VPL - Classification by UNSPSC/vendor/akeneo/api-php-client/src/Pagination/ResourceCursor.php on line 56
joshuaguinn@Joshuas-Macbook-Pro VPL - Classification by UNSPSC % php application.php
PHP Deprecated:  Return type of Akeneo\Pim\ApiClient\Pagination\ResourceCursor::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /Users/joshuaguinn/Akeneo/Custom_Development_and_POCs/VPL - Classification by UNSPSC/vendor/akeneo/api-php-client/src/Pagination/ResourceCursor.php on line 32

Deprecated: Return type of Akeneo\Pim\ApiClient\Pagination\ResourceCursor::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /Users/joshuaguinn/Akeneo/Custom_Development_and_POCs/VPL - Classification by UNSPSC/vendor/akeneo/api-php-client/src/Pagination/ResourceCursor.php on line 32
PHP Deprecated:  Return type of Akeneo\Pim\ApiClient\Pagination\ResourceCursor::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /Users/joshuaguinn/Akeneo/Custom_Development_and_POCs/VPL - Classification by UNSPSC/vendor/akeneo/api-php-client/src/Pagination/ResourceCursor.php on line 56

While it doesn’t break anything, it is not fun to look at. The fix is as easy as going to the offending methods and add a #[\ReturnTypeWillChange] PHP 8 attribute at the top of the method like so:

    #[\ReturnTypeWillChange]
    public function current()
    {
        return $this->currentPage->getItems()[$this->currentIndex];
    }
    #[\ReturnTypeWillChange]
    public function key()
    {
        return $this->totalIndex;
    }

Doing this on the key and current methods in the ResourceCursor class cleared everything up for me, but it is likely the same for some of the other iterators.

Version information:

  • akeneo/api-php-client: latest stable version (pulled yesterday)
  • PHP: version 8.1

Screen Shot 2022-04-14 at 4 34 05 PM

commented

Hello @JoshuaAkeneo ,

Issue confirmed while testing with PHP 8.1 and the version v8.0.0 of the client:

Fatal error: During inheritance of Iterator: Uncaught Exception: Deprecated Functionality: Return type of Akeneo\Pim\ApiClient\Pagination\ResourceCursor::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/project/magento/vendor/akeneo/api-php-client/src/Pagination/ResourceCursor.php on line 32

Adding the return type ": mixed" to the "current" and "key" functions might be a solution to solve the issue.

Thanks a lot!
Regards,