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.
pull/9370/head
Brett McBride 3 years ago
parent b17c8ca979
commit c26b1c87d7
  1. 2
      php/src/Google/Protobuf/Internal/GPBWire.php
  2. 13
      php/src/Google/Protobuf/Internal/MapField.php
  3. 12
      php/src/Google/Protobuf/Internal/MapFieldIter.php
  4. 12
      php/src/Google/Protobuf/Internal/RepeatedField.php
  5. 8
      php/src/Google/Protobuf/Internal/RepeatedFieldIter.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);
}
}

@ -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);
}

@ -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;
}

@ -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);
}

@ -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]);
}

Loading…
Cancel
Save