Skip to content

[DeadCode] Skip BooleanAnd in trait on RemoveAlwaysTrueIfConditionRector - #9003

Merged
TomasVotruba merged 1 commit into
rectorphp:mainfrom
odolbeau:fix/always-true-boolean-and-in-trait
Nov 18, 2026
Merged

TomasVotruba merged 1 commit into
rectorphp:mainfrom
odolbeau:fix/always-true-boolean-and-in-trait

Conversation

@odolbeau

Copy link
Copy Markdown
Contributor

RemoveAlwaysTrueIfConditionRector already skips traits (#7759), but the check runs after the BooleanAnd branch, so if ($a && $b) inside a trait is still refactored.

Inside a trait, types are resolved without the class using it. Here $this->options['usage'] ?? 'create' resolves to 'create', so the left side of && is seen as always true and gets removed:

trait SomeTrait
{
    public function check($id): string
    {
        $usage = $this->options['usage'] ?? 'create';

        if ($usage === 'update' && ! $id) {
            return 'missing';
        }

        if ($usage === 'create' && $id) { // becomes `if ($id)`, wrong when $usage is 'update'
            return 'exists';
        }

        return 'ok';
    }
}

The same method in a class is left untouched.

This moves the trait check to the top of refactor(), so it covers both branches. Added the fixture above.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@TomasVotruba
TomasVotruba merged commit dd5aec5 into rectorphp:main Nov 18, 2026
51 checks passed
@TomasVotruba

Copy link
Copy Markdown
Member

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants