views:

565

answers:

3

At present we have a rather large (4k+ loc) function that we wish to decompose in to separate functions.

Some of it has been decomposed where there are relatively encapsulated side effects and these ones are the easy bits.

However we now have ~3k loc of intertwined nastyness. Some vars are passed to external functions by ref and all sorts.

So, is anyone aware of any php refactoring tools that allow you to determine state modifications and side effects of a particular piece of code (including new references to variables which may be stored as members and modified later).

We have thus far being doing this by stepping into the called functions with xdebug but this is extremely intensive and some of the call stacks get quite deep.

If no-one knows of any libraries that do this as-is, can anyone think of a way of implementing this robustly? (Shy of adding features to xdebug which is beyond the scope of our project :'( )

EDIT: Also, I forgot to mention that this is legacy code so there are zero unit tests to verify behaviour.

A: 

You state that there are zero unit tests. This must be your first objective.

David Grant
That is our first objective. The current code is not unit-testable without writing unit tests for the 1357 methods / functions that get called (according to our internal static analysis tools which miss the dynamic calls)
Mike
Ouch. Good luck with that one.
David Grant
Not everything can be unit testd. Not everything *should* be unit tested. I really don't like these blanket "must unit test (everything is often implied)" statements. The real world often diverges (for good reason) from what might be theoretically nice.
cletus
@cletus: My statement wasn't that *everything* must be unit tested, just that the number of unit tests should be non-zero before attempting a refactoring.
David Grant
+4  A: 

Write some characterization tests before attempting anything. You could at least test the final output that goes in the browser. PHPUnit has means of testing HTML structures as well as SimpleTest. Additionally, PHPUnit has Selenium integration.

After having these tests in place every important newly extracted function/class/method should have its own unit tests.

My advise may not be the best depending on your situation. Ponder about it and see what's best.

Ionuț G. Stan
The code does not output any html however it does spit out an array.Thanks for the link to Characterization tests; I've not encountered them before but they look like they could be useful in this case.
Mike
A: 

There's a list of static analysis tools over here. Also, you may want to have a look at rephactor.

troelskn
Thanks. We already have an in house static analysis tool that can evaluate function / method calls and some of the dynamic stuffs but we need this to be runtime analysis of state before / after a given block more than static analysis.
Mike