Note: This software is under development and in alpha state. Refactorings do not contain all necessary pre-conditions and might mess up your code. Check the diffs carefully before applying the patches.
Automatic Refactorings for PHP Code by generating diffs that describe the refactorings steps. To prevent simple mistakes during refactorings, an automated tool is a great.
The library is standing on the shoulder of giants, using multiple existing libraries:
Based on data from these sources the Refactoring Browser consists of two distinct components:
Patchesallows to build patches based on change operations on a file.
Refactoringcontains the actual Refactoring domain and adapters to third party libraries.
Collectionsadds some collection semantics on top of PHP arrays. Currently contains a Set type.
The refactoring browser is used with:
php refactor.phar <refactoring> <arg1>...<argN>
It outputs a diff to the screen and you can apply it to your code by piping it to
php refactor.phar <refactoring> <arg1>...<argN> | patch -p1
Users of PHPStorm (or Netbeans) might wonder why this project exists, all the refactorings are available in this IDE. We feel there are several reasons to have such a tool in PHP natively:
Extract a range of lines into a new method and call this method from the original location:
php refactor.phar extract-method <file> <line-range> <new-method>
This refactoring automatically detects all necessary inputs and outputs from the function and generates the argument list and return statement accordingly.
Rename a local variable from one to another name:
php refactor.phar rename-local-variable <file> <line> <old-name> <new-name>
Converts a local variable into an instance variable, creates the property and renames all the occurrences in the selected method to use the instance variable:
php refactor.phar convert-local-to-instance-variable <file> <line> <variable>
Batch Operation to rename classes and namespaces by syncing class-names (IS-state) to filesystem names (SHOULD-state) based on the assumption of PSR-0.
Fix class and namespace names to correspond to the current filesystem layout, given that the project uses PSR-0. This means you can use this tool to rename classes and namespaces by renaming folders and files and then applying the command to fix class and namespaces.
php refactor.phar fix-class-names <dir>
Optimizes the use of Fully qualified names in a file so that FQN is imported with “use” at the top of the file and the FQN is replaced with its classname.
All other use statements will be untouched, only new ones will be added.
php refactor.phar optimize-use <file>
List of Refactorings to implement: