From c26b1c87d7ad6a36c4f1f0763b107f3a8b3257eb Mon Sep 17 00:00:00 2001 From: Brett McBride Date: Wed, 5 Jan 2022 16:06:40 +1100 Subject: [PATCH] fixing php 8.1 deprecation warnings php 8.1 is more strict, and raises some deprecation notices with existing protobuf code. Not all of the deprecations can be fixed without dropping support for php7.x (eg, the 'mixed' type doesn't appear until 8.1, and union types until 8.0, but as an interim solution the 'ReturnTypeWillChange' attribute can be used to suppress the notices. In passing, also be explicit about a cast from float to int in 'zigZagEncode64' which 8.1 also complains about when running tests. --- php/src/Google/Protobuf/Internal/GPBWire.php | 2 +- php/src/Google/Protobuf/Internal/MapField.php | 13 ++++++++----- php/src/Google/Protobuf/Internal/MapFieldIter.php | 12 +++++++----- php/src/Google/Protobuf/Internal/RepeatedField.php | 12 +++++++----- .../Google/Protobuf/Internal/RepeatedFieldIter.php | 8 +++++--- 5 files changed, 28 insertions(+), 19 deletions(-) diff --git a/php/src/Google/Protobuf/Internal/GPBWire.php b/php/src/Google/Protobuf/Internal/GPBWire.php index 29569530fd..034f5df92e 100644 --- a/php/src/Google/Protobuf/Internal/GPBWire.php +++ b/php/src/Google/Protobuf/Internal/GPBWire.php @@ -146,7 +146,7 @@ class GPBWire return bcsub(bcmul(bcsub(0, $int64), 2), 1); } } else { - return ($int64 << 1) ^ ($int64 >> 63); + return ((int)$int64 << 1) ^ ((int)$int64 >> 63); } } diff --git a/php/src/Google/Protobuf/Internal/MapField.php b/php/src/Google/Protobuf/Internal/MapField.php index 719fb350bd..20ac6c2d66 100644 --- a/php/src/Google/Protobuf/Internal/MapField.php +++ b/php/src/Google/Protobuf/Internal/MapField.php @@ -37,6 +37,8 @@ namespace Google\Protobuf\Internal; +use Traversable; + /** * MapField is used by generated protocol message classes to manipulate map * fields. It can be used like native PHP array. @@ -134,6 +136,7 @@ class MapField implements \ArrayAccess, \IteratorAggregate, \Countable * @throws \ErrorException Invalid type for index. * @throws \ErrorException Non-existing index. */ + #[\ReturnTypeWillChange] public function offsetGet($key) { return $this->container[$key]; @@ -151,7 +154,7 @@ class MapField implements \ArrayAccess, \IteratorAggregate, \Countable * @throws \ErrorException Invalid type for value. * @throws \ErrorException Non-existing key. */ - public function offsetSet($key, $value) + public function offsetSet($key, $value): void { $this->checkKey($this->key_type, $key); @@ -209,7 +212,7 @@ class MapField implements \ArrayAccess, \IteratorAggregate, \Countable * @return void * @throws \ErrorException Invalid type for key. */ - public function offsetUnset($key) + public function offsetUnset($key): void { $this->checkKey($this->key_type, $key); unset($this->container[$key]); @@ -224,7 +227,7 @@ class MapField implements \ArrayAccess, \IteratorAggregate, \Countable * @return bool True if the element at the given key exists. * @throws \ErrorException Invalid type for key. */ - public function offsetExists($key) + public function offsetExists($key): bool { $this->checkKey($this->key_type, $key); return isset($this->container[$key]); @@ -233,7 +236,7 @@ class MapField implements \ArrayAccess, \IteratorAggregate, \Countable /** * @ignore */ - public function getIterator() + public function getIterator(): Traversable { return new MapFieldIter($this->container, $this->key_type); } @@ -245,7 +248,7 @@ class MapField implements \ArrayAccess, \IteratorAggregate, \Countable * * @return integer The number of stored elements. */ - public function count() + public function count(): int { return count($this->container); } diff --git a/php/src/Google/Protobuf/Internal/MapFieldIter.php b/php/src/Google/Protobuf/Internal/MapFieldIter.php index 4e18005ea8..8370787876 100644 --- a/php/src/Google/Protobuf/Internal/MapFieldIter.php +++ b/php/src/Google/Protobuf/Internal/MapFieldIter.php @@ -68,9 +68,9 @@ class MapFieldIter implements \Iterator * * @return void */ - public function rewind() + public function rewind(): void { - return reset($this->container); + reset($this->container); } /** @@ -78,6 +78,7 @@ class MapFieldIter implements \Iterator * * @return object The element at the current position. */ + #[\ReturnTypeWillChange] public function current() { return current($this->container); @@ -88,6 +89,7 @@ class MapFieldIter implements \Iterator * * @return object The current key. */ + #[\ReturnTypeWillChange] public function key() { $key = key($this->container); @@ -117,9 +119,9 @@ class MapFieldIter implements \Iterator * * @return void */ - public function next() + public function next(): void { - return next($this->container); + next($this->container); } /** @@ -127,7 +129,7 @@ class MapFieldIter implements \Iterator * * @return bool True if there are more elements to iterate. */ - public function valid() + public function valid(): bool { return key($this->container) !== null; } diff --git a/php/src/Google/Protobuf/Internal/RepeatedField.php b/php/src/Google/Protobuf/Internal/RepeatedField.php index c0331ff38e..e589b644fe 100644 --- a/php/src/Google/Protobuf/Internal/RepeatedField.php +++ b/php/src/Google/Protobuf/Internal/RepeatedField.php @@ -39,6 +39,7 @@ namespace Google\Protobuf\Internal; use Google\Protobuf\Internal\GPBType; use Google\Protobuf\Internal\GPBUtil; +use Traversable; /** * RepeatedField is used by generated protocol message classes to manipulate @@ -121,6 +122,7 @@ class RepeatedField implements \ArrayAccess, \IteratorAggregate, \Countable * @throws \ErrorException Invalid type for index. * @throws \ErrorException Non-existing index. */ + #[\ReturnTypeWillChange] public function offsetGet($offset) { return $this->container[$offset]; @@ -138,7 +140,7 @@ class RepeatedField implements \ArrayAccess, \IteratorAggregate, \Countable * @throws \ErrorException Non-existing index. * @throws \ErrorException Incorrect type of the element. */ - public function offsetSet($offset, $value) + public function offsetSet($offset, $value): void { switch ($this->type) { case GPBType::SFIXED32: @@ -209,7 +211,7 @@ class RepeatedField implements \ArrayAccess, \IteratorAggregate, \Countable * @throws \ErrorException The element to be removed is not at the end of the * RepeatedField. */ - public function offsetUnset($offset) + public function offsetUnset($offset): void { $count = count($this->container); if (!is_numeric($offset) || $count === 0 || $offset !== $count - 1) { @@ -230,7 +232,7 @@ class RepeatedField implements \ArrayAccess, \IteratorAggregate, \Countable * @return bool True if the element at the given offset exists. * @throws \ErrorException Invalid type for index. */ - public function offsetExists($offset) + public function offsetExists($offset): bool { return isset($this->container[$offset]); } @@ -238,7 +240,7 @@ class RepeatedField implements \ArrayAccess, \IteratorAggregate, \Countable /** * @ignore */ - public function getIterator() + public function getIterator(): Traversable { return new RepeatedFieldIter($this->container); } @@ -250,7 +252,7 @@ class RepeatedField implements \ArrayAccess, \IteratorAggregate, \Countable * * @return integer The number of stored elements. */ - public function count() + public function count(): int { return count($this->container); } diff --git a/php/src/Google/Protobuf/Internal/RepeatedFieldIter.php b/php/src/Google/Protobuf/Internal/RepeatedFieldIter.php index 2b6f82309b..ad86f95f94 100644 --- a/php/src/Google/Protobuf/Internal/RepeatedFieldIter.php +++ b/php/src/Google/Protobuf/Internal/RepeatedFieldIter.php @@ -71,7 +71,7 @@ class RepeatedFieldIter implements \Iterator * * @return void */ - public function rewind() + public function rewind(): void { $this->position = 0; } @@ -81,6 +81,7 @@ class RepeatedFieldIter implements \Iterator * * @return object The element at the current position. */ + #[\ReturnTypeWillChange] public function current() { return $this->container[$this->position]; @@ -91,6 +92,7 @@ class RepeatedFieldIter implements \Iterator * * @return integer The current position. */ + #[\ReturnTypeWillChange] public function key() { return $this->position; @@ -101,7 +103,7 @@ class RepeatedFieldIter implements \Iterator * * @return void */ - public function next() + public function next(): void { ++$this->position; } @@ -111,7 +113,7 @@ class RepeatedFieldIter implements \Iterator * * @return bool True if there are more elements to iterate. */ - public function valid() + public function valid(): bool { return isset($this->container[$this->position]); }