doctrine / mongodb-odm

The Official PHP MongoDB ORM/ODM
https://www.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/
MIT License
1.09k stars 504 forks source link

Void methods in collections causing fatal error #2256

Closed Lewiatan closed 3 years ago

Lewiatan commented 3 years ago

Bug Report

I have a collection that extends Doctrine\Common\Collections\ArrayCollection which is persisted to database. This collection has methods that returns void, for example:

public function removeProduct(ProductId $productId): void
    {
        $index = $this->indexOf($productId);

        if ($index !== false) {
            $this->remove($index);
        }
    }

The generated persistent collection is trying to return value from void method:

/**
     * {@inheritDoc}
     */
    public function removeProduct(ProductId $productId): void
    {
        $this->initialize();
        if ($this->needsSchedulingForSynchronization()) {
            $this->changed();
        }
        return $this->coll->removeProduct($productId);
    }

I updated symfony recently, and during that process doctrine/common was also updated. I think that with doctrine/common in version 2.12 it was working, but I'm not 100% sure if I had outdated cache. I can verify that if needed.

Package Version
doctrine/mongodb-odm-bundle 4.2.2
doctrine/common 3.0.2

How to reproduce

  1. Create collection that is persisted to database
  2. Add void method
  3. Try to store collection

Expected behavior

Void methods are supported by proxy classes

malarzm commented 3 years ago

@Lewiatan thank you for the bug report! The collection code is generated in ODM itself, if you'd want to take a stab at fixing the bug it's here: https://github.com/doctrine/mongodb-odm/blob/3bdfe97be472160ebbc924d5882ce61bd640a515/lib/Doctrine/ODM/MongoDB/PersistentCollection/DefaultPersistentCollectionGenerator.php#L204. The generated code template needs to have the return part removed if void is declared as a return type. You'd also need to add a test case, here's a good candidate to add it to: https://github.com/doctrine/mongodb-odm/blob/3bdfe97be472160ebbc924d5882ce61bd640a515/tests/Doctrine/ODM/MongoDB/Tests/Functional/CustomCollectionsTest.php.

Lewiatan commented 3 years ago

Sure, I'll give it a try ;)