Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Modernizing Legacy Code Step by Step

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!

Joe Ferguson

November 16, 2018
Tweet

More Decks by Joe Ferguson

Other Decks in Technology

Transcript

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

    View full-size slide

  2. Who Am I?
    Joe Ferguson
    PHP Developer
    PHP Architect @ Ministry Brands
    Twitter: @JoePFerguson
    OSMI Board Member
    Drone Racing Pilot

    View full-size slide

  3. For Further Reading
    leanpub.com/mlaphp
    leanpub.com/minimumviabletests

    View full-size slide

  4. Legacy Applications

    View full-size slide

  5. “There are no solutions,
    only trade offs”
    - Joe Ferguson
    - Paul M. Jones
    - Thomas Sowell

    View full-size slide

  6. Taking stock of what we’re
    working with

    View full-size slide

  7. 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?

    View full-size slide

  8. Is there a framework?

    View full-size slide

  9. Is there a framework?

    View full-size slide

  10. Is there a coding standard?

    View full-size slide

  11. Is there a coding standard?

    View full-size slide

  12. No Standard? No problem!

    View full-size slide

  13. Is there any autoloading?

    View full-size slide

  14. Is there any autoloading?

    View full-size slide

  15. There is a PSR for that…

    View full-size slide

  16. How are dependencies handled?

    View full-size slide

  17. Is there an ORM or how is the
    database being utilized?

    View full-size slide

  18. Tools to help

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  23. Warning about Automating
    Code Changes

    View full-size slide

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

    View full-size slide

  25. Triage
    Address any critical things found
    PHP Short Tags!
    PHP version issues
    Current server is 5.2 lets test 7.2
    Fixing critical vulnerabilities

    View full-size slide

  26. Get into Git!

    View full-size slide

  27. Get into Git!

    View full-size slide

  28. “We should just rewrite the app”

    View full-size slide

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

    View full-size slide

  30. Why not rewrite?
    Progress halts entirely
    Business logic is rarely reused
    Most of the time is getting back to
    current functionality

    View full-size slide

  31. Why you should refactor
    Progress continues
    Business logic reused
    Modernizing functionality instead
    of recreating

    View full-size slide

  32. Existing Tests
    This is great!
    Review the tests to get an idea of coverage
    Passing tests become your control for any upcoming changes
    Confidence++

    View full-size slide

  33. 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?

    View full-size slide

  34. 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

    View full-size slide

  35. 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

    View full-size slide

  36. Digging into the Code
    Code Consolidation

    View full-size slide

  37. Code Consolidation

    View full-size slide

  38. Code Consolidation

    View full-size slide

  39. _config_vars.php

    View full-size slide

  40. _db_connect.php

    View full-size slide

  41. composer require vlucas/phpdotenv

    View full-size slide

  42. Database Class

    View full-size slide

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

    View full-size slide

  44. Digging into the Code
    Code Consolidation
    Replace globals with Dependency Injection
    Write tests

    View full-size slide

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

    View full-size slide

  46. 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)

    View full-size slide

  47. _functions.php

    View full-size slide

  48. Weather Class

    View full-size slide

  49. Refactoring index.php

    View full-size slide

  50. 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)

    View full-size slide

  51. 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)

    View full-size slide

  52. 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

    View full-size slide

  53. 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

    View full-size slide

  54. Joe Ferguson
    Twitter: @JoePFerguson
    Email: [email protected]
    Freenode: joepferguson
    Contact Info:
    https://joind.in/talk/4e1b4

    View full-size slide