Search code examples
phpsymfonyfactorysetterfluent

Fluent setter returning null value on a factory - PHP


In a Symfony project I have created the Factory below :

<?php

namespace Company\MyBundle\Factory;

use Company\MyBundle\Entity\ParentRequest;
use Company\MyBundle\Entity\ChildRequest;
use Company\MyBundle\Entity\Foo;
use Company\MyBundle\Entity\Bar;

/**
 * Class ChildRequestFactory
 *
 * Factory instanciating  ChildRequest with several values
 * @package Company\MyBundle\Factory
 */
class ChildRequestFactory extends AbstractRequestFactory
{

    /**
     * @param \DateTime $date
     * @param Bar|null $bar
     * @param Foo|null $foo
     * @return ChildRequest
     */
    public function createRequest(\DateTime $date, Bar $bar 
= null, Foo $foo = null)
    {
        $childRequest = new ChildRequest();
        $childRequest
            ->setDateValidation($date)
            ->setIsValid(ParentRequest::IS_VALIDATED)
            ->setFoo($foo)
            ->setBar($bar)
            ->setBaz('baz')
            ->setOrigin('Manager')
        ;

        return $childRequest;
    }

This factory is used by Service used by a Symfony command. Let's explain the problem right now :

  • When executing the command in local dev environment with dev fixtures, the factory correctly create and return the object.
  • When executing the command in local dev environment with preprod database, the factory correctly create and return the object.
  • When executing the command in preprod environment using preprod database, the following error is raised :

PHP Fatal error: Call to a member function setFoo() on null in /var/www/application/src/Company/MyBundle/Factory/ChildRequestFactory.php on line 37

[Symfony\Component\Debug\Exception\FatalErrorException] Error: Call to a member function setFoo() on null

Exception trace: () at /var/www/application/src/Company/MyBundle/Factory/ChildRequestFactory.php:37

This error only appears on the preprod environment. I have already check the versionning, the code is on the same git branch on the local and the preprod environments. The cache has already been remove in preprod. Database sync/schema update : OK

Symfony version : 2.7.13 (local and preprod) PHP version : 5.6.17 (local) / 5.6.23 (preprod)

When I don't use the fluent setters it's solve the problem on preprod env.

Any idea ?

Notice : I have changed the name of the bundle and some variables/classes/methods for confidentiality.

Update :

This is the setIsValid setter returning null.

/**
 * Set is_valid
 *
 * @param integer $isValid
 */
public function setIsValid($isValid)
{
    $this->isValid = $isValid;

    return $this;
}

Solution

  • So I found the origin of the problem. I usually do operations like rebases, interactive rebases or bisects with command line but I use GitKraken for simple commits and pushs. I suppose there was some issues using it because the changes including making all setters fluent was not pushed to the preprod environment. What a shame I didn't see it...all the new changes were present in the preprod environment except these one. Thanks everyone.