Skip to content
This repository was archived by the owner on Feb 8, 2024. It is now read-only.

4.2. Refactoring

Shinichi Okada edited this page May 6, 2015 · 4 revisions

Refactoring

Copy lines which you want to refactor. Refactor>Refactor this or ctrl + T. Select appropreate choice such as Method... or type met to filter. It will show a panel Extract Method. Select Visibility, Public, Protected or Private. Type a Name. It will create a method and replace the original with a new method name.

Renaming a method

ctrl + T and select Rename. It will show a panel at the bottom to show all occurences. Click Do Refactor to complete.

Moving a method to a base controller

Refactor to create a method. Move a cursor to the new method and ctrl + T, select Pull Members Up. And confirm Pull up members of ... to: BaseController. Then click Refactor to move the method to the parent controller.

Extract interface refactoring

  • Rename a class

ctrl + T > Rename. This will change the file name automatically as well.

  • Extract an interface, have the class implement that interface.

ctrl + T > Interface. This will display Extract Interface From Class ... Type Interface Nmae, Namespace and select Members to form interface. Click Refactor. This will add comments at the top of the file.

To modify the comments, cmd + , > file template > PHP Interface, delete #parse("PHP File Header.php").

<?php

#if (${NAMESPACE})
namespace ${NAMESPACE};
#end

interface ${NAME} {

}

PHPStorm implements the class with that interface.

Extract a variable

Move a cursor to a line or string, then ctrl + T > Variable..., select a variable from pop-up and type a Name in Introduce variable pop-up.

Inline a variable

Move a cursor to a string, ctrl + T > Inline...> Ok.

Automating __constructor

Note: Use Live Template

cmd + N > Constructor... > type the name of class Store in () and it will suggest classes. Give it a name.

function __construct(\Illuminate\Session\Store $session)

A cursor on $session and opt + enter to pop-up Initialize fields and Ok. It will add the following.

/**
* some doc block
* /
private $session;

..
{
    $this->session = $session;
}

A cursor on a class name \Illuminate\Session\Store, opt + enter and select Import class. This will add use statement at the top and replace the class and update doc block.

Auto Import

In preferences, search import and find Auto Import. Tick Enable auto-import in namespace scope.

cmd + N > Constructor, type Store in () and enter. It will add use statement.

Live template

shift shift > live template, add a new one. Abbreviation _c, Description: constructor. Template text:

public function __construct($ARGS$)
{
    $ENDS$
}

Applicable to PHP.

cmd + N > Constructor, type Store in () and enter. Escape first then type $session. cmd + enter select Initialize fields and click Ok.

Clone this wiki locally