Refactoring PHP

Discussion in 'Web Design and Development' started by thejadedmonkey, Aug 15, 2014.

  1. thejadedmonkey macrumors 604

    thejadedmonkey

    Joined:
    May 28, 2005
    Location:
    Pa
    #1
    This quote in another thread got me thinking. What's the best way to refactor PHP? It's near impossible to detect errors from an IDE, as PHP isn't strongly typed, and returning an integer is just as valid as a string or an object.

    How do you do it?
     
  2. Jamesbot macrumors member

    Joined:
    Jun 19, 2009
    #2
    Having a good suite of automated tests is probably the most helpful thing, in my opinion.

    If you have no test coverage, I'd recommend getting in the habit of writing some high level tests using something like selenium or phantomjs. So when you do a refactoring, start by testing the existing functionality. Run your tests after each change to make sure you didn't break anything.

    Often you'll break some other part of your app with your refactoring. When that happens, write a test for that part too. Then fix the regression. Little by little, you'll develop a suite of tests that act as a safety net for your refactorings.
     
  3. 960design macrumors 68020

    Joined:
    Apr 17, 2012
    Location:
    Destin, FL
    #3
    Typically I go in with a plan for the original code structure. As stated, that plan evolves over time where I end up needing to decouple functions into full classes, for example. Other things I do is clean up the constructors and try to make them all standardized.

    I don't refactor code for the fun of it. If it works then let it lie, learn your lesson and move on. We are all continually refining our skills. Coding hasn't been around very long ( compared to farming, medicine, law, barber shop quartets ) and we are all still learning what works best.
     

Share This Page