WIP: Refactoring to use class-orientated structure (Proof of Concept)
We seemed to be quite stuck with how to proceed last time we chatted, so I looked into an approach for doing incremental improvements.
I bought this book https://leanpub.com/mlaphp.
One of his pieces of wisdom in the opening section is:
I sometimes hear about how developers wisely wish to avoid a complete rewrite and instead want to “refactor” or “migrate” to a public framework. This sounds like the best of both worlds, combining an iterative approach with the developers’ desire to use the hottest new technology.
My experience with legacy PHP applications has been that they are almost as resistant to framework integration as they are to unit testing. If the application was already in a state where its logic could be ported to a framework, there would be little need to port it in the first place.
However, by the time we have completed the refactorings in this book, the application is very likely to be in a state that will be much more amenable to a public framework migration. Whether the developers will still want to do so is another matter.
This seems to match up to our discussions regarding laravel.
It contains a very clear set of steps to take to refactor PHP applications, the chapter headings are:
- Implement An Autoloader
- Consolidate Classes and Functions
- Replace global With Dependency Injection
- Replace new With Dependency Injection
- Write Tests
- Extract SQL Statements To Gateways
- Extract Domain Logic To Transactions
- Extract Presentation Logic To View Files
- Extract Action Logic To Controllers
- Replace Includes In Classes
- Separate Public And Non-Public Resources
- Decouple URL Paths From File Paths
- Remove Repeated Logic In Page Scripts
- Add A Dependency Injection Container
So, this branch is implementing some of the first and second chapters. I have only converted the homepage to use this style at the moment, just to show the concept.
This feels like a very promising approach to me.
A few points/comments:
- I think I moved it to the more modern PSR-4 autoloading (not PSR-0 as recommended by the book for legacy apps)
- I also added namespaces as it seemed to be easy enough
- phpstorm is very useful (Inspect Code can find all the invalid functions calls after moving around, and much more)
- the book feels like having someone who has done this before holding my hand
- getting into a certain structure makes it easier to use other fancy phpstorm features, like refactoring
- I feel motivated to do some of this
- I am still not sure what the bigger vision is, but this feels like it gets closer to somewhere good
I probably will not do anything more on this before we discuss it together.