From 944722f34fd4980874485c2670ad1ada014715e6 Mon Sep 17 00:00:00 2001 From: Maxime Rainville Date: Tue, 10 Jan 2023 16:47:34 +1300 Subject: [PATCH] API Removed #[\ReturnTypeWillChange] annotations --- src/Control/HTTPRequest.php | 18 +++------- src/Dev/BulkLoader_Result.php | 5 +-- src/ORM/ArrayList.php | 33 ++++-------------- src/ORM/Connect/MySQLQuery.php | 5 +-- src/ORM/Connect/MySQLStatement.php | 4 +-- src/ORM/Connect/Query.php | 4 +-- src/ORM/DataList.php | 42 +++++++---------------- src/ORM/ListDecorator.php | 21 ++++-------- src/ORM/Map.php | 52 ++++++++--------------------- src/ORM/PaginatedList.php | 7 ++-- src/ORM/UnsavedRelationList.php | 6 ++-- src/View/ViewableData.php | 4 +-- tests/php/ORM/ListDecoratorTest.php | 4 +-- 13 files changed, 59 insertions(+), 146 deletions(-) diff --git a/src/Control/HTTPRequest.php b/src/Control/HTTPRequest.php index e37feadf5..7dfb4721f 100644 --- a/src/Control/HTTPRequest.php +++ b/src/Control/HTTPRequest.php @@ -430,36 +430,26 @@ class HTTPRequest implements ArrayAccess /** * Enables the existence of a key-value pair in the request to be checked using * array syntax, so isset($request['title']) will check for $_POST['title'] and $_GET['title'] - * - * @param string $offset - * @return bool */ - #[\ReturnTypeWillChange] - public function offsetExists($offset) + public function offsetExists(mixed $offset): bool { return isset($this->postVars[$offset]) || isset($this->getVars[$offset]); } /** * Access a request variable using array syntax. eg: $request['title'] instead of $request->postVar('title') - * - * @param string $offset - * @return mixed */ - #[\ReturnTypeWillChange] - public function offsetGet($offset) + public function offsetGet(mixed $offset): mixed { return $this->requestVar($offset); } - #[\ReturnTypeWillChange] - public function offsetSet($offset, $value) + public function offsetSet(mixed $offset, mixed $value): void { $this->getVars[$offset] = $value; } - #[\ReturnTypeWillChange] - public function offsetUnset($offset) + public function offsetUnset(mixed $offset): void { unset($this->getVars[$offset]); unset($this->postVars[$offset]); diff --git a/src/Dev/BulkLoader_Result.php b/src/Dev/BulkLoader_Result.php index d14422786..333c89a2a 100644 --- a/src/Dev/BulkLoader_Result.php +++ b/src/Dev/BulkLoader_Result.php @@ -59,11 +59,8 @@ class BulkLoader_Result implements \Countable /** * Returns the count of all objects which were * created or updated. - * - * @return int */ - #[\ReturnTypeWillChange] - public function Count() + public function Count(): int { return count($this->created ?? []) + count($this->updated ?? []); } diff --git a/src/ORM/ArrayList.php b/src/ORM/ArrayList.php index eae6272dc..3d3c98910 100644 --- a/src/ORM/ArrayList.php +++ b/src/ORM/ArrayList.php @@ -8,6 +8,7 @@ use LogicException; use SilverStripe\Dev\Debug; use SilverStripe\View\ArrayData; use SilverStripe\View\ViewableData; +use Traversable; /** * A list object that wraps around an array of objects or arrays. @@ -81,10 +82,8 @@ class ArrayList extends ViewableData implements SS_List, Filterable, Sortable, L /** * Return the number of items in this list * - * @return int */ - #[\ReturnTypeWillChange] - public function count() + public function count(): int { return count($this->items ?? []); } @@ -102,11 +101,8 @@ class ArrayList extends ViewableData implements SS_List, Filterable, Sortable, L /** * Returns an Iterator for this ArrayList. * This function allows you to use ArrayList in foreach loops - * - * @return ArrayIterator */ - #[\ReturnTypeWillChange] - public function getIterator() + public function getIterator(): Traversable { foreach ($this->items as $i => $item) { if (is_array($item)) { @@ -794,24 +790,16 @@ class ArrayList extends ViewableData implements SS_List, Filterable, Sortable, L /** * Returns whether an item with $key exists - * - * @param mixed $offset - * @return bool */ - #[\ReturnTypeWillChange] - public function offsetExists($offset) + public function offsetExists(mixed $offset): bool { return array_key_exists($offset, $this->items ?? []); } /** * Returns item stored in list with index $key - * - * @param mixed $offset - * @return DataObject */ - #[\ReturnTypeWillChange] - public function offsetGet($offset) + public function offsetGet(mixed $offset): mixed { if ($this->offsetExists($offset)) { return $this->items[$offset]; @@ -821,12 +809,8 @@ class ArrayList extends ViewableData implements SS_List, Filterable, Sortable, L /** * Set an item with the key in $key - * - * @param mixed $offset - * @param mixed $value */ - #[\ReturnTypeWillChange] - public function offsetSet($offset, $value) + public function offsetSet(mixed $offset, mixed $value): void { if ($offset === null) { $this->items[] = $value; @@ -837,11 +821,8 @@ class ArrayList extends ViewableData implements SS_List, Filterable, Sortable, L /** * Unset an item with the key in $key - * - * @param mixed $offset */ - #[\ReturnTypeWillChange] - public function offsetUnset($offset) + public function offsetUnset(mixed $offset): void { unset($this->items[$offset]); } diff --git a/src/ORM/Connect/MySQLQuery.php b/src/ORM/Connect/MySQLQuery.php index 4f8db6993..65213e667 100644 --- a/src/ORM/Connect/MySQLQuery.php +++ b/src/ORM/Connect/MySQLQuery.php @@ -2,6 +2,8 @@ namespace SilverStripe\ORM\Connect; +use Traversable; + /** * A result-set from a MySQL database (using MySQLiConnector) * Note that this class is only used for the results of non-prepared statements @@ -45,8 +47,7 @@ class MySQLQuery extends Query } } - #[\ReturnTypeWillChange] - public function getIterator() + public function getIterator(): Traversable { $floatTypes = [MYSQLI_TYPE_FLOAT, MYSQLI_TYPE_DOUBLE, MYSQLI_TYPE_DECIMAL, MYSQLI_TYPE_NEWDECIMAL]; if (is_object($this->handle)) { diff --git a/src/ORM/Connect/MySQLStatement.php b/src/ORM/Connect/MySQLStatement.php index 81edf8c50..3e95195e9 100644 --- a/src/ORM/Connect/MySQLStatement.php +++ b/src/ORM/Connect/MySQLStatement.php @@ -4,6 +4,7 @@ namespace SilverStripe\ORM\Connect; use mysqli_result; use mysqli_stmt; +use Traversable; /** * Provides a record-view for mysqli prepared statements @@ -101,8 +102,7 @@ class MySQLStatement extends Query call_user_func_array([$this->statement, 'bind_result'], $variables ?? []); } - #[\ReturnTypeWillChange] - public function getIterator() + public function getIterator(): Traversable { while ($this->statement->fetch()) { // Dereferenced row diff --git a/src/ORM/Connect/Query.php b/src/ORM/Connect/Query.php index c4c4a4dbf..b27220667 100644 --- a/src/ORM/Connect/Query.php +++ b/src/ORM/Connect/Query.php @@ -4,6 +4,7 @@ namespace SilverStripe\ORM\Connect; use SilverStripe\Core\Convert; use Iterator; +use Traversable; /** * Abstract query-result class. A query result provides an iterator that returns a map for each record of a query @@ -147,8 +148,7 @@ abstract class Query implements \IteratorAggregate /** * Return the next record in the query result. */ - #[\ReturnTypeWillChange] - abstract public function getIterator(); + abstract public function getIterator(): Traversable; /** * Return the total number of items in the query result. diff --git a/src/ORM/DataList.php b/src/ORM/DataList.php index 4b1f4afc0..8fe1911c1 100644 --- a/src/ORM/DataList.php +++ b/src/ORM/DataList.php @@ -6,12 +6,12 @@ use SilverStripe\Core\Injector\Injector; use SilverStripe\Dev\Debug; use SilverStripe\ORM\Filters\SearchFilter; use SilverStripe\ORM\Queries\SQLConditionGroup; -use SilverStripe\View\TemplateIterator; use SilverStripe\View\ViewableData; -use ArrayIterator; use Exception; use InvalidArgumentException; use LogicException; +use BadMethodCallException; +use Traversable; /** * Implements a "lazy loading" DataObjectSet. @@ -915,11 +915,8 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li /** * Returns an Iterator for this DataList. * This function allows you to use DataLists in foreach loops - * - * @return Generator */ - #[\ReturnTypeWillChange] - public function getIterator() + public function getIterator(): Traversable { foreach ($this->getFinalisedQuery() as $row) { yield $this->createDataObject($row); @@ -948,11 +945,8 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li /** * Return the number of items in this DataList - * - * @return int */ - #[\ReturnTypeWillChange] - public function count() + public function count(): int { if ($this->finalisedQuery) { return $this->finalisedQuery->numRecords(); @@ -1329,12 +1323,8 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li /** * Returns whether an item with $key exists - * - * @param mixed $key - * @return bool */ - #[\ReturnTypeWillChange] - public function offsetExists($key) + public function offsetExists(mixed $key): bool { return ($this->limit(1, $key)->first() != null); } @@ -1343,37 +1333,29 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li * Returns item stored in list with index $key * * The object returned is not cached, unlike {@link DataObject::get_one()} - * - * @param mixed $key - * @return DataObject */ - #[\ReturnTypeWillChange] - public function offsetGet($key) + public function offsetGet(mixed $key): ?DataObject { return $this->limit(1, $key)->first(); } /** * Set an item with the key in $key - * - * @param mixed $key - * @param mixed $value + * @throws BadMethodCallException */ - #[\ReturnTypeWillChange] - public function offsetSet($key, $value) + public function offsetSet(mixed $key, mixed $value): void { - throw new \BadMethodCallException("Can't alter items in a DataList using array-access"); + throw new BadMethodCallException("Can't alter items in a DataList using array-access"); } /** * Unset an item with the key in $key * - * @param mixed $key + * @throws BadMethodCallException */ - #[\ReturnTypeWillChange] - public function offsetUnset($key) + public function offsetUnset(mixed $key): void { - throw new \BadMethodCallException("Can't alter items in a DataList using array-access"); + throw new BadMethodCallException("Can't alter items in a DataList using array-access"); } /** diff --git a/src/ORM/ListDecorator.php b/src/ORM/ListDecorator.php index d3f006e64..7a3226dd4 100644 --- a/src/ORM/ListDecorator.php +++ b/src/ORM/ListDecorator.php @@ -4,6 +4,7 @@ namespace SilverStripe\ORM; use SilverStripe\View\ViewableData; use LogicException; +use Traversable; /** * A base class for decorators that wrap around a list to provide additional @@ -50,28 +51,22 @@ abstract class ListDecorator extends ViewableData implements SS_List, Sortable, return $this; } - // PROXIED METHODS --------------------------------------------------------- - - #[\ReturnTypeWillChange] - public function offsetExists($key) + public function offsetExists(mixed $key): bool { return $this->list->offsetExists($key); } - #[\ReturnTypeWillChange] - public function offsetGet($key) + public function offsetGet(mixed $key): mixed { return $this->list->offsetGet($key); } - #[\ReturnTypeWillChange] - public function offsetSet($key, $value) + public function offsetSet(mixed $key, mixed $value): void { $this->list->offsetSet($key, $value); } - #[\ReturnTypeWillChange] - public function offsetUnset($key) + public function offsetUnset(mixed $key): void { $this->list->offsetUnset($key); } @@ -96,8 +91,7 @@ abstract class ListDecorator extends ViewableData implements SS_List, Sortable, $this->list->remove($itemObject); } - #[\ReturnTypeWillChange] - public function getIterator() + public function getIterator(): Traversable { return $this->list->getIterator(); } @@ -122,8 +116,7 @@ abstract class ListDecorator extends ViewableData implements SS_List, Sortable, return $this->list->count(); } - #[\ReturnTypeWillChange] - public function Count() + public function Count(): int { return $this->list->count(); } diff --git a/src/ORM/Map.php b/src/ORM/Map.php index d062cd4c6..a1b81f609 100644 --- a/src/ORM/Map.php +++ b/src/ORM/Map.php @@ -3,8 +3,10 @@ namespace SilverStripe\ORM; use ArrayAccess; +use BadMethodCallException; use Countable; use IteratorAggregate; +use Traversable; /** * Creates a map from an SS_List by defining a key column and a value column. @@ -143,15 +145,7 @@ class Map implements ArrayAccess, Countable, IteratorAggregate return $this; } - // ArrayAccess - - /** - * @var string $key - * - * @return boolean - */ - #[\ReturnTypeWillChange] - public function offsetExists($key) + public function offsetExists(mixed $key): bool { if (isset($this->firstItems[$key])) { return true; @@ -166,13 +160,7 @@ class Map implements ArrayAccess, Countable, IteratorAggregate return $record != null; } - /** - * @var string $key - * - * @return mixed - */ - #[\ReturnTypeWillChange] - public function offsetGet($key) + public function offsetGet(mixed $key): mixed { if (isset($this->firstItems[$key])) { return $this->firstItems[$key]; @@ -201,11 +189,9 @@ class Map implements ArrayAccess, Countable, IteratorAggregate * {@link DataQuery} instance. In this case, use {@link Map::toArray()} * and manipulate the resulting array. * - * @var string $key - * @var mixed $value + * @throws BadMethodCallException */ - #[\ReturnTypeWillChange] - public function offsetSet($key, $value) + public function offsetSet(mixed $key, mixed $value): void { if (isset($this->firstItems[$key])) { $this->firstItems[$key] = $value; @@ -215,7 +201,7 @@ class Map implements ArrayAccess, Countable, IteratorAggregate $this->lastItems[$key] = $value; } - throw new \BadMethodCallException('Map is read-only. Please use $map->push($key, $value) to append values'); + throw new BadMethodCallException('Map is read-only. Please use $map->push($key, $value) to append values'); } /** @@ -226,39 +212,30 @@ class Map implements ArrayAccess, Countable, IteratorAggregate * {@link DataQuery} instance. In this case, use {@link Map::toArray()} * and manipulate the resulting array. * - * @var string $key - * @var mixed $value + * @throws BadMethodCallException */ - #[\ReturnTypeWillChange] - public function offsetUnset($key) + public function offsetUnset(mixed $key): void { if (isset($this->firstItems[$key])) { unset($this->firstItems[$key]); - return; } if (isset($this->lastItems[$key])) { unset($this->lastItems[$key]); - return; } - throw new \BadMethodCallException( - 'Map is read-only. Unset cannot be called on keys derived from the DataQuery' + throw new BadMethodCallException( + 'Map is read-only. Unset cannot be called on keys derived from the DataQuery.' ); } /** * Returns an Map_Iterator instance for iterating over the complete set * of items in the map. - * - * Satisfies the IteratorAggreagte interface. - * - * @return Map_Iterator */ - #[\ReturnTypeWillChange] - public function getIterator() + public function getIterator(): Traversable { $keyField = $this->keyField; $valueField = $this->valueField; @@ -307,11 +284,8 @@ class Map implements ArrayAccess, Countable, IteratorAggregate /** * Returns the count of items in the list including the additional items set * through {@link Map::push()} and {@link Map::unshift}. - * - * @return int */ - #[\ReturnTypeWillChange] - public function count() + public function count(): int { return $this->list->count() + count($this->firstItems ?? []) + diff --git a/src/ORM/PaginatedList.php b/src/ORM/PaginatedList.php index 96a6fed9f..d68c2f9c1 100644 --- a/src/ORM/PaginatedList.php +++ b/src/ORM/PaginatedList.php @@ -9,6 +9,7 @@ use SilverStripe\View\ArrayData; use ArrayAccess; use Exception; use IteratorIterator; +use Traversable; /** * A decorator that wraps around a data list in order to provide pagination. @@ -208,11 +209,7 @@ class PaginatedList extends ListDecorator return $this; } - /** - * @return IteratorIterator - */ - #[\ReturnTypeWillChange] - public function getIterator() + public function getIterator(): Traversable { $pageLength = $this->getPageLength(); if ($this->limitItems && $pageLength) { diff --git a/src/ORM/UnsavedRelationList.php b/src/ORM/UnsavedRelationList.php index 51cde920f..d2f3c85b2 100644 --- a/src/ORM/UnsavedRelationList.php +++ b/src/ORM/UnsavedRelationList.php @@ -5,6 +5,7 @@ namespace SilverStripe\ORM; use InvalidArgumentException; use ArrayIterator; use SilverStripe\ORM\FieldType\DBField; +use Traversable; /** * An {@link ArrayList} that represents an unsaved relation. @@ -120,11 +121,8 @@ class UnsavedRelationList extends ArrayList implements Relation /** * Returns an Iterator for this relation. - * - * @return ArrayIterator */ - #[\ReturnTypeWillChange] - public function getIterator() + public function getIterator(): Traversable { return new ArrayIterator($this->toArray()); } diff --git a/src/View/ViewableData.php b/src/View/ViewableData.php index 6ba6c853b..a8340536f 100644 --- a/src/View/ViewableData.php +++ b/src/View/ViewableData.php @@ -20,6 +20,7 @@ use SilverStripe\ORM\ArrayLib; use SilverStripe\ORM\FieldType\DBField; use SilverStripe\ORM\FieldType\DBHTMLText; use SilverStripe\View\SSViewer; +use Traversable; use UnexpectedValueException; /** @@ -601,8 +602,7 @@ class ViewableData implements IteratorAggregate * * @return ArrayIterator */ - #[\ReturnTypeWillChange] - public function getIterator() + public function getIterator(): Traversable { return new ArrayIterator([$this]); } diff --git a/tests/php/ORM/ListDecoratorTest.php b/tests/php/ORM/ListDecoratorTest.php index 0410bba76..213857aa1 100644 --- a/tests/php/ORM/ListDecoratorTest.php +++ b/tests/php/ORM/ListDecoratorTest.php @@ -166,8 +166,8 @@ class ListDecoratorTest extends SapphireTest public function testOffsetExists() { - $this->list->expects($this->once())->method('offsetExists')->with('foo')->willReturn('mock'); - $this->assertSame('mock', $this->decorator->offsetExists('foo')); + $this->list->expects($this->once())->method('offsetExists')->with('foo')->willReturn(true); + $this->assertSame(true, $this->decorator->offsetExists('foo')); } public function testGetList()