Structural replace method parameter name
Hi there.
We have entity name with quite long name MyVeryLongEntityNameEntity
. This entity is used everywhere in quite large project.
There are thousands of methods that contains it as parameter like that:
public function name(
MyVeryLongEntityNameEntity $myVeryLongEntityNameEntity
): void;
private function xyz(
string $otherParam,
MyVeryLongEntityNameEntity $myVeryLongEntityNameEntity
): void;
public function __construct(
MyVeryLongEntityNameEntity $myVeryLongEntityNameEntity
): void;
public function __invoke(
MyVeryLongEntityNameEntity $myVeryLongEntityNameEntity
): void;
Now - we changed this entity name to something different, which was very easy task. MyVeryLongEntityNameEntity
to ShortEntity
PHPStorm handled it like nothing changing thousends of files.
But we ended up with ShortEntity $myVeryLongEntityNameEntity
everywhere.
Is there way to use Structural search & replace to do it? Change variable name from A to B for all methods/constructors in project that are of type ShortEntity
? I admit - i couldn't figure it out.
请先登录再写评论。
Hi,
I have also tried an SSR but was not able to find the needed pattern quickly. It is especially complicated when signatures have different number of parameters and the needed parameter position is floating. Also, even if it is possible to change its name in a signature, there are still usages that should be processed too, not sure how to cover it with SSR.
It may sound like a lame and even dangerous solution but what about simple “Replace in files” ($myVeryLongEntityNameEntity → $ShortEntity) if this variable name is unique?
By the way, there is a request to automatically rename variables when a class is renamed:
https://youtrack.jetbrains.com/issue/WI-60543/Refactor-variable-names-when-class-name-is-refactored
Rector PHP have RenameParamToMatchTypeRector but it is not configurable. It changes everything to match class type. This is IMO useless as changes things like
\DateTime $createdAt
. Seems we will have to do it manually. This will be nightmare.