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

  8. Is there a framework?

    View Slide

  9. Is there a coding standard?

    View Slide

  10. No Standard? No problem!

    View Slide

  11. Is there any autoloading?

    View Slide

  12. There is a PSR for that…

    View Slide

  13. How are dependencies behind
    handled?

    View Slide

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

    View Slide

  15. Tools to help

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  20. Warning about Automating
    Code Changes

    View Slide

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

    View Slide

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

    View Slide

  23. “We should just rewrite the app”

    View Slide

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

    View Slide

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

    View Slide

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

    View 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 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 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 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 Slide

  31. Digging into the Code
    Code Consolidation

    View Slide

  32. Code Consolidation

    View Slide

  33. Code Consolidation

    View Slide

  34. _config_vars.php

    View Slide

  35. _db_connect.php

    View Slide

  36. composer require vlucas/phpdotenv

    View Slide

  37. Database Class

    View Slide

  38. Progress

    View Slide

  39. header.php

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

  44. _functions.php

    View Slide

  45. Weather Class

    View Slide

  46. Refactoring index.php

    View Slide

  47. header.php

    View Slide

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

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

  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)
    Extract Action Logic (To Controllers)
    Write (more) tests

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  58. Logging

    View Slide

  59. Logging

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  65. Routing

    View Slide

  66. Routing

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

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

    View Slide