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

Dragging Old Frameworks Into the Future With Limited Kicking and Screaming

Dragging Old Frameworks Into the Future With Limited Kicking and Screaming

Joe's career has been filled with legacy projects he's been tasked with bringing into the modern PHP world. He'll share experiences specifically with refactoring a Kohana framework project and the steps his team is taking to modernize the code base while introducing new features and continuing to deliver releases for their customers. Join us as we learn about dragging old legacy projects into the future of modern PHP development without much kicking and screaming.

Joe Ferguson

May 31, 2018
Tweet

More Decks by Joe Ferguson

Other Decks in Programming

Transcript

  1. Dragging Old Frameworks Into
    the Future With Limited Kicking
    and Screaming
    Joe Ferguson
    May 31st 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 coding standard?

    View full-size slide

  10. No Standard? No problem!

    View full-size slide

  11. Is there any autoloading?

    View full-size slide

  12. There is a PSR for that…

    View full-size slide

  13. How are dependencies behind
    handled?

    View full-size slide

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

    View full-size slide

  15. Tools to help

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  20. Warning about Automating
    Code Changes

    View full-size slide

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

    View full-size slide

  22. Triage
    Address any critical things found
    PHP version issues
    Fixing critical vulnerabilities

    View full-size slide

  23. “We should just rewrite the app”

    View full-size slide

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

    View full-size slide

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

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

    View full-size slide

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

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

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

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

  31. Digging into the Code
    Code Consolidation

    View full-size slide

  32. Code Consolidation

    View full-size slide

  33. Code Consolidation

    View full-size slide

  34. _config_vars.php

    View full-size slide

  35. _db_connect.php

    View full-size slide

  36. composer require vlucas/phpdotenv

    View full-size slide

  37. Database Class

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

  42. _functions.php

    View full-size slide

  43. Weather Class

    View full-size slide

  44. Refactoring index.php

    View full-size slide

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

  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)
    Extract Presentation Logic to Views (Twig, Blade, etc)
    Extract Action Logic (To Controllers)

    View full-size slide

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

  48. Configuration
    https://github.com/mattstauffer/Torch/tree/master/components/config

    View full-size slide

  49. Configuration
    https://github.com/mattstauffer/Torch/tree/master/components/config

    View full-size slide

  50. Configuration
    https://github.com/mattstauffer/Torch/tree/master/components/config

    View full-size slide

  51. Configuration
    https://github.com/mattstauffer/Torch/tree/master/components/config

    View full-size slide

  52. Configuration
    https://github.com/mattstauffer/Torch/tree/master/components/config

    View full-size slide

  53. Logging
    https://github.com/mattstauffer/Torch/tree/master/components/log

    View full-size slide

  54. Logging
    https://github.com/mattstauffer/Torch/tree/master/components/log

    View full-size slide

  55. You’ll likely want to configure
    your logger into a base class
    that is extended

    View full-size slide

  56. Routing
    https://github.com/mattstauffer/Torch/tree/master/components/routing

    View full-size slide

  57. Routing
    https://github.com/mattstauffer/Torch/tree/master/components/routing

    View full-size slide

  58. Routing
    https://github.com/mattstauffer/Torch/tree/master/components/routing

    View full-size slide

  59. Routing
    https://github.com/mattstauffer/Torch/tree/master/components/routing

    View full-size slide

  60. Database
    https://github.com/mattstauffer/Torch/tree/master/components/database

    View full-size slide

  61. Database
    https://github.com/mattstauffer/Torch/tree/master/components/database

    View full-size slide

  62. Database
    https://github.com/mattstauffer/Torch/tree/master/components/database

    View full-size slide

  63. Database
    https://github.com/mattstauffer/Torch/tree/master/components/database

    View full-size slide

  64. Container
    https://github.com/mattstauffer/Torch/tree/master/components/container

    View full-size slide

  65. Container
    https://github.com/mattstauffer/Torch/tree/master/components/container

    View full-size slide

  66. Container
    https://github.com/mattstauffer/Torch/tree/master/components/container

    View full-size slide

  67. Container
    https://github.com/mattstauffer/Torch/tree/master/components/container

    View full-size slide

  68. Container
    https://github.com/mattstauffer/Torch/tree/master/components/container

    View full-size slide

  69. Container
    https://github.com/mattstauffer/Torch/tree/master/components/container

    View full-size slide

  70. Containers are very powerful.
    Make sure you’re not adding
    spaghetti code just to shoehorn
    into your App

    View full-size slide

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

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

    View full-size slide