Skip to content

Conversation

@TomasVotruba
Copy link
Member

@TomasVotruba TomasVotruba commented Oct 12, 2025

These structure are identical and can be narrowed:

-if ($result === 1000) {
-	return 1000;
-}

 return $result;

It doesn't make sense to compare variable to X and then return X or X. Just return X directly :)

@TomasVotruba TomasVotruba changed the title tv empty value or type return [dead-code] Add RemoveEmptyArrayConditionReturnRector Oct 12, 2025
@TomasVotruba
Copy link
Member Author

TomasVotruba commented Oct 12, 2025

I've realized this can be extended for any compared value, WIP

@TomasVotruba TomasVotruba changed the title [dead-code] Add RemoveEmptyArrayConditionReturnRector [dead-code] Add RemoveConditionExactReturnRector Oct 12, 2025
@TomasVotruba TomasVotruba force-pushed the tv-empty-value-or-type-return branch from 320cef7 to 98456f6 Compare October 12, 2025 09:02
@TomasVotruba TomasVotruba force-pushed the tv-empty-value-or-type-return branch from b4aa078 to 7f19815 Compare October 12, 2025 09:06
@TomasVotruba TomasVotruba merged commit cbe7e73 into main Oct 12, 2025
52 checks passed
@TomasVotruba TomasVotruba deleted the tv-empty-value-or-type-return branch October 12, 2025 09:09
Comment on lines +98 to +102
if ($identicalOrEqual->right instanceof MethodCall) {
continue;
}

if ($identicalOrEqual->right instanceof StaticCall) {
Copy link
Member

Choose a reason for hiding this comment

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

there is SideEffectNodeDetector service for it, I will create new PR for it.

Copy link
Member

Choose a reason for hiding this comment

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

Comment on lines +31 to +57
<<<'CODE_SAMPLE'
final class SomeClass
{
public function __construct(array $items)
{
if ($items === []) {
return [];
}

return $items;
}
}
CODE_SAMPLE

,
<<<'CODE_SAMPLE'
final class SomeClass
{
public function __construct(array $items)
{
if ($items === []) {
return [];
}

return $items;
}
}
Copy link
Member

Choose a reason for hiding this comment

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

before vs after code sample is equal, detected at getrector.com website

https://github.com/rectorphp/getrector-com/actions/runs/18443711056/job/52547527088?pr=3361#step:5:17

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

We should have a CI check for this here.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants