Skip to content

Commit

Permalink
PHP 8.1: silence the deprecation notice about RecursiveFilterIterator…
Browse files Browse the repository at this point in the history
… method return types

As of PHP 8.1, PHP adds return type declarations to the PHP native functions.

For the `RecursiveFilterIterator`, the relevant method signatures are:
* `accept(): bool`
* `hasChildren(): bool`
* `getChildren(): ?RecursiveFilterIterator`

As this libary still supports PHP 5.3, it is not possible to add this return type as:
1. Return types weren't available until PHP 7.0 and
2. the `mixed` return type only became available in PHP 8.0.

For libraries still supporting PHP < 7.0, there are two choices:
1. Either decouple from the interface.
2. Or use a PHP 8.1 attribute to silence the deprecation notice.

As prior to PHP 8.0, attributes are ignored as if they were comments, it is safe to add the attribute to the library and IMO, this is prefered over decoupling the classes from the interface.

To prevent PHPCS tripping up over "something" existing between the function docblock and the declaration, PHPCS 3.6.0 should be used, which is the first PHPCS version with full PHP 8.0 syntax support in the sniffs (albeit that there are still some small things to fix up in PHPCS).

Refs:
* https://wiki.php.net/rfc/internal_method_return_types
  • Loading branch information
jrfnl authored and grogy committed Aug 13, 2021
1 parent 49cc975 commit 7b09d72
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use JakubOnderka\PhpParallelLint\Contracts\SyntaxErrorCallback;
use JakubOnderka\PhpParallelLint\Process\GitBlameProcess;
use JakubOnderka\PhpParallelLint\Process\PhpExecutable;
use ReturnTypeWillChange;

class Manager
{
Expand Down Expand Up @@ -226,6 +227,7 @@ public function __construct(\RecursiveDirectoryIterator $iterator, array $exclud
* @link http://php.net/manual/en/filteriterator.accept.php
* @return bool true if the current element is acceptable, otherwise false.
*/
#[ReturnTypeWillChange]
public function accept()
{
$current = $this->current()->getPathname();
Expand All @@ -245,6 +247,7 @@ public function accept()
* @link http://php.net/manual/en/recursivefilteriterator.haschildren.php
* @return bool true if the inner iterator has children, otherwise false
*/
#[ReturnTypeWillChange]
public function hasChildren()
{
return $this->iterator->hasChildren();
Expand All @@ -257,6 +260,7 @@ public function hasChildren()
* @link http://php.net/manual/en/recursivefilteriterator.getchildren.php
* @return \RecursiveFilterIterator containing the inner iterator's children.
*/
#[ReturnTypeWillChange]
public function getChildren()
{
return new self($this->iterator->getChildren(), $this->excluded);
Expand Down

0 comments on commit 7b09d72

Please sign in to comment.