Skip to content
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

Incorrect behavior of ConsoleExecuteReturnIntRector #8793

Closed
carlos-granados opened this issue Aug 16, 2024 · 2 comments · Fixed by rectorphp/rector-src#6236
Closed

Incorrect behavior of ConsoleExecuteReturnIntRector #8793

carlos-granados opened this issue Aug 16, 2024 · 2 comments · Fixed by rectorphp/rector-src#6236
Labels

Comments

@carlos-granados
Copy link

carlos-granados commented Aug 16, 2024

Bug Report

Subject Details
Rector version last dev-main
Installed as composer dependency

Minimal PHP Code Causing Issue

See https://getrector.com/demo/753c3d4f-c1c8-4e40-aa91-96a739734d7e

<?php

namespace App\Command\User;

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

class TestCommand extends Command
{
    private ?OutputInterface $outputInterface = null;

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        return Command::SUCCESS;
    }
}

Responsible rules

  • ConsoleExecuteReturnIntRector

Expected Behavior

Rector changes the line

        return Command::SUCCESS;

to

        return (int) Command::SUCCESS;

And it shouldn't

I have tried to investigate the issue and as far as I can tell what happens is:

  • Rector applies the rules and on this first run the Command:SUCCESS expression (a ClassConstantFetch node) has the property class equal to "Symfony\Component\Console\Command\Command". The ConsoleExecuteReturnIntRector correctly identifies the type of this expression as an integer and does not change the code
  • The post rectors are applied and the NameImportingPostRector rector is called. This changes the class property of this expression to Command
  • Since there were changes applied, the rector rules are applied again. On this second run, when the ConsoleExecuteReturnIntRector rector tries to identify the type of the expression, since the node does not have the FQN, the getType() function in the MutatingScope returns an ErrorType and, since this is not an int, the (int) cast is added

I tried to figure out a solution but was unable to decide how to go about adding one, hope all these details can help someone else decide how to tackle this

Note: there needs to be another rector rule that changes this file for these erroneous changes to show, could be any rule, I used RemoveUnusedPrivatePropertyRector but any other that changes the file will do. Seems that if the only rector to change the file is ConsoleExecuteReturnIntRector then these changes are not applied

@samsonasik
Copy link
Member

@TomasVotruba it possibly due to removal of Name check by namespaced_name attribute at your PR:

I will look into it ;)

@carlos-granados
Copy link
Author

@samsonasik Can confirm this is working fine now, thanks!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants