Chapter 8. Refactoring

The first question that you may be asking after reading the title of this chapter is, “What is refactoring?” Some people may have used refactoring previously under a different name such as projectwide renaming. If you have used this feature before in other development tools, you will be glad to see it in Zend Studio for Eclipse, and if you have never used it before, you will probably learn to love it.

The requirements for refactoring in Zend Studio for Eclipse specify several different ways that the feature works. Those requirements are

In the remainder of this chapter, we examine each of these requirements and give examples of how to use them in a project.

Rename Local Variables

At first, some of the requirements for renaming appear trivial. Renaming local variables seems as though it should be straightforward, and is for the most part, except for a few cases. Let’s start off with defining what we mean by “local” variables.

Local variables can be broken into two main subcategories:

  • Naïve—The variable is not preceded by the keyword global and is not a super-global, and there is not a require statement inside the function, preceding the variable usage.

  • Advanced—The variable is not preceded by the keyword global and is not a super-global, and there is a require statement inside the function, preceding the variable usage, but this require does not define a global variable with the same name.

Let’s look at some examples of local variable renaming. Listing 8.1 shows a simple example of how refactoring works. If you highlight the variable $a and then select Refactor, Rename from the menu at the top of the screen, or right-click on the code editor, you see the refactoring window, as shown in Figure 8.1.

Example 8.1. Rename All Occurrences of $a

<?php

function foo($b) {
    $a = 4;
    $a = $a + $b;
    return $a;
}

?>
Refactoring window for variable renaming.

Figure 8.1. Refactoring window for variable renaming.

This window allows you to choose a new name for the selected variable. It does not allow you to preview your changes or make the changes until you have typed in a new variable name. Clicking OK applies the refactoring to the file. If you want to see what the changes are going to be like before you actually commit the changes, use the Preview option, as shown in Figure 8.2. This option must also be used to update variable names that may be in comments.

Code and comment preview window for refactoring.

Figure 8.2. Code and comment preview window for refactoring.

From Figure 8.2, you can see that refactoring in this simple case worked as expected. All the variables named $a were renamed to $c.

For the next example of local variable renaming, local and global variables are named the same. Although naming local and global variables the same is never a good idea, in some cases it may be necessary. It also helps to show how refactoring in Zend Studio for Eclipse works. Listing 8.2 uses the example from Listing 8.1 with a twist. Adding a global line to the code will change the outcome of the refactoring.

Example 8.2. Rename All Local Occurrences of $a

<?php

function foo($b) {
    $a = 4;
    $a = $a + $b;
    global $a;
    return $a;
}

?>

Zend Studio for Eclipse knows the difference between local and global variables. If you highlighted the first $a in the function and renamed it to $c using refactoring, the resulting file would look like Listing 8.3. All occurrences of $a after the global statement are left unchanged.

Example 8.3. Result of Refactoring with Local and Global Variables

<?php

function foo($b) {
    $c = 4;
    $c = $c + $b;
    global $a;
    return $a;
}

?>

The examples that we have been looking at so far in this section have been naïve local variables. Advanced local variables are not that much more complicated than naïve variables but could yield surprising refactoring results if you were not aware of them.

Advanced local variable renaming takes care of special cases when files are included into code, and the included file(s) contains variables that are used in the parent file. Listing 8.4 shows two files with variable $a. In this case, all occurrences of the variable are renamed.

Example 8.4. Advanced Renaming of Local Variables

<?php

// file: foo.php

function foo() {
    $a = 4;
    require('bar.php'),
    return $a;
}

?>

<?php

// file: bar.php

$a = 7;

?>

Rename Global Variables

Renaming global variables is functionally similar to renaming local variables. However, it is good to make the logical distinction between the two types of variables because they may play a larger role in more complex code. Let’s look at one simple example and one more complex example of global variable renaming.

Listing 8.5 shows a few lines of code with a mix of local and global variables. Refactoring the first occurrence of $a and naming it $c changes the first, third, and fourth occurrence of $a. The second occurrence is skipped because it is a local variable of the function foo().

Example 8.5. Simple Renaming of Global Variables

<?php

$a = 4;
echo foo();

function foo() {
    $c = 4;
    global $a;
    return $a;
}

?>

Like advanced local variable renaming, advanced global variable renaming recursively renames global variables in included files. Listing 8.6 shows an example of recursive global variable naming.

Example 8.6. Advanced Recursive Renaming of Global Variables

<?php

// file: foo.php

$a = 4;
require('bar.php'),

?>

<?php

// file: bar.php

$a = 7;
require ('foobar.php'),

?>

<?php
// file: foobar.php

$a = 16;

?>

All occurrences of $a in all three files can be renamed to $c by changing the first variable in the file foo.php.

Rename Data Members

Zend Studio for Eclipse is designed to rename variables inside classes and methods. Class data members are usually defined at the top of a class and then accessed using $this->variable_name later in methods. With refactoring, renaming data members as shown in Listing 8.7 is an easy process.

Example 8.7. Renaming Data Members in a Class

<?php

class Foo {

    var $member_var;
    static $static_var;

    function bar() {

        echo $this->member_var;
        echo $this->static_var;
    }

}

?>

Both data members in the class Foo can be renamed using refactoring.

Rename Methods, Functions, and Classes

Method, function, and class names all have the possibility of changing over the development cycle of a project. As requirements evolve, it may make sense also to change function names so that they are more descriptive. Refactoring in Zend Studio for Eclipse makes these types of changes easy. Listing 8.8 shows all occurrences of a function renamed. Method and class renaming work in the same way.

Example 8.8. Renaming All Occurrences of a Function

<?php

// original file

function foo() {

    echo "hi";

}

foo();

?>

<?php

// refactored file

function foobar() {

    echo "hi";

}

foobar();

?>

Move Files and Folders

One of the most far-reaching changes that you can make to a program is a path change. Anyone who has had to make the same change in hundreds of files because the “globals” file has to be moved knows how frustrating path changes are. Let’s set up a simple example to demonstrate how refactoring handles the moving of files and folders.

Suppose you have a file, we’ll call it foo.php, that is calling a function in a file called lib/math_lib.php, as shown in Listing 8.9.

Example 8.9. Two Files to Demonstrate File Path Refactoring

<?php

// filename: foo.php

require_once('lib/math_lib.php'),
echo add(2,3);
?>

<?php

// filename: lib/math_lib.php

function add($num1, $num2) {
    return $num1 + $num2;
}
?>

Using the information from Listing 8.9, suppose that the project gets more complicated and math_lib.php is moved to its own directory where helper files will eventually exist. The first thing that you have to do is create the directory that the file will be moved to. To move the file to a new folder, right-click on the file that you want to move and select Refactor, Move. Next, choose the new location for the file from the window, as shown in Figure 8.3.

Moving a file to a new directory with refactoring.

Figure 8.3. Moving a file to a new directory with refactoring.

When you are moving files, Zend Studio for Eclipse forces you to approve the move by previewing the changes that will be made in all files. After you have had a chance to look at and approve the changes, click on OK in the preview window that is shown in Figure 8.4.

Previewing changes to code when moving files with refactoring.

Figure 8.4. Previewing changes to code when moving files with refactoring.

Files may have to be renamed over the course of a project. They can be renamed in the same way as they are moved. Simply select Refactor, Rename and type in a new name, and the changes are applied to all files that include that file.

Summary

Refactoring is a powerful tool. It will probably save you time at some point while you are working on a large project. Just remember that with refactoring, as with any large-scale search and replace, the results should be verified after the replacement has been done.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.77.32