symfony / maker-bundle

Symfony Maker Bundle
https://symfony.com/
MIT License
3.37k stars 404 forks source link

[make:entity] Crash when return type from DoctrineHelper is mixed #1612

Open pkly opened 2 weeks ago

pkly commented 2 weeks ago

Hello,

I'm able to reliably crash the make:entity command on 1.61 because of the following line: https://github.com/symfony/maker-bundle/blob/ce60831a9d6c41c032117ddf59f303f56d004c87/src/Doctrine/DoctrineHelper.php#L291

As this helper relies on the convertToPHPValue return type (very clever btw) it will crash if the return value is set to mixed. This can be replicated with any custom doctrine type that is set in the application, with the following stack trace.

in BuilderHelpers.php line 208:

  [LogicException]               
  mixed type cannot be nullable  

Exception trace:
  at /var/www/vendor/nikic/php-parser/lib/PhpParser/BuilderHelpers.php:208
 PhpParser\BuilderHelpers::normalizeType() at /var/www/vendor/nikic/php-parser/lib/PhpParser/Builder/Property.php:127
 PhpParser\Builder\Property->setType() at /var/www/vendor/symfony/maker-bundle/src/Util/ClassSourceManipulator.php:402
 Symfony\Bundle\MakerBundle\Util\ClassSourceManipulator->addProperty() at /var/www/vendor/symfony/maker-bundle/src/Util/ClassSourceManipulator.php:151
 Symfony\Bundle\MakerBundle\Util\ClassSourceManipulator->addEntityField() at /var/www/vendor/symfony/maker-bundle/src/Maker/MakeEntity.php:243
 Symfony\Bundle\MakerBundle\Maker\MakeEntity->generate() at /var/www/vendor/symfony/maker-bundle/src/Command/MakerCommand.php:102
 Symfony\Bundle\MakerBundle\Command\MakerCommand->execute() at /var/www/vendor/symfony/console/Command/Command.php:326

It appears that the builder tries to make a field with type mixed|null or ?mixed but mixed already implicitly includes null, which causes the error.

After updating the return type for our custom type from mixed to a more sensible string|null the error has stopped. Maybe in such a case a note should be printed to cli?

mdoutreluingne commented 1 week ago

Hello,

How can we reproduce the bug with make:entity command?

pkly commented 1 week ago

As I've pointed out above, you can trigger the crash by changing the return type of convertToPHPValue to mixed on the doctrine type class you're using while selecting the field.

For example, you could modify the built-in string column type for doctrine to return that from said method. A crash should occur then.