
A command line refactoring tool for PHP - beberlei
http://qafoo.com/blog/041_refactoring_browser.html?utm_source=hn&utm_medium=social&utm_campaign=refactoring
======
itry
Is this really needed? I get away with :%s/$abcd/$this->abcd/gc just fine.

When you leave vim and do this:

    
    
        $ php refactor.phar convert-local-to-instance-variable tests/FooServiceTest.php\
        6 dependency | patch -p1
    

I stay in vim and simply do this:

    
    
        :%s/$dependency/$this->dependency/gc
    

And hit y a couple of times.

Pros:

I dont need to install, trust and learn a new tool.

I dont have to worry if i broke somthing. Because of edge cases the tool did
not handle or because I messed up the syntax.

~~~
beberlei
You picked the simplest refactoring, for converting local to instance variable
I agree with you, this is easy in Vim.

But still there are some potential problems:

1\. The variable already exists in the class. 2\. You accidently mark to much
or to few lines with ":<,>%"

For refactorings such as extract method the number of preconditions and
information to gather is much bigger, such as input and output variables,
existance of return statement etc.

We plan to integrate the tool into vim as well, so that you can call the
refactorings through vim directly.

~~~
itry
I did not pick anything. It is your example. Right from your page. And the
only one you provide. If you have a better one: show us.

~~~
beberlei
The blog post shows extract method as well as a second refactoring

Edit: Also see the screenshot here for a more complex extract method
refactoring: <http://qafoolabs.github.io/php-refactoring-browser/>

~~~
itry
Ah, I missed the extract-method example.

Similarely to the convert-local-to-instance-variable example, to me it looks
like overkill. I never had a problem, doing these things manually. But if you
like doing it this way - I dont complain. To each his own.

~~~
jasonlotito
I never had a problem doing things manually either. However, as soon as I can
automate them, I do. If I can automate extract method to, in effect, a single
keystroke, why not?

It's the difference between using hjkl for movement, and ignoring all the
other commands vim as to offer. Why wouldn't want you to automate patterns in
your work?

------
binarymax
I have minimal exposure to PHP, but recently helped a friend out with a small
project on the weekends, that someone else he knew had built. When I first saw
the codebase I found a veritable mess of very unreadable unmaintainable code.
No functions - no OOP of any kind - no separation of concerns - nothing
reusable - SQL calls (probably vulnerable to injection) mixed with HTML - a
hulking mess of spaghetti.

Can tools like these fix code like that, or at least help? It would have taken
me about a day per page just to make it readable - and there were dozens of
pages. I don't plan on going back into that minefield if I can help it, but it
would be good to know in the future.

 _\--EDIT--_ Thanks for all the helpful replies. Luckily I'm pretty sure the
project is behind me. Seeing that PHP reminded me of old bad ASP (pre .NET) -
it was easy to get away with lots of really bad stuff (on error resume next
anyone?). It makes me wonder if auto-refactoring spaghetti is a crackable
problem. When I dared look I did see lots of things that I could have fixed
with a little bash and sed, but of course I am human so I understand the
'intent' of the code. Fascinating stuff.

~~~
mootothemax
_Can tools like these fix code like that, or at least help?_

If you're working with PHP professionally, invest in PHPStorm. The time
savings it brings - not to mention, the improved reliability - make it an
instant win.

~~~
pytrin
How is it better than Netbeans or Eclipse? I've been using mostly the former
(after Zend Studio went AWOL). How does PHPStorm compare to those
alternatives?

~~~
mootothemax
_How is it better than Netbeans or Eclipse?_

It's been a couple of years since I last looked at Netbeans/Eclipse, so take
this with a grain of salt: PHPStorm _just works_.

The biggest issues I used to have were finding Netbeans very slow, and auto-
complete in both Netbeans and Eclipse being somewhat unreliable - great when
they worked, but that wasn't 100% of the time.

Essentially, PHPStorm is what I always wanted Netbeans/Eclipse to be.

As I say, though, it's been a long time since I last looked, and that's for a
very good reason: at long last, I'm completely satisfied with my IDE.

~~~
pytrin
Thanks, checking out their free trial now

~~~
dmiladinov
PHPStorm is like the perfect gateway drug. I first became familiar with
JetBrains products through PHPStorm, but have since upgraded to IntelliJ IDEA.
IntelliJ IDEA is the perfect polyglot IDE, with support for the following
either in-built or via plugin (and forgive me if I leave anything out, or add
it in a reply):

\- Java

\- Scala

\- Clojure

\- Android

\- PHP

\- html/css/javascript

\- Python/Django

\- Ruby/Rails

\- Groovy/Grails

\- Actionscript/Flex

I'm a raving fan!

------
moron4hire
Yeah, but then you're still working in PHP.

I cannot state clearly enough, PHP will not help you. Neither in your project
or in your career. If you are doing PHP, do everything in your power to switch
to something else. There is no use case for PHP that is not better served by
something else.

------
Edd314159
Seems like a neat and very clever tool, but why would you use a test case as
an example? Maintainability in unit tests is nowhere near as important as with
actual implementation code (refactoring eventually may even _hinder_ the
documentary aspect of test code).

~~~
beberlei
I disagree heavily.

Refactoring tests is as important as refactoring "production" code. Following
the four elements of simple design ([http://www.jbrains.ca/permalink/the-four-
elements-of-simple-...](http://www.jbrains.ca/permalink/the-four-elements-of-
simple-design)) you can use test refactorings to find better names and detect
need for abstractions.

Tests with lots of duplication, for example in the test setup, get hard to
maintain and developers tend to abandon them more easily (my own experience).

------
Egregore
Can it be integrated into an ide? I like it a lot better when I point at
method or variable in IDE and can quickly rename it. BTW: there is already
such thing for PHP: PHPStorm, although not open source.

~~~
beberlei
I suppose it can be integrated into IDEs. We (the developers) are Vim users,
so there is no immediate need for us to integrate it into anything except Vim.

We know that PHP Storm has many refactorings implemented very well, but a
command line tool is much more flexible and useable for everyone, not just PHP
Storm users.

------
tillk
Looks neat. :)

It would be pretty nice (if I may suggest that ;)), if you added add branch
support so the patches are saved into a refactoring branch or maybe individual
branches?

