From af51893fef7cb3a7eb49b80a5c922ab5f69e9dbe Mon Sep 17 00:00:00 2001 From: Niellles <10075507+Niellles@users.noreply.github.com> Date: Tue, 7 Feb 2023 15:37:46 +0100 Subject: [PATCH] fix: IterableFieldTrait:current() should be compatible with Iterator::current() As of PHP 8.1 methods return types should be compatible with their parent/interface. I.e. deprecration as of 8.1 enforced as of 9.0. IterableFieldTrait::current() should therefore return mixed, the same as Iterator::current(). However this is not possible to do without dropping support for PHP < 8.0, as mixed was introduced then. We fix this by creating different versions of IterableFieldTrait based on the PHP version; defining current() with(out) a return type based on the (major) version of PHP and/or the availability of mixed. phpstan: flip logic so that phpstan loads correct trait. --- src/Entity/IterableFieldTrait.php | 57 +++--------------------- src/Entity/IterableFieldTraitPhp7.php | 62 +++++++++++++++++++++++++++ src/Entity/IterableFieldTraitPhp8.php | 21 +++++++++ 3 files changed, 89 insertions(+), 51 deletions(-) create mode 100644 src/Entity/IterableFieldTraitPhp7.php create mode 100644 src/Entity/IterableFieldTraitPhp8.php diff --git a/src/Entity/IterableFieldTrait.php b/src/Entity/IterableFieldTrait.php index ae23c5b01..4e07c25f4 100644 --- a/src/Entity/IterableFieldTrait.php +++ b/src/Entity/IterableFieldTrait.php @@ -4,59 +4,14 @@ namespace Bolt\Entity; -trait IterableFieldTrait -{ - private $iteratorCursor = 0; - - /** @var array Field */ - private $fields = []; - - /** - * Makes ListFieldInterface fields |length filter - * return the number of elements in the field - */ - public function count(): int - { - return count($this->getValue()); - } - - /** - * Makes ListFieldInterface fields .length attribute - * return the number of elements in the field - */ - public function length(): int +if (PHP_MAJOR_VERSION >= 8) { + trait IterableFieldTrait { - return $this->count(); + use IterableFieldTraitPhp8; } - - /** - * @return Field|string - */ - public function current() +} else { + trait IterableFieldTrait { - return $this->fields[$this->iteratorCursor]; - } - - public function next(): void - { - ++$this->iteratorCursor; - } - - public function key(): int - { - return $this->iteratorCursor; - } - - public function valid(): bool - { - return isset($this->fields[$this->iteratorCursor]); - } - - public function rewind(): void - { - // Ensure $this->fields is initialised - $this->fields = $this->getValue(); - - $this->iteratorCursor = 0; + use IterableFieldTraitPhp7; } } diff --git a/src/Entity/IterableFieldTraitPhp7.php b/src/Entity/IterableFieldTraitPhp7.php new file mode 100644 index 000000000..6778dfc24 --- /dev/null +++ b/src/Entity/IterableFieldTraitPhp7.php @@ -0,0 +1,62 @@ +getValue()); + } + + /** + * Makes ListFieldInterface fields .length attribute + * return the number of elements in the field + */ + public function length(): int + { + return $this->count(); + } + + /** + * @return Field|string + */ + public function current() + { + return $this->fields[$this->iteratorCursor]; + } + + public function next(): void + { + ++$this->iteratorCursor; + } + + public function key(): int + { + return $this->iteratorCursor; + } + + public function valid(): bool + { + return isset($this->fields[$this->iteratorCursor]); + } + + public function rewind(): void + { + // Ensure $this->fields is initialised + $this->fields = $this->getValue(); + + $this->iteratorCursor = 0; + } +} diff --git a/src/Entity/IterableFieldTraitPhp8.php b/src/Entity/IterableFieldTraitPhp8.php new file mode 100644 index 000000000..99f9ef31f --- /dev/null +++ b/src/Entity/IterableFieldTraitPhp8.php @@ -0,0 +1,21 @@ +parentCurrent(); + } +}