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 Slide

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

    View Slide

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

    View Slide

  4. Legacy Applications

    View Slide

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

    View Slide

  6. Taking stock of what we’re
    working with

    View Slide

  7. View Slide

  8. View Slide

  9. View Slide

  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?

    View Slide

  11. Is there a framework?

    View Slide

  12. Is there a framework?

    View Slide

  13. Is there a coding standard?

    View Slide

  14. View Slide

  15. Is there a coding standard?

    View Slide

  16. No Standard? No problem!

    View Slide

  17. Is there any autoloading?

    View Slide

  18. View Slide

  19. Is there any autoloading?

    View Slide

  20. There is a PSR for that…

    View Slide

  21. How are dependencies handled?

    View Slide

  22. View Slide

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

    View Slide

  24. View Slide

  25. Tools to help

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  30. Warning about Automating
    Code Changes

    View Slide

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

    View Slide

  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

    View Slide

  33. Get into Git!

    View Slide

  34. Get into Git!

    View Slide

  35. “We should just rewrite the app”

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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++

    View Slide

  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?

    View Slide

  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

    View Slide

  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

    View Slide

  43. View Slide

  44. View Slide

  45. View Slide

  46. View Slide

  47. View Slide

  48. View Slide

  49. View Slide

  50. Digging into the Code
    Code Consolidation

    View Slide

  51. Code Consolidation

    View Slide

  52. Code Consolidation

    View Slide

  53. _config_vars.php

    View Slide

  54. _db_connect.php

    View Slide

  55. composer require vlucas/phpdotenv

    View Slide

  56. View Slide

  57. Database Class

    View Slide

  58. Progress

    View Slide

  59. header.php

    View Slide

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

    View Slide

  61. View Slide

  62. View Slide

  63. View Slide

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

    View Slide

  65. View Slide

  66. View Slide

  67. View Slide

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

    View Slide

  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)

    View Slide

  70. View Slide

  71. View Slide

  72. _functions.php

    View Slide

  73. Weather Class

    View Slide

  74. Refactoring index.php

    View Slide

  75. header.php

    View Slide

  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)

    View Slide

  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)

    View Slide

  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

    View Slide

  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

    View Slide

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

    View Slide