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

Bringing Old Legacy Apps To PHP 7 and Beyond - Midwest PHP 2017

Bringing Old Legacy Apps To PHP 7 and Beyond - Midwest PHP 2017

Most developers don't get the luxury of working on a greenfield project using a web framework of their choice. Often times we find ourselves inheriting a legacy codebase written years ago for an end-of-life version of PHP and a now-defunct web framework. The initial temptation might be, "I'll just rewrite all this on x framework". But hold on there my framework friend, a rewrite is costly and is rarely the correct answer.

We'll discuss some common pitfalls when getting a legacy codebase ready for PHP 7 and what we can do to make the upgrade path as painless as possible. These incremental changes will keep the business running while improving security, stability and maintainability of the codebase.

Sammy Kaye Powers

March 17, 2017
Tweet

More Decks by Sammy Kaye Powers

Other Decks in Programming

Transcript

  1. Bringing Old Legacy Apps To
    March 17th, 2017
    Sammy Kaye Powers
    @SammyK
    PHP 7 & Beyond

    View full-size slide

  2. Codebase
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  3. I’ll just rewrite
    this in
    ____________!
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  4. I’ll just rewrite
    this in
    ____________!
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  5. I’ll just rewrite
    this in
    ____________!
    Laravel
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  6. I’ll just rewrite
    this in
    ____________!
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  7. rewrite from
    scratch?
    Why not
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  8. Functionality
    Existing Codebase Fancy-pants rewrite

    View full-size slide

  9. Functionality
    Existing Codebase Fancy-pants rewrite

    View full-size slide

  10. Functionality
    Existing Codebase Fancy-pants rewrite

    View full-size slide

  11. Functionality
    Existing Codebase Fancy-pants rewrite

    View full-size slide

  12. Functionality
    Existing Codebase Fancy-pants rewrite

    View full-size slide

  13. Refactoring
    Martin Fowler
    http://martinfowler.com/books/refactoring.html

    View full-size slide

  14. “Legacy”
    /leɡəsē/
    • Requires an EOL version of PHP
    • No automated tests
    • Has outdated dependencies
    • No autoloading
    • No sign of “single responsibility”

    View full-size slide

  15. “Legacy”
    We’ll assume
    …means any code that
    can be improved
    (at least for this talk)
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  16. Expectations
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  17. elePHPant
    How to eat an

    View full-size slide

  18. ZERO
    Step
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  19. Around
    Poke
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  20. Around
    Poke
    • Composer &/or autoloading?
    • Documentation? (Look for hidden docs!)
    • Framework/Dependencies? (How out-dated?)
    • How is database layer implemented?
    • How is config handled?
    • How are front-end assets handled?
    • Are there tests?
    • Production env (PHP version)

    View full-size slide

  21. Analysis
    Static
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  22. Consider the
    Stakeholders
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  23. Consider the Stakeholders
    I’ll need 80
    hours to
    refactor the
    user auth & ACL
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  24. Consider the Stakeholders
    • Most critical security holes
    • Most critical bugs
    • Most critical features
    • Use data from static analysis
    Make a plan

    View full-size slide

  25. ONE
    Step
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  26. 5.(wat)
    @SammyK #mwphp17 joind.in/talk/82979
    Get it running on

    View full-size slide

  27. Environment
    Get it running on your local
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  28. Kickass
    Development
    Environments
    with Docker
    David McKay
    Tomorrow @ 11 AM in Ballroom D
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  29. Git with an
    SCM

    View full-size slide

  30. Git
    Get with
    master
    original
    local-config

    View full-size slide

  31. Fix all the errors
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  32. TWO
    Step
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  33. 7.1
    @SammyK #mwphp17 joind.in/talk/82979
    Get it running on

    View full-size slide

  34. 7.1
    ✓Performance
    increase
    PHP 7
    features

    View full-size slide

  35. BC
    is totes amaze

    View full-size slide

  36. PHP 7 enlightenment
    The path to
    7.1
    5.6

    View full-size slide

  37. 7.1
    5.5
    PHP 7 enlightenment
    The path to

    View full-size slide

  38. 7.1
    5.4
    PHP 7 enlightenment
    The path to

    View full-size slide

  39. 7.1
    >= 5.3
    PHP 7 enlightenment
    The path to

    View full-size slide

  40. THREE
    Step
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  41. Fix all the PHP 7 errors
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  42. PHP 7
    Quick tricks to refactor to
    (in order of ease)
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  43. PHP 7.0 “Gotchas”
    ASP and
    PHP<br/>tags removed<br/>@SammyK #mwphp17 joind.in/talk/82979<br/>

    View full-size slide

  44. <% %>
    <%= %>
    <br/>
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  45. PHP 7.0 “Gotchas”
    removed
    $HTTP_RAW_POST_DATA
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  46. php://input
    instead read from
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  47. PHP 7.0 “Gotchas”
    PHP-4-style
    constructors
    deprecated
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  48. PHP 7.0 “Gotchas”
    Some
    functions
    got the ax
    ereg*
    functions

    View full-size slide

  49. PHP 7.0 “Gotchas”
    “There’s a shim for that!”

    View full-size slide

  50. PHP 7.0 “Gotchas”
    “There’s a shim for that!”
    ereg* functions
    bbrala/php7-ereg-shim
    (Björn Brala)

    View full-size slide

  51. PHP 7.0 “Gotchas”
    Another
    one bites
    the dust
    ext/mysql
    functions

    View full-size slide

  52. PHP 7.0 “Gotchas”
    “There’s a shim for that!”
    ext/mysql functions
    dshafik/php7-mysql-shim
    (Davey Shafik)

    View full-size slide

  53. Caveats though…

    View full-size slide

  54. PHP 7.0 “Gotchas”
    Another bird
    with the
    PHP 7.0 stone
    ext/mssql
    functions
    instead use
    sqlsrv_*(), odbc_*(), PDO

    View full-size slide

  55. PHP 7.0 “Gotchas”
    A few others
    for the
    chopping
    block
    Some
    ext/mcrypt
    functions,
    et. al.

    View full-size slide

  56. PHP 7.0 “Gotchas”
    Scalar Type
    Declarations
    New reserved keywords:
    string, int, bool, float

    View full-size slide

  57. PHP 7.0 “Gotchas”
    Uniform
    Variable Syntax
    Change in evaluating indirect expressions
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  58. Variable variables
    string(5) "world"

    View full-size slide

  59. string(16) "PHP 5.x I see..."
    5.x

    View full-size slide

  60. 5.x
    string(16) "PHP 5.x I see..."

    View full-size slide

  61. 7.x
    Notice: Array to string conversion in /
    foo.php on line 6
    Notice: Undefined variable: Array in /
    foo.php on line 6

    View full-size slide

  62. FOUR
    Step
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  63. Code!
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  64. Tests
    TESTS
    Tests
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  65. PHPUnit?
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  66. Acceptance
    Tests
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  67. Codeception
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  68. Behavior-driven
    testing framework
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  69. Composer +
    Autoloading
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  70. @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  71. Feature
    (User Story)

    View full-size slide

  72. @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  73. Scenario
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  74. FIVE
    Step
    “The Refactoring Loop”
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  75. refactorLoop:
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  76. Start
    small
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  77. Make sure there’s a test
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  78. Refactor
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  79. Run your tests
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  80. goto refactorLoop;
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  81. lets you refactor like a
    BEAST
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  82. PHPUnit
    Don’t forget about

    View full-size slide

  83. PHP 7.0
    Features
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  84. random_int()
    random_bytes()
    PHP 7.0 Features
    CSPRNG!!
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  85. JOURNEY
    MY
    CENTER
    TO
    THE
    OF
    #ShamelessPlug
    Tomorrow @ 10 am
    in Main Ballroom

    View full-size slide

  86. PHP 7.0 Features
    Scalar Type
    Declarations
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  87. PHP 7.0 Features
    Scalar Type Declarations
    function foo(string $name,

    int $age,

    bool $isGreat,

    float $income)

    { /* */ }

    View full-size slide

  88. PHP 7.0 Features
    Scalar Type Declarations
    function foo(string $name,

    int $age,

    bool $isGreat,

    float $income)

    { /* */ }

    View full-size slide

  89. PHP 7.0 Features
    Return type
    declarations
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  90. PHP 7.0 Features
    Return type declarations
    /**

    * @return Collection

    */

    public function foo()

    {

    }

    View full-size slide

  91. PHP 7.0 Features
    /**

    * @return Collection

    */

    public function foo()

    {

    }
    Return type declarations

    View full-size slide

  92. PHP 7.0 Features
    public function foo(): Collection

    {

    }
    Return type declarations

    View full-size slide

  93. PHP 7.0 Features
    Null coalesce
    operator
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  94. PHP 7.0 Features
    $bar = isset($_GET[‘foo’]) ?
    $_GET[‘foo’] : null;
    Null coalesce operator
    $bar = $_GET[‘foo’] ?? null;

    View full-size slide

  95. PHP 7.0 Features
    Insane speed
    improvements
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  96. PHP 7.1
    Considerations & Features
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  97. PHP 7.1 Considerations
    deprecated
    ext/mcrypt
    Soon to be...

    View full-size slide

  98. rand() &
    mt_rand()
    changes
    PHP 7.1 Considerations

    View full-size slide

  99. Totes Samezies!

    View full-size slide

  100. array_rand()
    Now with even distribution!
    PHP 7.1 Considerations

    View full-size slide

  101. Void return
    types
    PHP 7.1 Features
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  102. public function foo(): void

    {

    }
    Void return types
    PHP 7.1 Features

    View full-size slide

  103. Nullable
    types
    (kinda like union types for null)
    @SammyK #mwphp17 joind.in/talk/82979
    PHP 7.1 Features

    View full-size slide

  104. Nullable types
    function foo(string $name)

    { /* */ }
    PHP 7.1 Features

    View full-size slide

  105. Nullable types
    function foo(?string $name)

    { /* */ }
    PHP 7.1 Features

    View full-size slide

  106. Nullable types
    function foo(string $name = null)

    { /* */ }
    foo();
    PHP 7.1 Features

    View full-size slide

  107. Nullable types
    foo();
    function foo(?string $name)

    { /* */ }
    foo(null);
    PHP 7.1 Features

    View full-size slide

  108. Nullable types
    function foo(): ?string {
    return null;
    }
    PHP 7.1 Features

    View full-size slide

  109. Class constant
    visibility
    @SammyK #mwphp17 joind.in/talk/82979
    PHP 7.1 Features

    View full-size slide

  110. Class constant visibility
    class Foo {

    public const FOO = 0;
    protected const BAR = 1;
    private const BAZ = 2;
    }
    PHP 7.1 Features

    View full-size slide

  111. `iterable`
    pseudo-type
    @SammyK #mwphp17 joind.in/talk/82979
    PHP 7.1 Features

    View full-size slide

  112. `iterable` pseudo-type
    function foo(iterable $a)

    { /* */ }
    $b = [];
    foo($b);
    class C implements Iterator {}
    $b = new C;
    foo($b);
    PHP 7.1 Features

    View full-size slide

  113. `iterable` pseudo-type
    class D implements IteratorAggregate {}
    foo(new D);
    Traversable
    Or instance of
    PHP 7.1 Features

    View full-size slide

  114. And
    beyond?
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  115. PHP 7.2
    Considering
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  116. PHP 7.2 considerations
    re(moved)
    ext/mcrypt

    View full-size slide

  117. PHP 7.2 considerations
    Argon2
    Password Hash
    password_*()
    (Could become new default in PHP 7.3/8.0)

    View full-size slide

  118. Pending implementation…
    PHP 7.2

    View full-size slide

  119. PHP 7.2 considerations
    sodium_*()

    View full-size slide

  120. PHP 7.2 considerations
    Trailing Comma

    View full-size slide

  121. 7.2 Deprecations
    (Will be removed by PHP 8.0)

    View full-size slide

  122. Some tools
    I like
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  123. vlucas/phpdotenv
    .env
    No peeking!
    No peeking!

    View full-size slide

  124. Some tools I like
    pimple/pimple
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  125. Some tools I like
    monolog/monolog
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  126. Some tools I like
    nikic/fast-route
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  127. Some tools I like
    illuminate/database
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  128. Some tools I like
    symfony/console
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  129. Create a PHP
    application without a
    framework
    https://github.com/PatrickLouys/no-framework-tutorial
    Patrick Louys
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  130. Big-picture
    Refactoring
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  131. Constant refactoring
    is the key to managing
    legacy
    codebases
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  132. Constant refactoring
    is the key to managing
    legacy
    codebases
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  133. Resources
    013: TDD & BDD In PHP
    055: Acceptance
    Testing with Behat
    017: Modernizing Legacy
    Codebases in PHP
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  134. Modernizing Legacy
    Applications in PHP
    https://leanpub.com/mlaphp
    Paul M. Jones
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  135. Refactoring
    Martin Fowler
    http://martinfowler.com/books/refactoring.html
    @SammyK #mwphp17 joind.in/talk/82979

    View full-size slide

  136. Takk!
    (thanks)
    @SammyK
    SammyK.me
    Host of @PHPRoundtable
    @ChiPHPUG
    West Coast Swing
    Hire me! :)
    /talk/82979

    View full-size slide