I'm looking for a way to automate the "merging" of two classes in PhpStorm, essentially removing a parent class from a grandparent->parent->child chain.
For example, here's my initial set of 3 classes:
// the grandparent class
class A {
// ...
}
// my parent class
class B extends A {
public function foo()
{
// ...
}
public function getAge(string $name): int {
return 10;
}
}
// my child class
class C extends B {
public function getAge(string $name): int {
if ($name === 'Bob') {
return 99;
}
return parent::getAge($name);
}
public function bar()
{
// ...
}
}
And here is my desired result, eliminating the B class
// the grandparent class remains unchanged
class A {
// ...
}
// the parent B class is eliminated
// child class extends the "grandparent" now
class C extends A {
// method pushed down from parent to child
public function foo()
{
// ...
}
// method kept as-is in child
public function bar()
{
// ...
}
// merge of child and parent methods
public function getAge(string $name): int {
// logic as-is from child
if ($name === 'Bob') {
return 99;
}
// parent::getAge() replaced with actual logic from parent
return 10;
}
}
Is there a way to do this refactor automatically in PhpStorm (either natively, or via a plugin)?
I'm looking for a way to automate the "merging" of two classes in PhpStorm, essentially removing a parent class from a grandparent->parent->child chain.
For example, here's my initial set of 3 classes:
// the grandparent class
class A {
// ...
}
// my parent class
class B extends A {
public function foo()
{
// ...
}
public function getAge(string $name): int {
return 10;
}
}
// my child class
class C extends B {
public function getAge(string $name): int {
if ($name === 'Bob') {
return 99;
}
return parent::getAge($name);
}
public function bar()
{
// ...
}
}
And here is my desired result, eliminating the B class
// the grandparent class remains unchanged
class A {
// ...
}
// the parent B class is eliminated
// child class extends the "grandparent" now
class C extends A {
// method pushed down from parent to child
public function foo()
{
// ...
}
// method kept as-is in child
public function bar()
{
// ...
}
// merge of child and parent methods
public function getAge(string $name): int {
// logic as-is from child
if ($name === 'Bob') {
return 99;
}
// parent::getAge() replaced with actual logic from parent
return 10;
}
}
Is there a way to do this refactor automatically in PhpStorm (either natively, or via a plugin)?
php
phpstorm
jetbrains-ide
Share
Improve this question
edited Mar 28 at 21:26
LazyOne
166k4848 gold badges414414 silver badges415415 bronze badges
asked Mar 28 at 15:04
ColinColin2,20911 gold badge2121 silver badges2222 bronze badges2
There are lots of possible complications with this. Easy to solve ones: What if class B is used somewhere? But also more difficult to solve ones: What if class B overwrites a method of class C? You will either loose access to the method in B or C. There are probably many more problems like this.
– KIKO Software
CommentedMar 28 at 16:01
If class B is used somewhere, I'd want to refactor it to use class C. B shouldn't overwrite a method of C (since C is the child to B).
– Colin
CommentedMar 28 at 18:28
Add a comment
|
1 Answer
1
Reset to default
2
I don't think there is a single refactoring available for this out of the box, but there are a number of tools to automate it in steps.
Use "Inline" refactoring to eliminate the direct parent:: calls.
(I hoped that Structural Search would be able to identify the relevant calls, but trying it out locally I could only get a list of classes, not the lines where the call happens. It's possible I just didn't get the right settings.)
Move the properties and methods using the "pull members up" and "push members down" refactorings:
Place the cursor in class B
Choose Refactor -> Push Members Down
Tick all properties methods
Click "Refactor"
Note that if there is more than one child class of B, this will create copies of the methods in all of them. Alternatively, you can use "Pull Members Up" to move them to class A.
Since you selected all properties and methods, class B will now be empty.
Use structural find and replace to make all classes inheriting from B inherit from A instead:
Open the "Replace structurally..." dialog
Select the built-in template under PHP -> General -> Class that extends another
Edit the "Search template" to class $a$ extends \Full\NsName\B {}
Edit the "Replace template" to class $a$ extends \Full\NsName\A {}
Click "Find"
Review the results and click "Replace Selected" or "Replace All" if you're happy
Use Find Usages to find any other references to class B, and replace with A or C as required.
Finally, use Safe Delete to have PhpStorm perform one last check for usages before deleting the class completely.