Skip to content

InvokableCommandInputAttributeRector test fixture #811

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Rector\Tests\Symfony\Symfony73\Rector\Class_\InvokableCommandInputAttributeRector\Fixture;

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

#[AsCommand('app:my-command')]
final class MyCommand extends Command
{
#[\Override]
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$io->info('Great success!');

return 1;
}
}
?>
-----
<?php

namespace Rector\Tests\Symfony\Symfony73\Rector\Class_\InvokableCommandInputAttributeRector\Fixture;

use Symfony\Component\Console\Attribute\AsCommand;

#[AsCommand('app:my-command')]
final class MyCommand
{
#[\Override]
public function __invoke(\Symfony\Component\Console\Style\SymfonyStyle $io): int
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should not be replaced, skip the change should be the way, right? /cc @darthf1 @thePanz @gharlan could you verify? Thank you.

Copy link
Author

@darthf1 darthf1 Jul 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The blog post has this (almost) exact example, and there $input and $output is changed into $io as well.

https://symfony.com/blog/new-in-symfony-7-3-invokable-commands-and-input-attributes

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made this change in most of my commands after using the InvokableCommandInputAttributeRector.

But I also had some commands where I used the $output as argument for other functions. So there I still need the $output param.

Best would be:

  • When $output is not used anywhere in __invoke => remove $output param altogether
  • When $output is used for new SymfonyStyle($input, $output) => do the change shown here in the pr
  • But when $output is used anywhere else => also keep the $output param for __invoke

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as this is a bugfix, I would keep a KISS solution: @samsonasik 's suggestion to not rewrite the parameters in the constructor looks reasonable as a first step.

{
$io->info('Great success!');

return 1;
}
}
?>
Loading