Pro Yearly is on sale from $80 to $50! »

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.

8c090cc1ccd623a146ddd9159b1bf7e2?s=128

Sammy Kaye Powers

March 17, 2017
Tweet

Transcript

  1. Bringing Old Legacy Apps To March 17th, 2017 Sammy Kaye

    Powers @SammyK PHP 7 & Beyond
  2. Codebase @SammyK #mwphp17 joind.in/talk/82979

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

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

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

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

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

  9. Functionality Existing Codebase Fancy-pants rewrite

  10. Functionality Existing Codebase Fancy-pants rewrite

  11. Functionality Existing Codebase Fancy-pants rewrite

  12. Functionality Existing Codebase Fancy-pants rewrite

  13. Functionality Existing Codebase Fancy-pants rewrite

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

  15. “Legacy” /leɡəsē/ • Requires an EOL version of PHP •

    No automated tests • Has outdated dependencies • No autoloading • No sign of “single responsibility”
  16. None
  17. “Legacy” We’ll assume …means any code that can be improved

    (at least for this talk) @SammyK #mwphp17 joind.in/talk/82979
  18. Expectations @SammyK #mwphp17 joind.in/talk/82979

  19. elePHPant How to eat an

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

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

  22. 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)
  23. Analysis Static @SammyK #mwphp17 joind.in/talk/82979

  24. phploc

  25. phploc

  26. phpcpd

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

  28. Consider the Stakeholders I’ll need 80 hours to refactor the

    user auth & ACL @SammyK #mwphp17 joind.in/talk/82979
  29. Consider the Stakeholders • Most critical security holes • Most

    critical bugs • Most critical features • Use data from static analysis Make a plan
  30. ONE Step @SammyK #mwphp17 joind.in/talk/82979

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

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

  33. Kickass Development Environments with Docker David McKay Tomorrow @ 11

    AM in Ballroom D @SammyK #mwphp17 joind.in/talk/82979
  34. Git with an SCM

  35. None
  36. Git Get with master original local-config

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

  38. None
  39. TWO Step @SammyK #mwphp17 joind.in/talk/82979

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

  41. 7.1 ✓Performance increase PHP 7 features ✓

  42. 7.1

  43. BC is totes amaze

  44. PHP 7 enlightenment The path to 7.1 5.6

  45. 7.1 5.5 PHP 7 enlightenment The path to

  46. 7.1 5.4 PHP 7 enlightenment The path to

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

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

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

  50. PHP 7 Quick tricks to refactor to (in order of

    ease) @SammyK #mwphp17 joind.in/talk/82979
  51. PHP 7.0 “Gotchas” ASP and <script> PHP tags removed @SammyK

    #mwphp17 joind.in/talk/82979
  52. <% %> <%= %> <script language=“php"> </script> @SammyK #mwphp17 joind.in/talk/82979

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

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

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

  56. None
  57. None
  58. PHP 7.0 “Gotchas” Some functions got the ax ereg* functions

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

  60. PHP 7.0 “Gotchas” “There’s a shim for that!” ereg* functions

    bbrala/php7-ereg-shim (Björn Brala)
  61. None
  62. PHP 7.0 “Gotchas” Another one bites the dust ext/mysql functions

  63. PHP 7.0 “Gotchas” “There’s a shim for that!” ext/mysql functions

    dshafik/php7-mysql-shim (Davey Shafik)
  64. Caveats though…

  65. PHP 7.0 “Gotchas” Another bird with the PHP 7.0 stone

    ext/mssql functions instead use sqlsrv_*(), odbc_*(), PDO
  66. PHP 7.0 “Gotchas” A few others for the chopping block

    Some ext/mcrypt functions, et. al.
  67. PHP 7.0 “Gotchas” Scalar Type Declarations New reserved keywords: string,

    int, bool, float
  68. None
  69. PHP 7.0 “Gotchas” Uniform Variable Syntax Change in evaluating indirect

    expressions @SammyK #mwphp17 joind.in/talk/82979
  70. None
  71. Variable variables string(5) "world"

  72. None
  73. string(16) "PHP 5.x I see..." 5.x

  74. 7.x

  75. None
  76. 5.x

  77. 5.x

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

  79. 7.x

  80. 7.x

  81. 7.x

  82. 7.x Notice: Array to string conversion in / foo.php on

    line 6 Notice: Undefined variable: Array in / foo.php on line 6
  83. Funky

  84. None
  85. None
  86. 7.x 5.x

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

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

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

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

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

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

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

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

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

  96. Feature (User Story)

  97. Scenario

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

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

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

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

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

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

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

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

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

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

  110. PHPUnit Don’t forget about

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

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

  113. JOURNEY MY CENTER TO THE OF #ShamelessPlug Tomorrow @ 10

    am in Main Ballroom
  114. PHP 7.0 Features Scalar Type Declarations @SammyK #mwphp17 joind.in/talk/82979

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

    $age,
 bool $isGreat,
 float $income)
 { /* */ }
  116. PHP 7.0 Features Scalar Type Declarations function foo(string $name,
 int

    $age,
 bool $isGreat,
 float $income)
 { /* */ }
  117. PHP 7.0 Features Return type declarations @SammyK #mwphp17 joind.in/talk/82979

  118. PHP 7.0 Features Return type declarations /**
 * @return Collection


    */
 public function foo()
 {
 }
  119. PHP 7.0 Features /**
 * @return Collection
 */
 public function

    foo()
 {
 } Return type declarations
  120. PHP 7.0 Features public function foo(): Collection
 {
 } Return

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

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

    Null coalesce operator $bar = $_GET[‘foo’] ?? null;
  123. PHP 7.0 Features Insane speed improvements @SammyK #mwphp17 joind.in/talk/82979

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

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

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

  127. None
  128. Totes Samezies!

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

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

  131. public function foo(): void
 {
 } Void return types PHP

    7.1 Features
  132. Nullable types (kinda like union types for null) @SammyK #mwphp17

    joind.in/talk/82979 PHP 7.1 Features
  133. Nullable types function foo(string $name)
 { /* */ } PHP

    7.1 Features
  134. Nullable types function foo(?string $name)
 { /* */ } PHP

    7.1 Features
  135. Nullable types function foo(string $name = null)
 { /* */

    } foo(); PHP 7.1 Features
  136. Nullable types foo(); function foo(?string $name)
 { /* */ }

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

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

  139. Class constant visibility class Foo {
 public const FOO =

    0; protected const BAR = 1; private const BAZ = 2; } PHP 7.1 Features
  140. `iterable` pseudo-type @SammyK #mwphp17 joind.in/talk/82979 PHP 7.1 Features

  141. `iterable` pseudo-type function foo(iterable $a)
 { /* */ } $b

    = []; foo($b); class C implements Iterator {} $b = new C; foo($b); PHP 7.1 Features
  142. `iterable` pseudo-type class D implements IteratorAggregate {} foo(new D); Traversable

    Or instance of PHP 7.1 Features
  143. And beyond? @SammyK #mwphp17 joind.in/talk/82979

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

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

  146. PHP 7.2 considerations Argon2 Password Hash password_*() (Could become new

    default in PHP 7.3/8.0)
  147. Pending implementation… PHP 7.2

  148. PHP 7.2 considerations sodium_*()

  149. PHP 7.2 considerations Trailing Comma

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

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

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

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

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

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

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

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

  158. Create a PHP application without a framework https://github.com/PatrickLouys/no-framework-tutorial Patrick Louys

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

  160. Constant refactoring is the key to managing legacy codebases @SammyK

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

    #mwphp17 joind.in/talk/82979
  162. Resources 013: TDD & BDD In PHP 055: Acceptance Testing

    with Behat 017: Modernizing Legacy Codebases in PHP @SammyK #mwphp17 joind.in/talk/82979
  163. Modernizing Legacy Applications in PHP https://leanpub.com/mlaphp Paul M. Jones @SammyK

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

  165. Takk! (thanks) @SammyK SammyK.me Host of @PHPRoundtable @ChiPHPUG West Coast

    Swing Hire me! :) /talk/82979