Skip to content

Commit

Permalink
Fix deprecation: Return types in ClientObjectCollection
Browse files Browse the repository at this point in the history
PHP 8.1 logs deprecation warnings:
> Return type of Office365\Runtime\ClientObjectCollection::getIterator()
> should either be compatible with IteratorAggregate::getIterator(): Traversable,
> or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress
> the notice in vendor/vgrem/php-spo/src/Runtime/ClientObjectCollection.php on line 287

and

> Return type of Office365\Runtime\ClientObjectCollection::offsetExists($offset)
> should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool

and

> Return type of Office365\Runtime\ClientObjectCollection::offsetGet($offset)
> should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed,
> or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice

and

> Return type of Office365\Runtime\ClientObjectCollection::offsetSet($offset, $value)
> should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void

and

> Return type of Office365\Runtime\ClientObjectCollection::offsetUnset($offset)
> should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void

Those can be fixed by adding return types for getIterator(), offsetExists(),
offsetSet() and offsetUnset().
For those, the minimum required PHP version needs to be raised to PHP 7.1,
since the "void" return type only exists in there.

The return type for offsetGet() is "mixed", which cannot be expressed in PHP 7.x,
only PHP 8.0 and later.
I decided to use the "#[\ReturnTypeWillChange]" annotation so the lib
can stay compatible with PHP 7.

Related: #293
  • Loading branch information
cweiske committed Oct 5, 2022
1 parent 1a16d48 commit a1d7ae3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
],
"license": "MIT",
"require": {
"php": ">=5.5",
"php": ">=7.1",
"ext-curl": "*",
"ext-json": "*",
"ext-simplexml": "*",
Expand Down
11 changes: 6 additions & 5 deletions src/Runtime/ClientObjectCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,10 @@ function setProperty($index, $value, $persistChanges = true)


/**
* @return Generator|Traversable
* @return Traversable
* @throws Exception
*/
public function getIterator()
public function getIterator(): Traversable
{
/** @var ClientObject $item */
foreach ($this->data as $index => $item) {
Expand Down Expand Up @@ -321,11 +321,12 @@ private function getNextItems(){
* @return boolean
* @abstracting ArrayAccess
*/
public function offsetExists($offset)
public function offsetExists($offset): bool
{
return isset($this->data[$offset]);
}

#[\ReturnTypeWillChange]
/**
* Returns the value at specified offset
*
Expand All @@ -348,7 +349,7 @@ public function offsetGet($offset)
* @access public
* @abstracting ArrayAccess
*/
public function offsetSet($offset, $value)
public function offsetSet($offset, $value): void
{
if (is_null($offset)) {
$this->data[] = $value;
Expand All @@ -364,7 +365,7 @@ public function offsetSet($offset, $value)
* @access public
* @abstracting ArrayAccess
*/
public function offsetUnset($offset)
public function offsetUnset($offset): void
{
if ($this->offsetExists($offset)) {
unset($this->data[$offset]);
Expand Down

0 comments on commit a1d7ae3

Please sign in to comment.