admin管理员组文章数量:1356831
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)?
Share Improve this question edited Mar 28 at 21:26 LazyOne 166k48 gold badges414 silver badges415 bronze badges asked Mar 28 at 15:04 ColinColin 2,2091 gold badge21 silver badges22 bronze badges 2- 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 Commented Mar 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 Commented Mar 28 at 18:28
1 Answer
Reset to default 2I 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.
本文标签: phpCan I refactor a child class to quotmerge inquot it39s parent class in PhpStormStack Overflow
版权声明:本文标题:php - Can I refactor a child class to "merge in" it's parent class in PhpStorm? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744029669a2578674.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论