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Ā PHPDoc,Ā comments 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:
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!

