PhpStorm Refactoring tutorial, part 3 – moving the class

Welcome in third part of my tutorial!

Today we are going to learn common thing while code refactor – class movement.

What I always did was to manually move the file in the Finder and then changed it’s namespace in the IDE. Finally I had to search whole project for class name etc. But PhpStorm can do all of that with one simple action.

I’m going to move the class \app\models\User to \common\models\User (Yii2 project).

Let me show you the beginning of my class:

<?php
namespace app\models;

use yii\base\Exception;
use yii\base\Model;
use yii\base\NotSupportedException;
use yii\web\IdentityInterface;

class User extends Model implements IdentityInterface
{

First thing what you need to do is to move the cursor into class name and open refactor menu (right click -> refactor, or press ctrl + t) and choose Move.

Fill the target namespace. I always check two checkboxes, Search in comments and string and Search for text occurrences. This will allows us to refactor the PHPDoccomments and other places where the class name exists.

Before pressing the Refactor button, I recommend to Preview the changes. All found places you will see on the list like this:

class move refactor

With second mouse button you can exclude results from refactoring. When you’re done, press Do Refactor. Now run your tests. Everything should work like as a charm!

Thank you for reading. Have a good day!

 

PhpStorm refactoring tutorial – part 2 – method extraction

Hello again.

This will be quick. Let’s take an example:

public function testAction($firstName, $lastName, $address)
{
    $data = ['firstName' => $firstName, 'lastName' => $lastName, 'address' => $address];
    $filteredData = [];
    foreach ($data as $key => $value) {
        $filteredData[$key] = str_replace('!@#$%^&*(', '', $value);
    }

    return $this->render('test', [
        'data' => $filteredData
    ]);
}

This method is doing too much. We want to extract new method which will filter the data and return the result. Please highlight the marek lines and press Refactor This button (ctrl + t). Choose Extract method. New window will appear. You can now customise your new method –  parameters order, visibility, PHPDoc and many many more. Im most cases you only need to add new method name and press enter – IDE fills all needed data for you. In this example I named new method filterData. Here’s the code after quick refactor:

public function testAction($firstName, $lastName, $address)
{
    $data = ['firstName' => $firstName, 'lastName' => $lastName, 'address' => $address];
    $filteredData = $this->filterData($data);

    return $this->render('test', [
        'data' => $filteredData
    ]);
}

/**
 * @param $data
 *
 * @return array
 */
private function filterData($data)
{
    $filteredData = [];
    foreach ($data as $key => $value) {
        $filteredData[$key] = str_replace('!@#$%^&*(', '', $value);
    }

    return $filteredData;
}

And that’s all :). Enjoy and tuned for next part. Cheers!

PhpStorm refactoring tutorial , part 1

PhpStorm like all JetBrains IDEs has o lot of built in features. And you can add even more with plugins. It provides many refactoring features.

This will be a first part of refactoring tutorial.

We will start from the simple things.

Lesson 1: Extracting a variable and renaming it.

Example code to be refactored:

'use strict';

$('#container').addClass('example');

$('#container').on('click', function () {
    alert('hello');
});

$('#container').remove();

This code is bad. If you use jQuery object more than once, assign it to variable.

First let’s extract variable. Move cursor to any occurrence of $(‘#container’) in the code and press ctrl+r. Menu will appear, now select Extract variable. IDE gives you ability to replace all occurrences or just the highlighted one. We want to replace all of them. Choose it. Now we have ability to name the variable. IDE suggest names by the code, in most cases it gives  correct name, but we can change it :).

Code after refactor:

'use strict';

var $container = $( '#container' );
$container.addClass('example');

$container.on('click', function () {
    alert('hello');
});

$container.remove();

Next thing. We have extracted the variable but later we decide that current name is not obvious. Naming is very important so we want to change it. Move cursor to any occurrence of variable $container and again press +r (on Mac) or ctrl+r (on other os). In the menu choose first position, Rename. Place a new name in the popup and do refactor.

'use strict';

var jQueryContainer = $( '#container' );
jQueryContainer.addClass('example');

jQueryContainer.on('click', function () {
    alert('hello');
});

jQueryContainer.remove();

You have ability to search the whole project for occurrences of the variable name in the string / comment and text . Try it by yourself, it’s very handy :).

That’s all for today. See you in the next part.