Modernizing Legacy Code Step by Step

Fef6ec2170ad1ecfcacdf2ead305f040?s=47 Joe Ferguson
November 16, 2018

Modernizing Legacy Code Step by Step

We’ll be taking a real PHP 5.2 codebase and migrating it to the latest and greatest PHP version. We’ll cover refactoring, adding components to handle routing, database access, as well as converting our HTML output to views. We’ll step through the entire process to demonstrate a repeatable solution for modernizing your legacy application!

Fef6ec2170ad1ecfcacdf2ead305f040?s=128

Joe Ferguson

November 16, 2018
Tweet

Transcript

  1. Modernizing Legacy Code Step By Step Joe Ferguson November 15th

    2018
  2. Who Am I? Joe Ferguson PHP Developer PHP Architect @

    Ministry Brands Twitter: @JoePFerguson OSMI Board Member Drone Racing Pilot
  3. For Further Reading leanpub.com/mlaphp leanpub.com/minimumviabletests

  4. Legacy Applications

  5. “There are no solutions, only trade offs” - Joe Ferguson

    - Paul M. Jones - Thomas Sowell
  6. Taking stock of what we’re working with

  7. None
  8. None
  9. None
  10. Initial Project Overview Is there a framework? Is there a

    coding standard? Is there any autoloading? How are dependencies behind handled? Is there an ORM or how is the database being utilized? Is there a development environment?
  11. Is there a framework?

  12. Is there a framework?

  13. Is there a coding standard?

  14. None
  15. Is there a coding standard?

  16. No Standard? No problem!

  17. Is there any autoloading?

  18. None
  19. Is there any autoloading?

  20. There is a PSR for that…

  21. How are dependencies handled?

  22. None
  23. Is there an ORM or how is the database being

    utilized?
  24. None
  25. Tools to help

  26. PHP Coding Standards Fixer http://cs.sensiolabs.org/

  27. PHP Coding Standards Fixer php-cs-fixer fix app --dry-run

  28. PHP Mess Detector https://phpmd.org

  29. PHP Mess Detector https://phpmd.org $ phpmd app html cleancode --reportfile

    report.html
  30. Warning about Automating Code Changes

  31. Composer https://getcomposer.org/doc/01-basic-usage.md

  32. Triage Address any critical things found PHP Short Tags! PHP

    version issues Current server is 5.2 lets test 7.2 Fixing critical vulnerabilities
  33. Get into Git!

  34. Get into Git!

  35. “We should just rewrite the app”

  36. "This would be so much easier in framework _____________”

  37. Why not rewrite? Progress halts entirely Business logic is rarely

    reused Most of the time is getting back to current functionality
  38. Why you should refactor Progress continues Business logic reused Modernizing

    functionality instead of recreating
  39. Existing Tests This is great! Review the tests to get

    an idea of coverage Passing tests become your control for any upcoming changes Confidence++
  40. No Existing Tests Not the end of the world! Inspect

    the code base, was it written to be easily testable? Could you easily mock dependencies?
  41. Untestable Code Not all code is testable If you can’t

    easily unit test the code base, consider alternatives Functional testing and Acceptance testing are valid options
  42. Acceptance Testing NOT a replacement for Unit Testing Test large

    parts of your application at a time Can be harder to pinpoint error location Gives large code coverage in short amount of time
  43. None
  44. None
  45. None
  46. None
  47. None
  48. None
  49. None
  50. Digging into the Code Code Consolidation

  51. Code Consolidation

  52. Code Consolidation

  53. _config_vars.php

  54. _db_connect.php

  55. composer require vlucas/phpdotenv

  56. None
  57. Database Class

  58. Progress

  59. header.php

  60. Digging into the Code Code Consolidation Replace globals with DI

  61. None
  62. None
  63. None
  64. Digging into the Code Code Consolidation Replace globals with Dependency

    Injection Write tests
  65. None
  66. None
  67. None
  68. Digging into the Code Code Consolidation Replace globals with Dependency

    Injection Write tests Extract SQL (to ORM or other)
  69. Digging into the Code Code Consolidation Replace globals with Dependency

    Injection Write tests Extract SQL (to ORM or other) Extract Business Logic (To domain logic)
  70. None
  71. None
  72. _functions.php

  73. Weather Class

  74. Refactoring index.php

  75. header.php

  76. Digging into the Code Code Consolidation Replace globals with Dependency

    Injection Write tests Extract SQL (to ORM or other) Extract Business Logic (To domain logic) Extract Presentation Logic to Views (Twig, Blade, etc)
  77. Digging into the Code Code Consolidation Replace globals with Dependency

    Injection Write tests Extract SQL (to ORM or other) Extract Business Logic (To domain logic) Extract Presentation Logic to Views (Twig, Blade, etc) Extract Action Logic (To Controllers)
  78. Digging into the Code Code Consolidation Replace globals with Dependency

    Injection Write tests Extract SQL (to ORM or other) Extract Business Logic (To domain logic) Extract Presentation Logic to Views (Twig, Blade, etc) Extract Action Logic (To Controllers) Write (more) tests
  79. Resources / Q & A Modernizing Legacy Applications In PHP

    - Paul M. Jones https://leanpub.com/mlaphp Minimum Viable Tests - Chris Hartjes https://leanpub.com/minimumviabletests Code Style https://github.com/FriendsOfPHP/PHP-CS-Fixer, http://editorconfig.org php mess detector https://phpmd.org Autoloading PSR-4 https://getcomposer.org/doc/01-basic-usage.md ORMs http://www.doctrine-project.org http://propelorm.org Docker https://leanpub.com/dockerfordevs, Vagrant https://puphpet.com
  80. Joe Ferguson Twitter: @JoePFerguson Email: joe@joeferguson.me Freenode: joepferguson Contact Info:

    https://joind.in/talk/4e1b4