Skip to content

Conversation

@peterfox
Copy link
Contributor

Changes

  • Adds two test fixtures for RemoveDuplicatedArrayKeyRector
  • RemoveDuplicatedArrayKeyRector to ignore keys that use CallLike keys or PropertyFetches

Why

I found this dead code rule could end up removing array keys if the key was set by a method call, function call or a property fetch that might be dynamic. There might be a way to allow these options back by using PHPStan to look for constant values returned by the dynamic call but for now I think it best to just exclude them so the rule is less likely to remove intended user code.

@TomasVotruba TomasVotruba merged commit c75ecb5 into rectorphp:main Jun 18, 2025
45 checks passed
@TomasVotruba
Copy link
Member

Thank you Peter 🙂

Comment on lines +99 to +105
if ($arrayItem->key instanceof CallLike) {
continue;
}

if ($arrayItem->key instanceof PropertyFetch) {
continue;
}
Copy link
Member

Choose a reason for hiding this comment

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

this should use ExprAnalyzer->isDynamicExpr()

Copy link
Member

Choose a reason for hiding this comment

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


namespace Rector\Tests\DeadCode\Rector\Array_\RemoveDuplicatedArrayKeyRector\Fixture;

class SkipMethodAndFunctCalls
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
class SkipMethodAndFunctCalls
class SkipPropertyFetch


namespace Rector\Tests\DeadCode\Rector\Array_\RemoveDuplicatedArrayKeyRector\Fixture;

class SkipMethodAndFunctCalls
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
class SkipMethodAndFunctCalls
class SkipCallLikeKeys

@github-actions
Copy link
Contributor

This pull request has been automatically locked because it has been closed for 150 days. Please open a new PR if you want to continue the work.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 27, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants