From fe0c933c14022cde31b620d3f7ed5b03774615d7 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Mon, 8 Aug 2022 10:41:56 +0200 Subject: [PATCH 1/2] Update phpdoc of the lazy collection --- .../Collections/AbstractLazyCollection.php | 41 ++++++++++++++++++- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/lib/Doctrine/Common/Collections/AbstractLazyCollection.php b/lib/Doctrine/Common/Collections/AbstractLazyCollection.php index fc661ea1..90dbe72f 100644 --- a/lib/Doctrine/Common/Collections/AbstractLazyCollection.php +++ b/lib/Doctrine/Common/Collections/AbstractLazyCollection.php @@ -3,9 +3,12 @@ namespace Doctrine\Common\Collections; use Closure; +use LogicException; use ReturnTypeWillChange; use Traversable; +use function assert; + /** * Lazy collection that is backed by a concrete collection * @@ -18,8 +21,8 @@ abstract class AbstractLazyCollection implements Collection /** * The backed collection to use * - * @psalm-var Collection - * @var Collection + * @psalm-var Collection|null + * @var Collection|null */ protected $collection; @@ -35,6 +38,7 @@ abstract class AbstractLazyCollection implements Collection public function count() { $this->initialize(); + assert($this->collection !== null); return $this->collection->count(); } @@ -45,6 +49,7 @@ public function count() public function add($element) { $this->initialize(); + assert($this->collection !== null); return $this->collection->add($element); } @@ -55,6 +60,7 @@ public function add($element) public function clear() { $this->initialize(); + assert($this->collection !== null); $this->collection->clear(); } @@ -64,6 +70,7 @@ public function clear() public function contains($element) { $this->initialize(); + assert($this->collection !== null); return $this->collection->contains($element); } @@ -74,6 +81,7 @@ public function contains($element) public function isEmpty() { $this->initialize(); + assert($this->collection !== null); return $this->collection->isEmpty(); } @@ -84,6 +92,7 @@ public function isEmpty() public function remove($key) { $this->initialize(); + assert($this->collection !== null); return $this->collection->remove($key); } @@ -94,6 +103,7 @@ public function remove($key) public function removeElement($element) { $this->initialize(); + assert($this->collection !== null); return $this->collection->removeElement($element); } @@ -104,6 +114,7 @@ public function removeElement($element) public function containsKey($key) { $this->initialize(); + assert($this->collection !== null); return $this->collection->containsKey($key); } @@ -114,6 +125,7 @@ public function containsKey($key) public function get($key) { $this->initialize(); + assert($this->collection !== null); return $this->collection->get($key); } @@ -124,6 +136,7 @@ public function get($key) public function getKeys() { $this->initialize(); + assert($this->collection !== null); return $this->collection->getKeys(); } @@ -134,6 +147,7 @@ public function getKeys() public function getValues() { $this->initialize(); + assert($this->collection !== null); return $this->collection->getValues(); } @@ -144,6 +158,7 @@ public function getValues() public function set($key, $value) { $this->initialize(); + assert($this->collection !== null); $this->collection->set($key, $value); } @@ -153,6 +168,7 @@ public function set($key, $value) public function toArray() { $this->initialize(); + assert($this->collection !== null); return $this->collection->toArray(); } @@ -163,6 +179,7 @@ public function toArray() public function first() { $this->initialize(); + assert($this->collection !== null); return $this->collection->first(); } @@ -173,6 +190,7 @@ public function first() public function last() { $this->initialize(); + assert($this->collection !== null); return $this->collection->last(); } @@ -183,6 +201,7 @@ public function last() public function key() { $this->initialize(); + assert($this->collection !== null); return $this->collection->key(); } @@ -193,6 +212,7 @@ public function key() public function current() { $this->initialize(); + assert($this->collection !== null); return $this->collection->current(); } @@ -203,6 +223,7 @@ public function current() public function next() { $this->initialize(); + assert($this->collection !== null); return $this->collection->next(); } @@ -213,6 +234,7 @@ public function next() public function exists(Closure $p) { $this->initialize(); + assert($this->collection !== null); return $this->collection->exists($p); } @@ -223,6 +245,7 @@ public function exists(Closure $p) public function filter(Closure $p) { $this->initialize(); + assert($this->collection !== null); return $this->collection->filter($p); } @@ -233,6 +256,7 @@ public function filter(Closure $p) public function forAll(Closure $p) { $this->initialize(); + assert($this->collection !== null); return $this->collection->forAll($p); } @@ -243,6 +267,7 @@ public function forAll(Closure $p) public function map(Closure $func) { $this->initialize(); + assert($this->collection !== null); return $this->collection->map($func); } @@ -253,6 +278,7 @@ public function map(Closure $func) public function partition(Closure $p) { $this->initialize(); + assert($this->collection !== null); return $this->collection->partition($p); } @@ -263,6 +289,7 @@ public function partition(Closure $p) public function indexOf($element) { $this->initialize(); + assert($this->collection !== null); return $this->collection->indexOf($element); } @@ -273,6 +300,7 @@ public function indexOf($element) public function slice($offset, $length = null) { $this->initialize(); + assert($this->collection !== null); return $this->collection->slice($offset, $length); } @@ -287,6 +315,7 @@ public function slice($offset, $length = null) public function getIterator() { $this->initialize(); + assert($this->collection !== null); return $this->collection->getIterator(); } @@ -300,6 +329,7 @@ public function getIterator() public function offsetExists($offset) { $this->initialize(); + assert($this->collection !== null); return $this->collection->offsetExists($offset); } @@ -313,6 +343,7 @@ public function offsetExists($offset) public function offsetGet($offset) { $this->initialize(); + assert($this->collection !== null); return $this->collection->offsetGet($offset); } @@ -327,6 +358,7 @@ public function offsetGet($offset) public function offsetSet($offset, $value) { $this->initialize(); + assert($this->collection !== null); $this->collection->offsetSet($offset, $value); } @@ -339,6 +371,7 @@ public function offsetSet($offset, $value) public function offsetUnset($offset) { $this->initialize(); + assert($this->collection !== null); $this->collection->offsetUnset($offset); } @@ -365,6 +398,10 @@ protected function initialize() $this->doInitialize(); $this->initialized = true; + + if ($this->collection === null) { + throw new LogicException('You must initialize the collection property in the doInitialize() method.'); + } } /** From e732b16c4521ea7629cdce53cd11384f139e2592 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Sun, 14 Aug 2022 18:00:23 +0200 Subject: [PATCH 2/2] Use psalm-assert --- .../Collections/AbstractLazyCollection.php | 34 ++----------------- phpstan.neon.dist | 3 ++ 2 files changed, 5 insertions(+), 32 deletions(-) diff --git a/lib/Doctrine/Common/Collections/AbstractLazyCollection.php b/lib/Doctrine/Common/Collections/AbstractLazyCollection.php index 90dbe72f..8e043343 100644 --- a/lib/Doctrine/Common/Collections/AbstractLazyCollection.php +++ b/lib/Doctrine/Common/Collections/AbstractLazyCollection.php @@ -7,8 +7,6 @@ use ReturnTypeWillChange; use Traversable; -use function assert; - /** * Lazy collection that is backed by a concrete collection * @@ -38,7 +36,6 @@ abstract class AbstractLazyCollection implements Collection public function count() { $this->initialize(); - assert($this->collection !== null); return $this->collection->count(); } @@ -49,7 +46,6 @@ public function count() public function add($element) { $this->initialize(); - assert($this->collection !== null); return $this->collection->add($element); } @@ -60,7 +56,6 @@ public function add($element) public function clear() { $this->initialize(); - assert($this->collection !== null); $this->collection->clear(); } @@ -70,7 +65,6 @@ public function clear() public function contains($element) { $this->initialize(); - assert($this->collection !== null); return $this->collection->contains($element); } @@ -81,7 +75,6 @@ public function contains($element) public function isEmpty() { $this->initialize(); - assert($this->collection !== null); return $this->collection->isEmpty(); } @@ -92,7 +85,6 @@ public function isEmpty() public function remove($key) { $this->initialize(); - assert($this->collection !== null); return $this->collection->remove($key); } @@ -103,7 +95,6 @@ public function remove($key) public function removeElement($element) { $this->initialize(); - assert($this->collection !== null); return $this->collection->removeElement($element); } @@ -114,7 +105,6 @@ public function removeElement($element) public function containsKey($key) { $this->initialize(); - assert($this->collection !== null); return $this->collection->containsKey($key); } @@ -125,7 +115,6 @@ public function containsKey($key) public function get($key) { $this->initialize(); - assert($this->collection !== null); return $this->collection->get($key); } @@ -136,7 +125,6 @@ public function get($key) public function getKeys() { $this->initialize(); - assert($this->collection !== null); return $this->collection->getKeys(); } @@ -147,7 +135,6 @@ public function getKeys() public function getValues() { $this->initialize(); - assert($this->collection !== null); return $this->collection->getValues(); } @@ -158,7 +145,6 @@ public function getValues() public function set($key, $value) { $this->initialize(); - assert($this->collection !== null); $this->collection->set($key, $value); } @@ -168,7 +154,6 @@ public function set($key, $value) public function toArray() { $this->initialize(); - assert($this->collection !== null); return $this->collection->toArray(); } @@ -179,7 +164,6 @@ public function toArray() public function first() { $this->initialize(); - assert($this->collection !== null); return $this->collection->first(); } @@ -190,7 +174,6 @@ public function first() public function last() { $this->initialize(); - assert($this->collection !== null); return $this->collection->last(); } @@ -201,7 +184,6 @@ public function last() public function key() { $this->initialize(); - assert($this->collection !== null); return $this->collection->key(); } @@ -212,7 +194,6 @@ public function key() public function current() { $this->initialize(); - assert($this->collection !== null); return $this->collection->current(); } @@ -223,7 +204,6 @@ public function current() public function next() { $this->initialize(); - assert($this->collection !== null); return $this->collection->next(); } @@ -234,7 +214,6 @@ public function next() public function exists(Closure $p) { $this->initialize(); - assert($this->collection !== null); return $this->collection->exists($p); } @@ -245,7 +224,6 @@ public function exists(Closure $p) public function filter(Closure $p) { $this->initialize(); - assert($this->collection !== null); return $this->collection->filter($p); } @@ -256,7 +234,6 @@ public function filter(Closure $p) public function forAll(Closure $p) { $this->initialize(); - assert($this->collection !== null); return $this->collection->forAll($p); } @@ -267,7 +244,6 @@ public function forAll(Closure $p) public function map(Closure $func) { $this->initialize(); - assert($this->collection !== null); return $this->collection->map($func); } @@ -278,7 +254,6 @@ public function map(Closure $func) public function partition(Closure $p) { $this->initialize(); - assert($this->collection !== null); return $this->collection->partition($p); } @@ -289,7 +264,6 @@ public function partition(Closure $p) public function indexOf($element) { $this->initialize(); - assert($this->collection !== null); return $this->collection->indexOf($element); } @@ -300,7 +274,6 @@ public function indexOf($element) public function slice($offset, $length = null) { $this->initialize(); - assert($this->collection !== null); return $this->collection->slice($offset, $length); } @@ -315,7 +288,6 @@ public function slice($offset, $length = null) public function getIterator() { $this->initialize(); - assert($this->collection !== null); return $this->collection->getIterator(); } @@ -329,7 +301,6 @@ public function getIterator() public function offsetExists($offset) { $this->initialize(); - assert($this->collection !== null); return $this->collection->offsetExists($offset); } @@ -343,7 +314,6 @@ public function offsetExists($offset) public function offsetGet($offset) { $this->initialize(); - assert($this->collection !== null); return $this->collection->offsetGet($offset); } @@ -358,7 +328,6 @@ public function offsetGet($offset) public function offsetSet($offset, $value) { $this->initialize(); - assert($this->collection !== null); $this->collection->offsetSet($offset, $value); } @@ -371,7 +340,6 @@ public function offsetSet($offset, $value) public function offsetUnset($offset) { $this->initialize(); - assert($this->collection !== null); $this->collection->offsetUnset($offset); } @@ -389,6 +357,8 @@ public function isInitialized() * Initialize the collection * * @return void + * + * @psalm-assert Collection $this->collection */ protected function initialize() { diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 5021eb0e..0b85a3dc 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -6,3 +6,6 @@ parameters: - message: '~Parameter #1 \$key of method Doctrine\\Common\\Collections\\ArrayCollection::set\(\) expects TKey of \(int\|string\), int\|string given\.~' path: 'lib/Doctrine/Common/Collections/ArrayCollection.php' + - + message: '~Cannot call method .* on Doctrine\\Common\\Collections\\Collection\|null\.~' + path: 'lib/Doctrine/Common/Collections/AbstractLazyCollection.php'