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
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:

  1. Place the cursor in class B
  2. Choose Refactor -> Push Members Down
  3. Tick all properties methods
  4. 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:

  1. Open the "Replace structurally..." dialog
  2. Select the built-in template under PHP -> General -> Class that extends another
  3. Edit the "Search template" to class $a$ extends \Full\NsName\B {}
  4. Edit the "Replace template" to class $a$ extends \Full\NsName\A {}
  5. Click "Find"
  6. 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