Refactoring

From HTML5 Builder
Jump to: navigation, search

HTML5 Builder includes refactoring features to clean-up your code. To access those features, either open the Refactor menu, or right-click on the Code Editor and select the Refactoring command.

General

Extract

Interface

To extract an interface from a class, structure, method, property, event or indexer, right-click on the Code Editor, and select Refactoring > Extract Interface….

On the dialog that will pop up, you must specify the following data:

  • The name of the interface you are creating. For example: MainClassInterface.
  • The namespace for the interface, that is, the file where the interface will be added (without extension). For example, mainForm would add the new interface to the mainForm.php script.
  • Mark on the list the parts of the element to be included on the resulting interface.

Method

To extract code from a method into another method, select the target lines of code, right-click on the Code Editor, and select Refactoring > Extract Method….

On the dialog that will pop up, you must specify the name of the new method. For example: performSubtask. The same dialog will display a preview of the resulting new method.

For example, if you had the following method:

function performLargeTask() {
  echo "First Line<br />\n";
  echo "Second Line<br />\n";
  echo "Third Line<br />\n";
  echo "Fourth Line<br />\n";
}

You could select the second and third lines, open the Extract Method dialog, and extract them on a method called print2And3. This would be the result:

function performLargeTask() {
  echo "First Line<br />\n";
  $this->print2And3();
  echo "Fourth Line<br />\n";
}

function print2And3() {
  echo "Second Line<br />\n";
  echo "Third Line<br />\n";
}

Superclass

To extract a superclass (parent class) from a class, interface, method, property, event, field or indexer, right-click on the Code Editor, and select Refactoring > Extract Superclass….

On the dialog that will pop up, you must specify the following data:

  • The name of the superclass you are creating. For example: SuperMainClass.
  • The namespace for the superclass, that is, the file where the superclass will be added (without extension). For example, mainForm would add the new interface to the mainForm.php script.
  • Mark on the list the parts of the element to be included on the resulting superclass.

Rename

To rename a variable, type, field, method, parameter or any other PHP element, select it on the Code Editor, right-click also on the Code Editor, and select Refactoring > Rename typeName”….

Note: type will be the type of element selected, and Name the name of the selected element.

On the dialog that will pop up, you must specify the following data:

  • The new name for the element. For example, if you are renaming $oldVariableName, the new name could be $newVariableName.
  • Whether you want to get a list of the references to the element before you actually apply the refactoring. If you leave View references before refactoring checked, you will get a list of the changes to be performed on the Refactorings widget, where you can then apply them, or not. If you unckeck it, the element will be renamed right away.

Functions

Parameters

To rework the parameters of a method, select the method on the Code Editor, right-click also on the Code Editor, and select Refactoring > Change Parameters….

The Change Parameters dialog will pop up. Use it to add new parameters, and rearrange or remove existing ones, and then click OK. An entry will be added to the Refactorings widget with the set of changes, so you can apply them from there.

Note: After you remove a parameter, you must manually delete it from any existing call to the method and from the definition fo the method if it contains references to it.

For new parameters, you can define the following properties:

  • Parameter Name. The name of the parameter. Remember PHP parameters, as other variables, are always prefixed with a $ sign. Example: $firstParameter.
  • Data Type. Type of data the parameter expects. You can specify either array or the name of a class (a list or available classes is displayed below the field). For additional information, check Type Hinting on PHP Documentation. Example: ComboBox.
  • Literal Value. The value the parameter should have in case it is omitted on the call to the function, that is, the default value of the parameter. Example: "defaultString".
  • Parameter Type. Whether the parameter should be passed by value or by reference. With the later, changes to the parameter from the function will affect the original variable passed on the call. For more information, check PHP Documentation on Passing by Reference.

Class Members

Properties

Declare

In PHP, you do not need to declare a class property before you actually use it. The following class is perfectly valid PHP code, and works as you would expect:

class SomeClass{
    function getProperty() { return $this->property; }  
    function setProperty($value) { $this->property = $value; }  
}

$this->property is the same property in both calls. That is, if you call setProperty(16);, getProperty() will return 16. And if you want to get the property initialized to some value when an object for the class is created, you do not have to declare the property either: you can initialize it in __construct() instead.

Nevertheless, it is a good practice to declare your class variables. It leads to a more readable code, makes it possible to write inline documentation for them, etc.

To add the declaration of a property you are using to your class, select the target property, right-click anywhere in the Code Editor, and go to Refactoring > Declare Field….

The Declare New Field dialog will pop up. Use it to select the visibility you want for the property, and click OK. The new declaration will be appended to the list of property declarations at the beginning of the class definition.

Pull Up

To move a class member to a parent class, select the member on the Code Editor, right-click also on the Code Editor and select Refactoring > Pull Members Up…. Follow the same process selecting a class instead of a member, and you will be able to choose the members you want to pull up.

The Pull Members Up dialog will pop up. Use it to select the members to be pulled up to a parent class, and the parent class they will be moved to. Then click OK. An entry will be added to the Refactorings widget with the set of changes, so you can apply them from there.

Push Down

To move a class member to a child class, select the member on the Code Editor, right-click also on the Code Editor and select Refactoring > Push Members Down…. Follow the same process selecting a class instead of a member, and you will be able to choose the members you want to push down.

The Push Members Down dialog will pop up. Use it to select the members to be pushed down to a child class, and the child class they will be moved to. Then click OK. An entry will be added to the Refactorings widget with the set of changes, so you can apply them from there.

Help Resources

Video Tutorials