Skip to content

Commit

Permalink
PHP 8.1: fix deprecation notices
Browse files Browse the repository at this point in the history
A run on PHP 8.1 currently shows:
```
PHP Deprecated:  Return type of iter\rewindable\_RewindableGenerator::current() should either be compatible with Iterator::current(): mixed, or the #[ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/runner/work/iter/iter/src/iter.rewindable.php on line 123
PHP Deprecated:  Return type of iter\rewindable\_RewindableGenerator::next() should either be compatible with Iterator::next(): void, or the #[ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/runner/work/iter/iter/src/iter.rewindable.php on line 108
PHP Deprecated:  Return type of iter\rewindable\_RewindableGenerator::key() should either be compatible with Iterator::key(): mixed, or the #[ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/runner/work/iter/iter/src/iter.rewindable.php on line 118
PHP Deprecated:  Return type of iter\rewindable\_RewindableGenerator::valid() should either be compatible with Iterator::valid(): bool, or the #[ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/runner/work/iter/iter/src/iter.rewindable.php on line 113
PHP Deprecated:  Return type of iter\rewindable\_RewindableGenerator::rewind() should either be compatible with Iterator::rewind(): void, or the #[ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/runner/work/iter/iter/src/iter.rewindable.php on line 103
PHP Deprecated:  Return type of iter\_CountableTestDummy::count() should either be compatible with Countable::count(): int, or the #[ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/runner/work/iter/iter/test/iterTest.php on line 625
```

These deprecation notices relate to the [Return types for internal methods RFC](https://wiki.php.net/rfc/internal_method_return_types) in PHP 8.1.

Using the attribute will silence the deprecation notices for now for those methods for which it cannot be added yet (PHP 8.0 `mixed`).
For all other methods, the return type has now been added.
  • Loading branch information
jrfnl authored and nikic committed Aug 2, 2021
1 parent c042eeb commit d132392
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
11 changes: 8 additions & 3 deletions src/iter.rewindable.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ function callRewindable(callable $function, ...$args) {
}

namespace iter\rewindable {

use ReturnTypeWillChange;

/**
* These functions are just rewindable wrappers around the normal
* non-rewindable functions from the iter namespace
Expand Down Expand Up @@ -100,26 +103,28 @@ public function __construct(callable $function, array $args) {
$this->generator = null;
}

public function rewind() {
public function rewind(): void {
$function = $this->function;
$this->generator = $function(...$this->args);
}

public function next() {
public function next(): void {
if (!$this->generator) { $this->rewind(); }
$this->generator->next();
}

public function valid() {
public function valid(): bool {
if (!$this->generator) { $this->rewind(); }
return $this->generator->valid();
}

#[ReturnTypeWillChange]
public function key() {
if (!$this->generator) { $this->rewind(); }
return $this->generator->key();
}

#[ReturnTypeWillChange]
public function current() {
if (!$this->generator) { $this->rewind(); }
return $this->generator->current();
Expand Down
2 changes: 1 addition & 1 deletion test/iterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ function() {
}

class _CountableTestDummy implements \Countable {
public function count() {
public function count(): int {
return 42;
}
}

0 comments on commit d132392

Please sign in to comment.