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

Break Off SunPHP20

Omni Adams
February 07, 2020

Break Off SunPHP20

Most of the PHP that still exists today was written long ago by developers lost to the sands of time. It was written in an era where we didn't have the tools we do today such as Composer, type safety, unit testing, and code standards. Even code that was written recently frequently ignores best practices in order to "get things done". You've been tasked with maintaining a legacy application and would like to modernize it, but the powers that be won't let you rewrite it. This talk will discuss strategies for bringing ancient codebases into the modern age piece-by-piece.

Omni Adams

February 07, 2020
Tweet

More Decks by Omni Adams

Other Decks in Programming

Transcript

  1. Break Off A Piece of That Monolith

    View full-size slide

  2. Break Off A Piece of That Mono
    Omni Adams — When I Work — Sunshine PHP 2020
    lith

    View full-size slide

  3. https://commons.wikimedia.org/wiki/File:Spaghetti,_cooked,_in_bowl.jpg

    View full-size slide

  4. https://www.flickr.com/photos/wagner-machado-carlos-lemes/2987599021

    View full-size slide

  5. https://www.flickr.com/photos/tambako/3589021887

    View full-size slide

  6. https://www.flickr.com/photos/nachof/2482912078

    View full-size slide

  7. https://www.pxfuel.com/en/free-photo-ozrrp

    View full-size slide

  8. https://www.geograph.org.uk/photo/5846712

    View full-size slide

  9. –DHH
    “You don’t want to hear that the reason your
    monolith is a spaghetti monster is because you let it
    become that way, one commit at the time, due to
    weak habits, pressurized deadlines, or simply sheer
    lack of competence.”
    https://m.signalvnoise.com/integrated-systems-for-integrated-programmers/

    View full-size slide

  10. It’s REALLY hard

    View full-size slide

  11. Plan of Attack
    Testing
    Tools
    Refactoring

    View full-size slide

  12. https://commons.wikimedia.org/wiki/File:PHPUnit_Logo.svg

    View full-size slide

  13. PHPUnit 8.5.0 by Sebastian Bergmann and contributors.
    ............................................................... 63 / 281 ( 22%)
    ............................................................... 126 / 281 ( 44%)
    ............................................................... 189 / 281 ( 67%)
    ............................................................... 252 / 281 ( 89%)
    ............................. 281 / 281 (100%)
    Time: 2.83 seconds, Memory: 24.00 MB
    OK (281 tests, 6 assertions)
    Code Coverage Report:
    Summary:
    Classes: 51.72% (15/29)
    Methods: 75.83% (91/120)
    Lines: 55.95% (941/1682)

    View full-size slide

  14. Testing
    Unit tests
    Functional tests

    View full-size slide

  15. Static Analysis
    PHP_CodeSniffer
    PHPStan

    View full-size slide

  16. Style Guides

    View full-size slide

  17. FILE: /home/omni/code/object/src/Nether/Object/Datastore.php
    ---------------------------------------------------------------------------------------------------------
    FOUND 891 ERRORS AND 2 WARNINGS AFFECTING 569 LINES
    ---------------------------------------------------------------------------------------------------------
    1 | ERROR | [x] End of line character is invalid; expected "\n" but found "\r\n"
    3 | ERROR | [x] Header blocks must be separated by a single blank line
    4 | ERROR | [ ] Import statements must not begin with a leading backslash
    4 | ERROR | [x] Header blocks must not contain blank lines
    6 | ERROR | [ ] Import statements must not begin with a leading backslash
    7 | ERROR | [ ] Import statements must not begin with a leading backslash
    8 | ERROR | [ ] Import statements must not begin with a leading backslash
    11 | ERROR | [x] The implements keyword must be on the same line as the class name
    11 | ERROR | [x] Opening brace of a class must be on the line after the definition
    13 | ERROR | [x] Spaces must be used to indent lines; tabs are not allowed
    13 | ERROR | [x] Scope keyword "protected" must be followed by a single space; found
    newline
    14 | ERROR | [x] Spaces must be used to indent lines; tabs are not allowed
    15 | ERROR | [x] Spaces must be used to indent lines; tabs are not allowed
    16 | ERROR | [x] Spaces must be used to indent lines; tabs are not allowed

    View full-size slide

  18. Static Analysis
    PHP_CodeSniffer
    PHPStan

    View full-size slide

  19. ------ ----------------------------------------------------------------------
    Line requisition.edit.php
    ------ ----------------------------------------------------------------------
    21 Class Error referenced with incorrect case: error.
    24 Instantiated class requisition not found.
    25 Call to method load_from_database() on an unknown class requisition.
    27 Class Error referenced with incorrect case: error.
    28 Constant ERR_RECORD_NOT_FOUND not found.
    29 Call to an undefined method Error::new_error().
    36 Constant APP_ROOT not found.
    199 Constant REQ_TERM_FULL_TIME not found.

    View full-size slide

  20. Refactoring
    Breaking up big pieces of code
    Pulling procedural code out to objects
    Pulling large pieces of functionality out

    View full-size slide

  21. Refactoring: Bigger is better?

    View full-size slide

  22. Refactoring: Small for the win?

    View full-size slide

  23. Refactoring: Just right

    View full-size slide

  24. The Strangler Pattern

    View full-size slide

  25. https://www.flickr.com/photos/vancouver-archives/8229393567

    View full-size slide

  26. Example: Calendar
    PHP3ish code
    Interspersed PHP and HTML
    No style guide
    No composer
    No tests

    View full-size slide

  27. /calendar.add.php —> {old crusty code}
    /calendar —> {new framework}

    View full-size slide

  28. Microservices

    View full-size slide

  29. /users
    /customers
    /sales
    /weather

    View full-size slide

  30. https://speakerdeck.com/omnicolor/break-off-sunphp20
    https://twitter.com/omnicolor
    Omni Adams
    [email protected]
    When I Work
    https://joind.in/talk/a79da

    View full-size slide