diff --git a/lib/Doctrine/Common/Collections/AbstractLazyCollection.php b/lib/Doctrine/Common/Collections/AbstractLazyCollection.php index fc661ea1..8e043343 100644 --- a/lib/Doctrine/Common/Collections/AbstractLazyCollection.php +++ b/lib/Doctrine/Common/Collections/AbstractLazyCollection.php @@ -3,6 +3,7 @@ namespace Doctrine\Common\Collections; use Closure; +use LogicException; use ReturnTypeWillChange; use Traversable; @@ -18,8 +19,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; @@ -356,6 +357,8 @@ public function isInitialized() * Initialize the collection * * @return void + * + * @psalm-assert Collection $this->collection */ protected function initialize() { @@ -365,6 +368,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.'); + } } /** 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'