• This repository has been archived on 12/Jul/2020
  • Stars
    star
    558
  • Rank 78,044 (Top 2 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 11 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A command line refactoring tool for PHP

PHP Refactoring Browser

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.

Build Status

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.

See a screenshot of extract-method in action.

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:

  • Patches allows to build patches based on change operations on a file.
  • Refactoring contains the actual Refactoring domain and adapters to third party libraries.
  • Collections adds some collection semantics on top of PHP arrays. Currently contains a Set type.

Install & Basic Usage

Download PHAR

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 patch -p1:

php refactor.phar <refactoring> <arg1>...<argN> | patch -p1

Editor Plugins

There are third-party plugins available for using PHP refactorings within different text editors. These separately maintained projects can be found at the links below:

Why?

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:

  • We are VIM users and don't want to use an IDE for refactorings. Also we are independent of an IDE and users of any (non PHP Storm) editor can now benefit from the practice of automated refactorings.
  • The non-existence of a simple refactoring tool leads to programmers not refactoring "just to be safe". This hurts long time maintainability of code. Refactoring is one of the most important steps during development and just come easy.
  • Generating patches for refactorings before applying them allows to easily verify the operation yourself or sending it to a colleague.
  • The libraries (see above) to build such a tool are available, so why not do it.
  • The project is an academic of sorts as well, as you can see in the Design Goals we try to be very strict about the Ports and Adapters architecture and a Domain Driven Design.

Refactorings

Extract Method

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 Local Variable

Rename a local variable from one to another name:

php refactor.phar rename-local-variable <file> <line> <old-name> <new-name>

Convert Local to Instance Variable

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>

Rename Class and Namespaces

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>

Optimize use statements

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>

Roadmap

Not prioritized.

Integration:

  • Vim Plugin to apply refactorings from within Vim.

List of Refactorings to implement:

  • Extract Method (Prototype Done)
  • Rename Local Variable (Prototype Done)
  • Optimize use statements (Done)
  • Convert Local Variable to Instance Variable (Prototype Done)
  • Rename Class PSR-0 aware (Done)
  • Rename Namespace PSR-0 aware (Done)
  • Convert Magic Value to Constant
  • Rename Method
    • Private Methods Only first
  • Rename Instance Variable
    • Private Variables Only First
  • Extract Interface

Internals

Design Goals

  • Be independent of third-party libraries and any Type Inference Engine (PDepend, PHP Analyzer) via Ports+Adapters
  • Apply Domain-Driven-Design and find suitable Bounded Contexts and Ubiquitous Language within them
  • Avoid primitive obsession by introducing value objects for useful constructs in the domain

More Repositories

1

QafooLabsNoFrameworkBundle

Bundle to use Symfony controllers without depending on the Framework. Moved to GyroProject MVC-Bundle
PHP
123
star
2

php-profiler-extension

Deprecated, see Tideways/php-profiler-extension
C
27
star
3

gilded-rose-kata

Gilded Rose Kata for PHP
PHP
20
star
4

emscripten-opengl-example

A simple example of using emscripten to transpile an OpenGL rendered Cube from C to JavaScript
14
star
5

xhprof-collector

Deprecated in favour of QafooLabs/Profiler
PHP
10
star
6

TestingKatas

Base directory for testing katas
Puppet
9
star
7

profiler

Moved to tideways/profiler
PHP
9
star
8

Daemon

Library to implement background daemons in PHP
PHP
7
star
9

Lounge

Simple example CouchApp
JavaScript
3
star
10

testing-setup

Qafoo base repository for testing workshops.
PHP
3
star
11

TimePlanner

TimePlanner
PHP
3
star
12

bepado-demo-shop

Demo Shop to demonstrate usage of the Bepado SDK
PHP
3
star
13

Stub

CouchApp stub
JavaScript
3
star
14

profiler-packages

Package definitions for Qafoo Profiler
3
star
15

ansible-example

Simple example for using Ansible (with Vagrant)
HTML
2
star
16

Blog

CouchApp Demo App
JavaScript
1
star
17

redis-testing

Ruby
1
star
18

CouchDBExamples

Simple PHP CouchDB examples
PHP
1
star
19

symfony2-workshop

PHP
1
star
20

NodeJsCouchTesting

JavaScript
1
star
21

programming-language-catalog

A simple demo application using AngularJS. This Application does not have a certain purpose. It is supposed to only demonstrate the features of AngularJS
JavaScript
1
star
22

pouchdemo

A very simple application to demo the pouchdb replication feature
JavaScript
1
star
23

ErrorHandler

Helper library with common error handling code.
PHP
1
star
24

fixtures

Examples for the "Getting Fixtures Right" talk
PHP
1
star