Refactoring Legacy PHP for Serverless Microservices Part 1

Refactoring Legacy PHP for Serverless Microservices Part 1

AWS, Azure, GCC, Heroku and other PaaS, and IaaS cloud services providers have announced the General Availability of platform support for severless microservices. AWS Lambda services and Azure Functions offer ways to refactor traditional, monolithic legacy PHP code typically run in either a VM or a Container to serverless microservices.

Moving PHP Legacy Web Apps to Serverless

The easiest and least risky approach to move a PHP 5 web application to serverless cloud platforms is to use refactoring techniques. Some PHP web applications written with opinionated frameworks like CakePHP or Zend will have a more difficult time moving to serverless code architectures but those issues can be overcome by taking more time to do a top-down design review of PHP application and then tackling how to remove the "MVC framework" code paradigms step-by-step, module by module. It almost goes without saying, up-to-date, well-written code documentation and test harnesses can go a long way to making the "renovation" effort easier. If you happen to have already adopted PHP's object-oriented coding features in your PHP web application, you may find the MVC framework problems less of a challenge.

Refactoring, First Steps

The best way to refactor legacy PHP code is to understand refactoring should be done at the smallest scale. Resist the urge to improve the code will not be easy, but success depends on only making changes that are minimal, safe transformations of the legacy PHP code to keep its behavior unchanged. Initially, limit the scope of the refactoring to a single PHP module. Before making any code changes, if they don't already exist, write a set of PHPUnit tests for the module and confirm they work. This is why tests are important because refactoring may break the module and the tests will give you a trail of 'breadcrumbs' to find your way out of difficulty, especially if your PHP code lacks clear and accurate documentation.

Refactoring also does not involve rewriting or replacing large chunks of PHP code. Some modules may contain hundreds of lines of PHP code, As you make changes remember refactoring is a gradual, evolutionary process, designed to preserve the knowledge about the problem domain embedded in the existing code.

Create a Refactoring Workflow

As you refactor each PHP module, you should repeat a series of stages, each stage in your refactoring will comprise a series of steps. Creating a workflow for the refactoring will allow you to gain confidence and become more efficient. Keep in mind the refactoring steps are complete only when correct code behavior has been restored, proved by testing when you complete all of its steps.

The refactoring workflow, once you develop one, will make it safe to change your design later. In fact, the real benefits of refactoring code is doing so makes it more cost-effective, both in terms of the effort you have to make and the results you obtain. There is no single " best" way to refactor PHP code; no silver bullet. No matter what way you choose, its' best to keep the following 'general summary" in mind:

  • Refactored code is STABLE: passes all of the tests
  • Refactored code is "DRY" (Don't Repeat Yourself")
  • Refactored code is TRANSPARENT: easily understood: clearly communicates what it does and how
  • Refactored code is LEAN: no extraneous logic, variables, duplicated code, and concepts
  • Refactorted code is: COMPLETE: includes only what's needed and nothing else


To view or add a comment, sign in

Explore content categories