views:

6111

answers:

13

Are there any tools which support refactoring PHP code (renaming variables, extracting methods, finding method references, ...)? Thank you.

A: 

PDT for Eclipse supports some basic refactoring (that I know of). You can rename classes and have them automatically renamed when they're referenced, and I think you can even move files and have their include/require references changed, too.

Brian Warshaw
+1  A: 

Maybe it's just because I'm still getting comfortable with the concepts of refactoring but I don't like having a tool do it for me.

I still prefer to do most refactoring manually because it forces me to slow down and really think about what I'm moving around and why.

Mark Biek
+2  A: 

The NetBeans IDE has some refactoring capability but it doesn't always seem to work. I am however using 6.1 with early PHP access. I expect 6.5 to have better refactoring capabilities with PHP.

Ross
I use Netbeans 6.5 and you can do things like rename methods but it doesn't fix references in other files, even in files in the same project, which makes it mostly useless.
cletus
I am using Netbeans 6.7, it kinda works...
Ronald Conco
+1  A: 

There is no refactoring support for PHP in Netbeans 6.5, its coming in future version though.

PDT eclipse plugin also does not support refactoring.

Aptana does not support refactoring either. I couldn't get goto definition to work either.

I end up using search and replace currently. I too would like at least rename function support.

grom
+4  A: 

rephactor is an automatic refactoring tool. It is still a bit incomplete, but it has a few basic refactorings.

Else, a good test suite is really the best tool for refactoring, you can have.

troelskn
THis uses regexes to find code. It simply isn't safe.
Ira Baxter
Automatic refactorings never are. You should have tests before and after to verify the operation. Rephactor actually integrates the flow of testing before/after.
troelskn
@troelskn: Never? Why should a refactoring tool be implemented in a way that damages your program? That just means you have a truly bad refactoring tool. It is *technically* straightforward to do most refactorings *correctly*. It is a lot of engineering sweat, and most people don't want to pay for it, and that explains why the stuff that is out there is generally pretty bad. But that doesn't mean refactoring tools *have* to be broken.
Ira Baxter
Any change to a code base runs the risk of introducing errors.
troelskn
@troelskn: Not generally. With dynamic languages, yes, as name-implementation binding is done at runtime.Static languages, like Haskell or C++, where the name-implementation binding is done at compile time, /before/ the code is run, can provide refactoring which is guaranteed successful. This is why dynamic languages, in general, suck.
Jesse
+2  A: 

Still my favorite refactoring tool is good ol' EditPlus in combination with Total Commander's super fast file search. (Hint: Alt+F7) I totally agree with Mark Biek's reply that you want to have full control over what is replaced where and when.

Whenever i have to do a some refactoring, i create a list in total commander of the files i want to alter by either searching with the built-in function or just by selecting them, and then i drag them all to EditPlus

Editplus can search & replace through the current file, a selection or all opened files using normal text matching, regex matching or multiline matching. It really gives you full control :)

Also, what comes in handy for code refactoring is EditPlus's Macro Recorder (Ctrl + Q to start recording macro 1-9 and Alt + 1-9 to playback a macro).

Once you get the hang of it, you know you can just search for a variable, ctrl + shift + (home || end || arrow keys) through your code, delete, copy/paste and use al that keyboard wizardry to like, for instance, convert a CSV file into a bunch of SQL queries within 30 seconds.

SchizoDuckie
This isn't refactoring. Its just editing.
Ira Baxter
refactoring is just automated editing.
quodlibetor
@quotlibetor: Writing Beethoven's Fifth is just editing by your definition. The difference between refactoring (moving of well defined code chunks in semantically legal ways) is so qualitatively different than moving arbitrary chunks of text with unknown meaning that the distinction between the two terms can't be erased.
Ira Baxter
A: 

As far as I know, the only IDEs that has any significant Refactoring support for PHP are Zend Studio from the makers of PHP and the Eclipse PHP plugin. Though the features are very basic. Nothing that I know of has the features of products like Resharper for C# etc.

orj
A: 

I read that the IDE Delphi for PHP will have refactoring capabilities in the release codenamed Crocodile, scheduled for early 2009. See this link for details.

Shunyata Kharg
+2  A: 

I personally prefer PHPEclipse IDE as my primary development tool. But I really miss basic "replace everywhere in the project" refactoring there.

But very simple find/sed command can solve it easily:

find . -type f -name "*Controller.php" -print0 \
         | xargs -0 sed -i 's/string1/string2/g'

So using this intelligently you can find references, renaming, removing and make any sort of manipulations with the source code.

BTW: you even can not remember all this parameters and attributes - just type it once and then call it through reverse-i-search.

Hope this helps.

Fedyashev Nikita
A: 

Zend Studio can Refactor a bit.....

matthy
+2  A: 

Scisr is a simple, standalone refactoring tool for PHP.

I know this is an old question, but since it's one of the best rundowns of PHP refactoring tools, I thought I would add my new project to the list.

A lot of my design goals sprung out of the inadequacies of other items mentioned here - they are tied into certain IDEs, or try to dictate your testing practices, or require a whole bunch of rote manual labor. I am hoping to create an overall better experience in Scisr. It's simple to install and run. It tries to be clever, but not too clever. It does the tasks that I have found to lend themselves best to automation. Enjoy!

IanGreenleaf
I have just read your project home page. I totally agree with the method behind the project and hope it does well. (I'm develop on a PC so unfortunately will have to wait until I move to Ubuntu before I can use it)
JW
Glad you like it! If you felt like doing a bit of hacking, it shouldn't be too hard to get running on a Windows environment. My guess would be the file paths would be the only real trouble, and a lot of that is handled by the code I cribbed from CodeSniffer, which I believe supports Windows.
IanGreenleaf
A: 

Try Web IDE from IntelliJ. It's in the EAP stage (alpha/beta) now, but I use it as the only IDE for several months, so it's stable and very good.

Nishi
A: 

Just published an article about the next release of Delphi for PHP where you can see the refactoring engine in action.

http://blogs.embarcadero.com/joseleon/2010/06/09/delphi-for-php-next-release-early-preview-15-refactoring/

qadram software