Skip to content

[Transform] Adds ArrayDimFetchToMethodCallRector rule#5723

Merged
TomasVotruba merged 2 commits intorectorphp:mainfrom
peterfox:feature/array-access-to-method
Mar 15, 2024
Merged

[Transform] Adds ArrayDimFetchToMethodCallRector rule#5723
TomasVotruba merged 2 commits intorectorphp:mainfrom
peterfox:feature/array-access-to-method

Conversation

@peterfox
Copy link
Copy Markdown
Contributor

Changes

  • Adds the new rule.
  • Adds a value object to be used as a configuration option for the rule.
  • Adds tests.
  • Updates the docs.

Why

In Laravel there's a few classes that use ArrayAccess to grab from the service container or config keys but it's not an overly popular way to do things anymore. This rule will be helpful to a few people I think who want to replace ArrayAccess implementing classes to use a specific method instead.

-$app['someService'];
+$app->make('someService');

@peterfox peterfox requested a review from staabm March 15, 2024 18:32
@peterfox peterfox changed the title Adds ArrayDimFetchToMethodCallRector rule [Transform] Adds ArrayDimFetchToMethodCallRector rule Mar 15, 2024
@TomasVotruba
Copy link
Copy Markdown
Member

Looks good 👍 Thanks 🥳

@TomasVotruba TomasVotruba merged commit cdde425 into rectorphp:main Mar 15, 2024
@peterfox peterfox deleted the feature/array-access-to-method branch March 15, 2024 19:38
}

foreach ($this->arrayDimFetchToMethodCalls as $arrayDimFetchToMethodCall) {
if (! $node->dim instanceof Node) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This check should be outside the loop, as return null early

}

if (! $this->isObjectType($node->var, new ObjectType($arrayDimFetchToMethodCall->getClass()))) {
return null;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this should continue to next loop of configurable config instead of return null

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.

4 participants