PhpStorm 2018.1 Help

Pull Members Up, Push Members Down

Basics

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.

Pulling members up

  1. Select the class to be moved to a parent class.
  2. On the main menu or on the context menu, choose Refactor | Pull Members Up. The Pull Members Up dialog box appears.
  3. Select the destination object (parent class).
  4. In the Members section, select the members you want to move.
  5. Click Refactor to pull the selected members to their destination.

PHP example

Suppose you have a class Car which extends the parent class Vehicle:

abstract class Vehicle { } class Car extends Vehicle { protected $weight; private $numOfPassengers; function __construct($weight,$numOfPassengers) { $this->weight = $weight; $this->numOfPassengers = $numOfPassengers; } protected function printWeight() { echo 'Weight = ' . $this->weight; } protected function printPassengers() { echo 'Number of passengers = ' . $this->numOfPassengers; } }
Let's pull the method printPassengers() and the field $numOfPassengers from Car to Vehicle.

  1. Place the caret inside the class Car and choose Refactor } Pull Members Up... on the context menu.
  2. 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.
    ps_pull_members_dialog_php_example.png
  3. Click Refactor. After the refactoring your classes will look as follows:
    abstract class Vehicle { protected $numOfPassengers; protected function printPassengers() { echo 'Number of passengers = ' . $this->numOfPassengers; } } class Car extends Vehicle { protected $weight; function __construct($weight,$numOfPassengers) { $this->weight = $weight; $this->numOfPassengers = $numOfPassengers; } protected function printWeight() { echo 'Weight = ' . $this->weight; } }

Pushing members down

  1. In the editor, open the class whose members you need to push down.
  2. On the main menu or on the context menu, choose Refactor | Push Members Down. Push Members Down dialog box displays the list of members to be pushed down.
  3. 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.

  4. Preview and apply changes.

PHP example

Suppose you have a class Vehicle, a class Car that extends Vehicle, and a class Truck that lso extends Vehicle:

abstract class Vehicle { protected $code; public $name; protected function start() { echo "Let's start!"; } public function printName() { echo "Name = " . $this->name; } } class Car extends Vehicle { protected $weight; function __construct($weight) { $this->weight = $weight; } } class Truck extends Vehicle { protected $length; function __construct($length) { $this->length = $length; } }
Let's push the method start() from the parent class Vehicle to its child classes Car and Truck.

  1. Place the caret inside the class Car and choose Refactor } Push Members Down... on the context menu.
  2. In the Push Members Down Dialog that opens, select the checkbox next to start() and click Refactor. After the refactoring your classes will look as follows:
    abstract class Vehicle { protected $code; public $name; public function printName() { echo "Name = " . $this->name; } } class Car extends Vehicle { protected $weight; function __construct($weight) { $this->weight = $weight; } protected function start() { echo "Let's start!"; } } class Truck extends Vehicle { protected $length; function __construct($length) { $this->length = $length; } protected function start() { echo "Let's start!"; } }
Last modified: 27 July 2018

See Also