Pull members up, push members down
The Pull Members Up refactoring allows you to move class members to a parent class. This may be helpful when you start to add functionality from the bottom of the class hierarchy and then realize that it can also be used in more common cases (otherwise some parts of your code may become obsolete for the parent class but still be valid for one of its children). With the Pull members up refactoring, you no longer need to manually copy a method or field from one class, paste it into another and fix internal references in the member.
The Push Members Down refactoring helps clean up the class hierarchy by moving class members to a subclass. The members are then relocated into the direct subclasses only.
Pull members up
Select the class to be moved to a parent class.
On the main menu or onthe context menu, choose Pull Members Up dialog appears.
. TheSelect the destination object (parent class).
In the Members section, select the members you want to move.
Click Refactor to pull the selected members to their destination.
Example
Suppose you have a class Car
which extends the parent class Vehicle
. Let's pull the printPassengers()
method and the $numOfPassengers
field from Car
to Vehicle
.
Place the caret inside the class
Car
and choose from the context menu.In the Pull Members Up Dialog that opens, select the checkboxes next to
printPassengers()
and$numOfPassengers
. PhpStorm informs you that the visibility of$numOfPassengers
will be changed from private to protected.Click Refactor.
After the refactoring, the classes will look as follows:
Push members down
In the editor, open the class whose members you need to push down.
On the main menu or onthe context menu, choose Push Members Down dialog displays the list of members to be pushed down.
.In the Members to be pushed down area, select the members you want to move. Note that the member at caret is already selected.
If pushing a member might cause problems, you will be notified with red highlighting. It means that, if the situation is unattended, an error will emerge after refactoring. PhpStorm prompts you with a Problems Detected dialog, where you can opt to ignore or fix the problem.
Preview and apply changes.
Example
Suppose you have a Vehicle
class, a Car
class that extends Vehicle
, and a Truck
class that also extends Vehicle
. Let's push the start()
method from the parent class Vehicle
to its child classes Car
and Truck
.
Place the caret inside the
Car
class and choose from the context menu.In the Push Members Down Dialog that opens, select the checkbox next to
start()
and click Refactor.
After the refactoring, the classes will look as follows: