Skip to content

[Console] Hidden option using PHP 8 AsCommand attribute #41942

Closed
@duboiss

Description

@duboiss

Symfony version(s) affected: 5.3.0 to current latest 5.3.3

Description
I always get a different behavior between hidden option in php 8 attribute (#[AsCommand]) and the setHidden() method in configure method even with the fix #41686. Also with cache:clear.

How to reproduce
I've created a project (see below) to simplify the tests, but :

  • Declare the DefaultCommand class in src/Command:
<?php

namespace App\Command;

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class DefaultCommand extends Command
{
    protected static $defaultName = 'app:default';

    protected function configure(): void
    {
        $this
            ->setDescription('wrapper')
            ->setHidden(true)
        ;
    }

    protected function execute(InputInterface $input, OutputInterface $output): ?int
    {
        /** @var Application $application */
        $application = $this->getApplication();

        $command = $application->find('list');
        $arguments = ['namespace' => 'app'];

        $listInput = new ArrayInput($arguments);

        return $command->run($listInput, $output);
    }
}
  • Declare a new Application.php class in src folder and use it in bin/console instead of the default Application class (just need to change the namespace):

src/Application.php

<?php

declare(strict_types=1);

namespace App;

use App\Command\DefaultCommand;
use Symfony\Bundle\FrameworkBundle\Console\Application as SymfonyApplication;
use Symfony\Component\HttpKernel\KernelInterface;

class Application extends SymfonyApplication
{
    public function __construct(KernelInterface $kernel)
    {
        parent::__construct($kernel);

        if ($defaultName = DefaultCommand::getDefaultName()) {
            $this->setDefaultCommand($defaultName);
        }
    }
}

bin/console

#!/usr/bin/env php
<?php

use App\Kernel;
use App\Application;

if (!is_file(dirname(__DIR__).'/vendor/autoload_runtime.php')) {
    throw new LogicException('Symfony Runtime is missing. Try running "composer require symfony/runtime".');
}

require_once dirname(__DIR__).'/vendor/autoload_runtime.php';

return function (array $context) {
    $kernel = new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']);

    return new Application($kernel);
};
  • Make a random command, for exemple app:hello with or without PHP 8 attributes.
  • See the output of bin/console. Update DefaultCommand with PHP 8 #[AsCommand] attribute and observe the difference.

OR use the following project as exemple
https://github.com/duboiss/sfcommand

git clone https://github.com/duboiss/sfcommand
cd sfcommand
composer install
bin/console

git switch php8
bin/console

Additional context
Without attribute
image

With attribute
image

This is probably due to the fact that it has become lazy. Is there any way to keep the PHP 8 attribute and disable lazy (if that is the cause)?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions