4.2. Refactoring - shinokada/php_notes GitHub Wiki

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.

Adding Laravel 5 IDE Helper Generator

barryvdh/laravel-ide-helper

This is an helper file for Laravel 5, to provide autocomplete information to your IDE.

Add a pre-generated file to the root folder of your Laravel project (but this isn't as up-to-date as self generated files).). Add this file name to .gitignore.

Generated version: https://gist.github.com/barryvdh/5227822

Multiple cursors

You can change the keymap for Select Next Occurrrence from ctrl + G to ctrl + D as SublimeText.(I'm not changing it.)

To select all occurences, ctrl + cmd + G on a word.

Spliting window

Create a keymap for Split Vertically ctrl + V and Split Horizontally ctrl + H.

cmd + W to close a window.

Vim in PhpStorm

In Preferences > plugins, click Browse Repositories and search vim and install IdeaVim.

After installation, change keymap opt + cmd + V to ctrl + cmd + V to enable vim. Or go to Tools > Vim Emulator to enable vim.

Create .ideavimrc file for PhpStorm vim.

xdebug

Install xdebug through homebrew or pecl.

// homebrew
brew search xdebug
brew install php55-xdebug
// pecl
pecl install xdebug
//php --ini // this does not work. This will give you a wrong php.ini file. 

Check phpinfo.php to find the Loaded Configuration File. My case is /etc/php.ini.

Add the following at the end of php.ini and restart the server sudo apachectl restart. And check the phpinfo.php again.

[xdebug]
zend_extension="/usr/local/Cellar/php55/5.5.18/lib/php/extensions/no-debug-non-zts-20121212/xdebug.so"
xdebug.remote_enable=1
xdebug.remote_host=localhost
xdebug.remote_port=9000
xdebug.remote_autostart=1
xdebug.profiler_enable=1
xdebug.profiler_output_dir="~/xdebug_profiler_output"
xdebug.idekey=PHPSTORM

Debugging with xdebug

Debugging

Setting up PhpStorm. Preferences > PHP > Interpreters. Find the location of php by which php. My case it returns, /usr/local/opt/php55/bin/php. Enter this in PHP executable. Xdebug should be in Debugger: if you have installed it.

Add a break point and Run > Debug or ctrl + D.

Go Run > Edit Configurations and add a PHP Script and add a name and file field.

Run > Debug > index.php (for example). Use Step into, Step over, Step out to debug step-by-step. Hovering a variable gives a value, and selectign an inequality sign and hovering it will gives true or false of the statement.

Use Variables screen and for particular variables you can use Watches screen on the right.

Add a condition by right click a breaking point. Such as $age == 15 etc.

Using xdebug in Laravel

Go to Run > Edit Configurations and add a PHP Web Application. Set up a Servers by clicking ... . Servers Name: Laravel, Host: localhost, Port: 8000, Debugger: Xdebug and Apply > OK. In Run/Debug Configurations, Name: Demo-Laravel, Start URL: /articles, Apply > OK. Add a break point and then Run > Debug Demo-Laravel and check a browser

Composer

From New project select Composer project from Project type. Find composer path in a terminal by which composer. Enter this to Use existing composer.phar field. Enter a name of packages like 'laravel/laravel' and select a version from Version to install.

To add a dependency, Tools > Composer > Add dependency. Add a shortcut for this. cmd + , , Keymap, find composer and init composer. Keymap can be ctrl + opt + cmd + C.

Automate namespace

Preferences > Directories, in laravel click app folder and click Mark as sources. This will add Source and folders app in the panel. Click Edit properties which is next to x in the panel. Add a namespace. Now create a foler under app and add a new PHP class by right click on the new folder. It will add the namespace in the field.

⚠️ **GitHub.com Fallback** ⚠️