Slide 1

Slide 1 text

Bringing Old Legacy Apps To September 9th, 2017 Sammy Kaye Powers @SammyK PHP 7 & Beyond

Slide 2

Slide 2 text

joind.in/talk/228f1 @SammyK #pnwphp Slides Get the joind.in/talk/228f1

Slide 3

Slide 3 text

joind.in/talk/228f1 @SammyK #pnwphp Upgrade Path 7.1 Less about More about Refactoring Strategies to

Slide 4

Slide 4 text

joind.in/talk/228f1 @SammyK #pnwphp Codebase

Slide 5

Slide 5 text

joind.in/talk/228f1 @SammyK #pnwphp

Slide 6

Slide 6 text

joind.in/talk/228f1 @SammyK #pnwphp I’ll just rewrite this in ____________!

Slide 7

Slide 7 text

joind.in/talk/228f1 @SammyK #pnwphp I’ll just rewrite this in ____________!

Slide 8

Slide 8 text

joind.in/talk/228f1 @SammyK #pnwphp I’ll just rewrite this in ____________! Laravel

Slide 9

Slide 9 text

joind.in/talk/228f1 @SammyK #pnwphp I’ll just rewrite this in ____________!

Slide 10

Slide 10 text

joind.in/talk/228f1 @SammyK #pnwphp rewrite from scratch? Why not

Slide 11

Slide 11 text

joind.in/talk/228f1 @SammyK #pnwphp Functionality Existing Codebase Fancy-pants rewrite

Slide 12

Slide 12 text

joind.in/talk/228f1 @SammyK #pnwphp Functionality Existing Codebase Fancy-pants rewrite

Slide 13

Slide 13 text

joind.in/talk/228f1 @SammyK #pnwphp Functionality Existing Codebase Fancy-pants rewrite

Slide 14

Slide 14 text

joind.in/talk/228f1 @SammyK #pnwphp Functionality Existing Codebase Fancy-pants rewrite

Slide 15

Slide 15 text

joind.in/talk/228f1 @SammyK #pnwphp Functionality Existing Codebase Fancy-pants rewrite

Slide 16

Slide 16 text

joind.in/talk/228f1 @SammyK #pnwphp Refactoring Martin Fowler http://martinfowler.com/books/refactoring.html

Slide 17

Slide 17 text

joind.in/talk/228f1 @SammyK #pnwphp “Legacy” /leɡəsē/ • Requires an EOL version of PHP • No automated tests • Has outdated dependencies • No autoloading • No sign of “single responsibility”

Slide 18

Slide 18 text

joind.in/talk/228f1 @SammyK #pnwphp

Slide 19

Slide 19 text

joind.in/talk/228f1 @SammyK #pnwphp “Legacy” We’ll assume …means any code that is running on PHP 5.x (at least for this talk)

Slide 20

Slide 20 text

joind.in/talk/228f1 @SammyK #pnwphp Expectations

Slide 21

Slide 21 text

joind.in/talk/228f1 @SammyK #pnwphp elePHPant How to eat an

Slide 22

Slide 22 text

joind.in/talk/228f1 @SammyK #pnwphp ZERO Step

Slide 23

Slide 23 text

joind.in/talk/228f1 @SammyK #pnwphp Around Poke

Slide 24

Slide 24 text

joind.in/talk/228f1 @SammyK #pnwphp 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)

Slide 25

Slide 25 text

joind.in/talk/228f1 @SammyK #pnwphp Analysis Static

Slide 26

Slide 26 text

phploc

Slide 27

Slide 27 text

phploc

Slide 28

Slide 28 text

phpcpd

Slide 29

Slide 29 text

joind.in/talk/228f1 @SammyK #pnwphp Consider the Stakeholders

Slide 30

Slide 30 text

joind.in/talk/228f1 @SammyK #pnwphp Consider the Stakeholders I’ll need 80 hours to refactor the user auth & ACL

Slide 31

Slide 31 text

joind.in/talk/228f1 @SammyK #pnwphp Consider the Stakeholders • Most critical security holes • Most critical bugs • Most critical features • Use data from static analysis Make a plan

Slide 32

Slide 32 text

joind.in/talk/228f1 @SammyK #pnwphp ONE Step

Slide 33

Slide 33 text

joind.in/talk/228f1 @SammyK #pnwphp 5.(wat) Get it running on

Slide 34

Slide 34 text

Environment Get it running on your local

Slide 35

Slide 35 text

joind.in/talk/228f1 @SammyK #pnwphp Git with an SCM

Slide 36

Slide 36 text

joind.in/talk/228f1 @SammyK #pnwphp

Slide 37

Slide 37 text

joind.in/talk/228f1 @SammyK #pnwphp Git Get with master original local-config

Slide 38

Slide 38 text

joind.in/talk/228f1 @SammyK #pnwphp Fix all the errors

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

joind.in/talk/228f1 @SammyK #pnwphp TWO Step

Slide 41

Slide 41 text

joind.in/talk/228f1 @SammyK #pnwphp 7.1 Get it running on

Slide 42

Slide 42 text

joind.in/talk/228f1 @SammyK #pnwphp PHP 7.0 Is almost 2 years old!

Slide 43

Slide 43 text

joind.in/talk/228f1 @SammyK #pnwphp 2

Slide 44

Slide 44 text

joind.in/talk/228f1 @SammyK #pnwphp years

Slide 45

Slide 45 text

joind.in/talk/228f1 @SammyK #pnwphp old!

Slide 46

Slide 46 text

joind.in/talk/228f1 @SammyK #pnwphp OLD

Slide 47

Slide 47 text

joind.in/talk/228f1 @SammyK #pnwphp http://php.net/supported-versions.php > 3 months Active support (bug fixes) for PHP 7.0 ends

Slide 48

Slide 48 text

joind.in/talk/228f1 @SammyK #pnwphp OLD PHP 7.0 is

Slide 49

Slide 49 text

7.1 ✓Performance increase PHP 7 features ✓

Slide 50

Slide 50 text

7.1

Slide 51

Slide 51 text

BC is totes amaze

Slide 52

Slide 52 text

joind.in/talk/228f1 @SammyK #pnwphp PHP 7 enlightenment The path to 7.1 5.6

Slide 53

Slide 53 text

joind.in/talk/228f1 @SammyK #pnwphp 7.1 5.5 PHP 7 enlightenment The path to

Slide 54

Slide 54 text

joind.in/talk/228f1 @SammyK #pnwphp 7.1 5.4 PHP 7 enlightenment The path to

Slide 55

Slide 55 text

joind.in/talk/228f1 @SammyK #pnwphp 7.1 <= 5.3 PHP 7 enlightenment The path to

Slide 56

Slide 56 text

joind.in/talk/228f1 @SammyK #pnwphp THREE Step

Slide 57

Slide 57 text

joind.in/talk/228f1 @SammyK #pnwphp Fix all the PHP 7 errors

Slide 58

Slide 58 text

joind.in/talk/228f1 @SammyK #pnwphp PHP 7 Quick tricks to refactor to (in order of ease)

Slide 59

Slide 59 text

joind.in/talk/228f1 @SammyK #pnwphp PHP 7.0 “Gotchas” ASP and PHP tags removed

Slide 60

Slide 60 text

<% %> <%= %>

Slide 61

Slide 61 text

joind.in/talk/228f1 @SammyK #pnwphp PHP 7.0 “Gotchas” removed $HTTP_RAW_POST_DATA

Slide 62

Slide 62 text

joind.in/talk/228f1 @SammyK #pnwphp php://input instead read from

Slide 63

Slide 63 text

joind.in/talk/228f1 @SammyK #pnwphp PHP 7.0 “Gotchas” PHP-4-style constructors deprecated

Slide 64

Slide 64 text

joind.in/talk/228f1 @SammyK #pnwphp

Slide 65

Slide 65 text

joind.in/talk/228f1 @SammyK #pnwphp

Slide 66

Slide 66 text

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

Slide 67

Slide 67 text

joind.in/talk/228f1 @SammyK #pnwphp PHP 7.0 “Gotchas” “There’s a shim for that!”

Slide 68

Slide 68 text

joind.in/talk/228f1 @SammyK #pnwphp PHP 7.0 “Gotchas” “There’s a shim for that!” ereg* functions bbrala/php7-ereg-shim (Björn Brala)

Slide 69

Slide 69 text

joind.in/talk/228f1 @SammyK #pnwphp

Slide 70

Slide 70 text

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

Slide 71

Slide 71 text

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

Slide 72

Slide 72 text

joind.in/talk/228f1 @SammyK #pnwphp Caveats though…

Slide 73

Slide 73 text

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

Slide 74

Slide 74 text

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

Slide 75

Slide 75 text

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

Slide 76

Slide 76 text

No content

Slide 77

Slide 77 text

joind.in/talk/228f1 @SammyK #pnwphp PHP 7.0 “Gotchas” Uniform Variable Syntax Change in evaluating indirect expressions

Slide 78

Slide 78 text

joind.in/talk/228f1 @SammyK #pnwphp

Slide 79

Slide 79 text

Variable variables string(5) "world"

Slide 80

Slide 80 text

joind.in/talk/228f1 @SammyK #pnwphp

Slide 81

Slide 81 text

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

Slide 82

Slide 82 text

7.x

Slide 83

Slide 83 text

joind.in/talk/228f1 @SammyK #pnwphp

Slide 84

Slide 84 text

joind.in/talk/228f1 @SammyK #pnwphp 5.x

Slide 85

Slide 85 text

joind.in/talk/228f1 @SammyK #pnwphp 5.x

Slide 86

Slide 86 text

joind.in/talk/228f1 @SammyK #pnwphp 5.x string(16) "PHP 5.x I see..."

Slide 87

Slide 87 text

joind.in/talk/228f1 @SammyK #pnwphp 7.x

Slide 88

Slide 88 text

joind.in/talk/228f1 @SammyK #pnwphp 7.x

Slide 89

Slide 89 text

joind.in/talk/228f1 @SammyK #pnwphp 7.x

Slide 90

Slide 90 text

joind.in/talk/228f1 @SammyK #pnwphp 7.x Notice: Array to string conversion in / foo.php on line 6 Notice: Undefined variable: Array in / foo.php on line 6

Slide 91

Slide 91 text

joind.in/talk/228f1 @SammyK #pnwphp Funky

Slide 92

Slide 92 text

joind.in/talk/228f1 @SammyK #pnwphp

Slide 93

Slide 93 text

No content

Slide 94

Slide 94 text

joind.in/talk/228f1 @SammyK #pnwphp 7.x 5.x

Slide 95

Slide 95 text

joind.in/talk/228f1 @SammyK #pnwphp FOUR Step

Slide 96

Slide 96 text

joind.in/talk/228f1 @SammyK #pnwphp Code!

Slide 97

Slide 97 text

joind.in/talk/228f1 @SammyK #pnwphp Tests TESTS Tests

Slide 98

Slide 98 text

joind.in/talk/228f1 @SammyK #pnwphp PHPUnit?

Slide 99

Slide 99 text

joind.in/talk/228f1 @SammyK #pnwphp Acceptance Tests

Slide 100

Slide 100 text

joind.in/talk/228f1 @SammyK #pnwphp Codeception

Slide 101

Slide 101 text

joind.in/talk/228f1 @SammyK #pnwphp Behavior-driven testing framework

Slide 102

Slide 102 text

joind.in/talk/228f1 @SammyK #pnwphp Composer + Autoloading

Slide 103

Slide 103 text

joind.in/talk/228f1 @SammyK #pnwphp

Slide 104

Slide 104 text

Feature (User Story)

Slide 105

Slide 105 text

joind.in/talk/228f1 @SammyK #pnwphp Scenario

Slide 106

Slide 106 text

joind.in/talk/228f1 @SammyK #pnwphp

Slide 107

Slide 107 text

joind.in/talk/228f1 @SammyK #pnwphp Scenario

Slide 108

Slide 108 text

No content

Slide 109

Slide 109 text

joind.in/talk/228f1 @SammyK #pnwphp FIVE Step “The Refactoring Loop”

Slide 110

Slide 110 text

joind.in/talk/228f1 @SammyK #pnwphp refactorLoop:

Slide 111

Slide 111 text

joind.in/talk/228f1 @SammyK #pnwphp Start small

Slide 112

Slide 112 text

No content

Slide 113

Slide 113 text

joind.in/talk/228f1 @SammyK #pnwphp Make sure there’s a test

Slide 114

Slide 114 text

joind.in/talk/228f1 @SammyK #pnwphp Refactor

Slide 115

Slide 115 text

joind.in/talk/228f1 @SammyK #pnwphp Run your tests

Slide 116

Slide 116 text

joind.in/talk/228f1 @SammyK #pnwphp goto refactorLoop;

Slide 117

Slide 117 text

joind.in/talk/228f1 @SammyK #pnwphp lets you refactor like a BEAST

Slide 118

Slide 118 text

PHPUnit Don’t forget about

Slide 119

Slide 119 text

joind.in/talk/228f1 @SammyK #pnwphp End-to-end is great! Except when…

Slide 120

Slide 120 text

joind.in/talk/228f1 @SammyK #pnwphp It’s Not

Slide 121

Slide 121 text

joind.in/talk/228f1 @SammyK #pnwphp PHP 7.0 Features

Slide 122

Slide 122 text

joind.in/talk/228f1 @SammyK #pnwphp random_int() random_bytes() PHP 7.0 Features CSPRNG!!

Slide 123

Slide 123 text

joind.in/talk/228f1 @SammyK #pnwphp PHP 7.0 Features Scalar Type Declarations

Slide 124

Slide 124 text

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

Slide 125

Slide 125 text

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

Slide 126

Slide 126 text

joind.in/talk/228f1 @SammyK #pnwphp PHP 7.0 Features Return type declarations

Slide 127

Slide 127 text

PHP 7.0 Features Return type declarations /**
 * @return Collection
 */
 public function foo()
 {
 }

Slide 128

Slide 128 text

PHP 7.0 Features /**
 * @return Collection
 */
 public function foo()
 {
 } Return type declarations

Slide 129

Slide 129 text

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

Slide 130

Slide 130 text

joind.in/talk/228f1 @SammyK #pnwphp PHP 7.0 Features Null coalesce operator

Slide 131

Slide 131 text

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

Slide 132

Slide 132 text

joind.in/talk/228f1 @SammyK #pnwphp PHP 7.0 Features Insane speed improvements

Slide 133

Slide 133 text

joind.in/talk/228f1 @SammyK #pnwphp PHP 7.1 Considerations & Features

Slide 134

Slide 134 text

joind.in/talk/228f1 @SammyK #pnwphp PHP 7.1 Considerations deprecated ext/mcrypt Soon to be...

Slide 135

Slide 135 text

joind.in/talk/228f1 @SammyK #pnwphp rand() & mt_rand() changes PHP 7.1 Considerations

Slide 136

Slide 136 text

joind.in/talk/228f1 @SammyK #pnwphp

Slide 137

Slide 137 text

joind.in/talk/228f1 @SammyK #pnwphp Totes Samezies!

Slide 138

Slide 138 text

joind.in/talk/228f1 @SammyK #pnwphp array_rand() Now with even distribution! PHP 7.1 Considerations

Slide 139

Slide 139 text

joind.in/talk/228f1 @SammyK #pnwphp Void return types PHP 7.1 Features

Slide 140

Slide 140 text

public function foo(): void
 {
 } Void return types PHP 7.1 Features

Slide 141

Slide 141 text

joind.in/talk/228f1 @SammyK #pnwphp Nullable types (kinda like union types for null) PHP 7.1 Features

Slide 142

Slide 142 text

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

Slide 143

Slide 143 text

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

Slide 144

Slide 144 text

Nullable types function foo(string $name = null)
 { /* */ } foo(); PHP 7.1 Features

Slide 145

Slide 145 text

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

Slide 146

Slide 146 text

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

Slide 147

Slide 147 text

joind.in/talk/228f1 @SammyK #pnwphp Class constant visibility PHP 7.1 Features

Slide 148

Slide 148 text

Class constant visibility class Foo {
 public const FOO = 0; protected const BAR = 1; private const BAZ = 2; } PHP 7.1 Features

Slide 149

Slide 149 text

joind.in/talk/228f1 @SammyK #pnwphp `iterable` pseudo-type PHP 7.1 Features

Slide 150

Slide 150 text

`iterable` pseudo-type function foo(iterable $a)
 { /* */ } $b = []; foo($b); class C implements Iterator {} $b = new C; foo($b); PHP 7.1 Features

Slide 151

Slide 151 text

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

Slide 152

Slide 152 text

joind.in/talk/228f1 @SammyK #pnwphp And beyond?

Slide 153

Slide 153 text

joind.in/talk/228f1 @SammyK #pnwphp PHP 7.2 Considering

Slide 154

Slide 154 text

PHP 7.2 considerations re(moved) ext/mcrypt

Slide 155

Slide 155 text

joind.in/talk/228f1 @SammyK #pnwphp PHP 7.2 considerations Argon2 Password Hash password_*() (Could become new default in PHP 7.3/8.0)

Slide 156

Slide 156 text

joind.in/talk/228f1 @SammyK #pnwphp PHP 7.2 considerations sodium_*()

Slide 157

Slide 157 text

joind.in/talk/228f1 @SammyK #pnwphp PHP 7.2 considerations Trailing Comma

Slide 158

Slide 158 text

joind.in/talk/228f1 @SammyK #pnwphp PHP 7.2 considerations Undefined Constants

Slide 159

Slide 159 text

joind.in/talk/228f1 @SammyK #pnwphp PHP 7.2 considerations

Slide 160

Slide 160 text

joind.in/talk/228f1 @SammyK #pnwphp PHP 7.2 considerations

Slide 161

Slide 161 text

joind.in/talk/228f1 @SammyK #pnwphp PHP 7.2 considerations Type Annotation object

Slide 162

Slide 162 text

joind.in/talk/228f1 @SammyK #pnwphp PHP 7.2 considerations

Slide 163

Slide 163 text

joind.in/talk/228f1 @SammyK #pnwphp 7.2 Deprecations (Will be removed by PHP 8.0)

Slide 164

Slide 164 text

joind.in/talk/228f1 @SammyK #pnwphp Some tools I like

Slide 165

Slide 165 text

vlucas/phpdotenv .env No peeking! No peeking!

Slide 166

Slide 166 text

vlucas/phpdotenv NotSecure :(

Slide 167

Slide 167 text

Env vars aren’t designed for secrets (shared with child processes, etc) Error messages can contain dump of env vars (filp/whoops)

Slide 168

Slide 168 text

joind.in/talk/228f1 @SammyK #pnwphp Some tools I like pimple/pimple

Slide 169

Slide 169 text

joind.in/talk/228f1 @SammyK #pnwphp Some tools I like monolog/monolog

Slide 170

Slide 170 text

joind.in/talk/228f1 @SammyK #pnwphp Some tools I like nikic/fast-route

Slide 171

Slide 171 text

joind.in/talk/228f1 @SammyK #pnwphp Some tools I like illuminate/database

Slide 172

Slide 172 text

joind.in/talk/228f1 @SammyK #pnwphp Some tools I like symfony/console

Slide 173

Slide 173 text

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

Slide 174

Slide 174 text

joind.in/talk/228f1 @SammyK #pnwphp Big-picture Refactoring

Slide 175

Slide 175 text

joind.in/talk/228f1 @SammyK #pnwphp Constant refactoring is the key to managing legacy codebases

Slide 176

Slide 176 text

joind.in/talk/228f1 @SammyK #pnwphp Constant refactoring is the key to managing legacy codebases

Slide 177

Slide 177 text

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

Slide 178

Slide 178 text

joind.in/talk/228f1 @SammyK #pnwphp Modernizing Legacy Applications in PHP https://leanpub.com/mlaphp Paul M. Jones

Slide 179

Slide 179 text

joind.in/talk/228f1 @SammyK #pnwphp Refactoring Martin Fowler http://martinfowler.com/books/refactoring.html

Slide 180

Slide 180 text

I have stickers! Takk! (thanks) @SammyK SammyK.me Host of @PHPRoundtable @ChiPHPUG West Coast Swing /talk/228f1