Issue Bug Closed

#97522Word "new" in command "description" breaks DI

Mathias Brodala 04/29/22, 16:44 updated 05/02/22, 07:18

Description

Given a command definition like this …

services:
  _defaults:
    autowire: true
    autoconfigure: true
    public: false

  Acme\Foo\:
    resource: '../Classes/*'

  Acme\Foo\Command\BarCommand:
    tags:
      - name: console.command
        command: example:bar
        description: Build a new bar
        schedulable: false

  Acme\Foo\Command\QuxCommand:
    tags:
      - name: console.command
        command: example:qux
        description: Do something
        schedulable: false

… you get an error like this:

 [ ParseError ]                                                         
  syntax error, unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM), expecting '('  

Exception trace:
#0 ()
   var/cache/code/di/DependencyInjectionContainer_<hash>.php:<line>
...

The generated service in the DI cache file:

    /**
     * Gets the public 'Acme\Foo\Command\BarCommand' shared autowired service.
     *
     * @return \Acme\Foo\Command\BarCommand
     */
    protected function getBarCommandService()
    {
        $this->services['Acme\\Foo\\Command\\BarCommand'] = $instance = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstanceForDi(\Acme\Foo\Command\BarCommand::class, \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstanceForDi(\Acme\Foo\Security\BarDependency::class));

        $instance->setName('example:bar');
        $instance->setDescription('Build a \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstanceForDi(bar');

        return $instance;
    }

    /**
     * Gets the public 'Acme\Foo\Command\QuxCommand' shared autowired service.
     *
     * @return \Acme\Foo\Command\QuxCommand
     */
    protected function getQuxCommandService::class)
    {
        # ...
    }

This is very likely caused by the rather simple replacement here:

https://github.com/TYPO3/typo3/blob/6f7514f51634fd98a4d4def818c3f16e28aff538/typo3/sysext/core/Classes/DependencyInjection/ContainerBuilder.php#L166

$code = preg_replace('/new ([^\(\s]+)\(/', '\\TYPO3\\CMS\\Core\\Utility\\GeneralUtility::makeInstanceForDi(\\1::class, ', $code);

Reviews 0

No change resolves or names this issue in its commit message, and no comment links one.