From d77b3d915360b99326990df50a3fe9b4a2ff2fb2 Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Fri, 1 Jul 2022 12:31:29 +1000 Subject: [PATCH] [REF][PHP8.1] Add in type hints to fix deprecations and add in #[\ReturnTypeWillChange] where type hint would be transversable or mixed for php7 compatability --- CRM/Utils/LazyArray.php | 10 ++++++---- Civi/Api4/Generic/AbstractAction.php | 7 ++++--- Civi/Api4/Generic/Result.php | 3 ++- Civi/Api4/Service/Spec/RequestSpec.php | 9 ++++++--- Civi/Token/TokenProcessor.php | 1 + Civi/Token/TokenRow.php | 10 ++++++---- ext/eventcart/CRM/Event/Cart/BAO/EventInCart.php | 7 ++++--- .../phpunit/CRM/Contact/BAO/QueryTestDataProvider.php | 7 +++++-- .../Form/Search/Custom/GroupTestDataProvider.php | 7 +++++-- .../phpunit/CRM/Mailing/BAO/QueryTestDataProvider.php | 7 +++++-- 10 files changed, 44 insertions(+), 24 deletions(-) diff --git a/CRM/Utils/LazyArray.php b/CRM/Utils/LazyArray.php index 4ea6ab1659..72f8089711 100644 --- a/CRM/Utils/LazyArray.php +++ b/CRM/Utils/LazyArray.php @@ -56,15 +56,16 @@ class CRM_Utils_LazyArray implements ArrayAccess, IteratorAggregate, Countable { return $this; } - public function offsetExists($offset) { + public function offsetExists($offset): bool { return isset($this->load()->cache[$offset]); } + #[\ReturnTypeWillChange] public function &offsetGet($offset) { return $this->load()->cache[$offset]; } - public function offsetSet($offset, $value) { + public function offsetSet($offset, $value): void { if ($offset === NULL) { $this->load()->cache[] = $value; } @@ -73,10 +74,11 @@ class CRM_Utils_LazyArray implements ArrayAccess, IteratorAggregate, Countable { } } - public function offsetUnset($offset) { + public function offsetUnset($offset): void { unset($this->load()->cache[$offset]); } + #[\ReturnTypeWillChange] public function getIterator() { return new ArrayIterator($this->load()->cache); } @@ -88,7 +90,7 @@ class CRM_Utils_LazyArray implements ArrayAccess, IteratorAggregate, Countable { return $this->load()->cache; } - public function count() { + public function count(): int { return count($this->load()->cache); } diff --git a/Civi/Api4/Generic/AbstractAction.php b/Civi/Api4/Generic/AbstractAction.php index 0abc53a3fd..0aa5e8be4b 100644 --- a/Civi/Api4/Generic/AbstractAction.php +++ b/Civi/Api4/Generic/AbstractAction.php @@ -327,13 +327,14 @@ abstract class AbstractAction implements \ArrayAccess { /** * @inheritDoc */ - public function offsetExists($offset) { + public function offsetExists($offset): bool { return in_array($offset, ['entity', 'action', 'params', 'version', 'check_permissions', 'id']) || isset($this->_arrayStorage[$offset]); } /** * @inheritDoc */ + #[\ReturnTypeWillChange] public function &offsetGet($offset) { $val = NULL; if (in_array($offset, ['entity', 'action'])) { @@ -359,7 +360,7 @@ abstract class AbstractAction implements \ArrayAccess { /** * @inheritDoc */ - public function offsetSet($offset, $value) { + public function offsetSet($offset, $value): void { if (in_array($offset, ['entity', 'action', 'entityName', 'actionName', 'params', 'version', 'id'])) { throw new \API_Exception('Cannot modify api4 state via array access'); } @@ -374,7 +375,7 @@ abstract class AbstractAction implements \ArrayAccess { /** * @inheritDoc */ - public function offsetUnset($offset) { + public function offsetUnset($offset): void { if (in_array($offset, ['entity', 'action', 'entityName', 'actionName', 'params', 'check_permissions', 'version', 'id'])) { throw new \API_Exception('Cannot modify api4 state via array access'); } diff --git a/Civi/Api4/Generic/Result.php b/Civi/Api4/Generic/Result.php index 3689bee311..c1896890bf 100644 --- a/Civi/Api4/Generic/Result.php +++ b/Civi/Api4/Generic/Result.php @@ -157,7 +157,7 @@ class Result extends \ArrayObject implements \JsonSerializable { * * @return int */ - public function count() { + public function count(): int { return $this->rowCount ?? parent::count(); } @@ -211,6 +211,7 @@ class Result extends \ArrayObject implements \JsonSerializable { /** * @return array */ + #[\ReturnTypeWillChange] public function jsonSerialize() { return $this->getArrayCopy(); } diff --git a/Civi/Api4/Service/Spec/RequestSpec.php b/Civi/Api4/Service/Spec/RequestSpec.php index 7899ebe46e..c18381c1eb 100644 --- a/Civi/Api4/Service/Spec/RequestSpec.php +++ b/Civi/Api4/Service/Spec/RequestSpec.php @@ -176,23 +176,26 @@ class RequestSpec implements \Iterator { return $this->action; } + #[\ReturnTypeWillChange] public function rewind() { return reset($this->fields); } + #[\ReturnTypeWillChange] public function current() { return current($this->fields); } + #[\ReturnTypeWillChange] public function key() { return key($this->fields); } - public function next() { - return next($this->fields); + public function next(): void { + next($this->fields); } - public function valid() { + public function valid(): bool { return key($this->fields) !== NULL; } diff --git a/Civi/Token/TokenProcessor.php b/Civi/Token/TokenProcessor.php index 4929a9c14f..87290e4aec 100644 --- a/Civi/Token/TokenProcessor.php +++ b/Civi/Token/TokenProcessor.php @@ -528,6 +528,7 @@ class TokenRowIterator extends \IteratorIterator { $this->tokenProcessor = $tokenProcessor; } + #[\ReturnTypeWillChange] public function current() { return new TokenRow($this->tokenProcessor, parent::key()); } diff --git a/Civi/Token/TokenRow.php b/Civi/Token/TokenRow.php index 6701008391..0ef5b067e3 100644 --- a/Civi/Token/TokenRow.php +++ b/Civi/Token/TokenRow.php @@ -355,7 +355,7 @@ class TokenRowContext implements \ArrayAccess, \IteratorAggregate, \Countable { * * @return bool */ - public function offsetExists($offset) { + public function offsetExists($offset): bool { return isset($this->tokenProcessor->rowContexts[$this->tokenRow][$offset]) || isset($this->tokenProcessor->context[$offset]); } @@ -367,6 +367,7 @@ class TokenRowContext implements \ArrayAccess, \IteratorAggregate, \Countable { * * @return string */ + #[\ReturnTypeWillChange] public function &offsetGet($offset) { if (isset($this->tokenProcessor->rowContexts[$this->tokenRow][$offset])) { return $this->tokenProcessor->rowContexts[$this->tokenRow][$offset]; @@ -384,7 +385,7 @@ class TokenRowContext implements \ArrayAccess, \IteratorAggregate, \Countable { * @param string $offset * @param mixed $value */ - public function offsetSet($offset, $value) { + public function offsetSet($offset, $value): void { $this->tokenProcessor->rowContexts[$this->tokenRow][$offset] = $value; } @@ -393,7 +394,7 @@ class TokenRowContext implements \ArrayAccess, \IteratorAggregate, \Countable { * * @param mixed $offset */ - public function offsetUnset($offset) { + public function offsetUnset($offset): void { unset($this->tokenProcessor->rowContexts[$this->tokenRow][$offset]); } @@ -402,6 +403,7 @@ class TokenRowContext implements \ArrayAccess, \IteratorAggregate, \Countable { * * @return \ArrayIterator */ + #[\ReturnTypeWillChange] public function getIterator() { return new \ArrayIterator($this->createMergedArray()); } @@ -411,7 +413,7 @@ class TokenRowContext implements \ArrayAccess, \IteratorAggregate, \Countable { * * @return int */ - public function count() { + public function count(): int { return count($this->createMergedArray()); } diff --git a/ext/eventcart/CRM/Event/Cart/BAO/EventInCart.php b/ext/eventcart/CRM/Event/Cart/BAO/EventInCart.php index 97cc92d256..b06844e7c5 100644 --- a/ext/eventcart/CRM/Event/Cart/BAO/EventInCart.php +++ b/ext/eventcart/CRM/Event/Cart/BAO/EventInCart.php @@ -222,7 +222,7 @@ class CRM_Event_Cart_BAO_EventInCart extends CRM_Event_Cart_DAO_EventInCart impl * * @return bool */ - public function offsetExists($offset) { + public function offsetExists($offset): bool { return array_key_exists(array_merge($this->fields(), ['main_conference_event_id']), $offset); } @@ -231,6 +231,7 @@ class CRM_Event_Cart_BAO_EventInCart extends CRM_Event_Cart_DAO_EventInCart impl * * @return int */ + #[\ReturnTypeWillChange] public function offsetGet($offset) { if ($offset == 'event') { return $this->event->toArray(); @@ -249,13 +250,13 @@ class CRM_Event_Cart_BAO_EventInCart extends CRM_Event_Cart_DAO_EventInCart impl * @param mixed $offset * @param mixed $value */ - public function offsetSet($offset, $value) { + public function offsetSet($offset, $value): void { } /** * @param mixed $offset */ - public function offsetUnset($offset) { + public function offsetUnset($offset): void { } /** diff --git a/tests/phpunit/CRM/Contact/BAO/QueryTestDataProvider.php b/tests/phpunit/CRM/Contact/BAO/QueryTestDataProvider.php index 6c40d94231..f8cfd1ae95 100644 --- a/tests/phpunit/CRM/Contact/BAO/QueryTestDataProvider.php +++ b/tests/phpunit/CRM/Contact/BAO/QueryTestDataProvider.php @@ -210,6 +210,7 @@ class CRM_Contact_BAO_QueryTestDataProvider implements Iterator { $this->i = 0; } + #[\ReturnTypeWillChange] public function rewind() { $this->i = 0; } @@ -217,6 +218,7 @@ class CRM_Contact_BAO_QueryTestDataProvider implements Iterator { /** * @return array */ + #[\ReturnTypeWillChange] public function current() { $count = count($this->dataset[$this->i]['id']); $ids = $this->dataset[$this->i]['id']; @@ -234,18 +236,19 @@ class CRM_Contact_BAO_QueryTestDataProvider implements Iterator { /** * @return int */ + #[\ReturnTypeWillChange] public function key() { return $this->i; } - public function next() { + public function next(): void { $this->i++; } /** * @return bool */ - public function valid() { + public function valid(): bool { return isset($this->dataset[$this->i]); } diff --git a/tests/phpunit/CRM/Contact/Form/Search/Custom/GroupTestDataProvider.php b/tests/phpunit/CRM/Contact/Form/Search/Custom/GroupTestDataProvider.php index 7df961d860..46c321f39e 100644 --- a/tests/phpunit/CRM/Contact/Form/Search/Custom/GroupTestDataProvider.php +++ b/tests/phpunit/CRM/Contact/Form/Search/Custom/GroupTestDataProvider.php @@ -323,6 +323,7 @@ class CRM_Contact_Form_Search_Custom_GroupTestDataProvider implements Iterator { ], ]; + #[\ReturnTypeWillChange] public function rewind() { $this->i = 0; } @@ -330,6 +331,7 @@ class CRM_Contact_Form_Search_Custom_GroupTestDataProvider implements Iterator { /** * @return array */ + #[\ReturnTypeWillChange] public function current() { $count = count($this->dataset[$this->i]['id']); $ids = $this->dataset[$this->i]['id']; @@ -357,18 +359,19 @@ class CRM_Contact_Form_Search_Custom_GroupTestDataProvider implements Iterator { /** * @return int */ + #[\ReturnTypeWillChange] public function key() { return $this->i; } - public function next() { + public function next(): void { $this->i++; } /** * @return bool */ - public function valid() { + public function valid(): bool { return isset($this->dataset[$this->i]); } diff --git a/tests/phpunit/CRM/Mailing/BAO/QueryTestDataProvider.php b/tests/phpunit/CRM/Mailing/BAO/QueryTestDataProvider.php index 2f795a2a2b..a736c2aaf8 100644 --- a/tests/phpunit/CRM/Mailing/BAO/QueryTestDataProvider.php +++ b/tests/phpunit/CRM/Mailing/BAO/QueryTestDataProvider.php @@ -113,6 +113,7 @@ class CRM_Mailing_BAO_QueryTestDataProvider implements Iterator { $this->i = 0; } + #[\ReturnTypeWillChange] public function rewind() { $this->i = 0; } @@ -120,6 +121,7 @@ class CRM_Mailing_BAO_QueryTestDataProvider implements Iterator { /** * @return array */ + #[\ReturnTypeWillChange] public function current() { $count = count($this->dataset[$this->i]['id']); $ids = $this->dataset[$this->i]['id']; @@ -137,18 +139,19 @@ class CRM_Mailing_BAO_QueryTestDataProvider implements Iterator { /** * @return int */ + #[\ReturnTypeWillChange] public function key() { return $this->i; } - public function next() { + public function next(): void { $this->i++; } /** * @return bool */ - public function valid() { + public function valid(): bool { return isset($this->dataset[$this->i]); } -- 2.25.1