Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

Functionality Existing Codebase Fancy-pants rewrite

Slide 10

Slide 10 text

Functionality Existing Codebase Fancy-pants rewrite

Slide 11

Slide 11 text

Functionality Existing Codebase Fancy-pants rewrite

Slide 12

Slide 12 text

Functionality Existing Codebase Fancy-pants rewrite

Slide 13

Slide 13 text

Functionality Existing Codebase Fancy-pants rewrite

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

elePHPant How to eat an

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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 23

Slide 23 text

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

Slide 24

Slide 24 text

phploc

Slide 25

Slide 25 text

phploc

Slide 26

Slide 26 text

phpcpd

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

Git with an SCM

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

Git Get with master original local-config

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

7.1 ✓Performance increase PHP 7 features ✓

Slide 42

Slide 42 text

7.1

Slide 43

Slide 43 text

BC is totes amaze

Slide 44

Slide 44 text

PHP 7 enlightenment The path to 7.1 5.6

Slide 45

Slide 45 text

7.1 5.5 PHP 7 enlightenment The path to

Slide 46

Slide 46 text

7.1 5.4 PHP 7 enlightenment The path to

Slide 47

Slide 47 text

7.1 >= 5.3 PHP 7 enlightenment The path to

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

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

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

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

Slide 54

Slide 54 text

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

Slide 55

Slide 55 text

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

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

No content

Slide 58

Slide 58 text

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

Slide 59

Slide 59 text

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

Slide 60

Slide 60 text

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

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

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

Slide 63

Slide 63 text

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

Slide 64

Slide 64 text

Caveats though…

Slide 65

Slide 65 text

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

Slide 66

Slide 66 text

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

Slide 67

Slide 67 text

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

Slide 68

Slide 68 text

No content

Slide 69

Slide 69 text

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

Slide 70

Slide 70 text

No content

Slide 71

Slide 71 text

Variable variables string(5) "world"

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

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

Slide 74

Slide 74 text

7.x

Slide 75

Slide 75 text

No content

Slide 76

Slide 76 text

5.x

Slide 77

Slide 77 text

5.x

Slide 78

Slide 78 text

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

Slide 79

Slide 79 text

7.x

Slide 80

Slide 80 text

7.x

Slide 81

Slide 81 text

7.x

Slide 82

Slide 82 text

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

Slide 83

Slide 83 text

Funky

Slide 84

Slide 84 text

No content

Slide 85

Slide 85 text

No content

Slide 86

Slide 86 text

7.x 5.x

Slide 87

Slide 87 text

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

Slide 88

Slide 88 text

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

Slide 89

Slide 89 text

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

Slide 90

Slide 90 text

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

Slide 91

Slide 91 text

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

Slide 92

Slide 92 text

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

Slide 93

Slide 93 text

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

Slide 94

Slide 94 text

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

Slide 95

Slide 95 text

@SammyK #mwphp17 joind.in/talk/82979

Slide 96

Slide 96 text

Feature (User Story)

Slide 97

Slide 97 text

Scenario

Slide 98

Slide 98 text

@SammyK #mwphp17 joind.in/talk/82979

Slide 99

Slide 99 text

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

Slide 100

Slide 100 text

No content

Slide 101

Slide 101 text

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

Slide 102

Slide 102 text

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

Slide 103

Slide 103 text

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

Slide 104

Slide 104 text

No content

Slide 105

Slide 105 text

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

Slide 106

Slide 106 text

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

Slide 107

Slide 107 text

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

Slide 108

Slide 108 text

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

Slide 109

Slide 109 text

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

Slide 110

Slide 110 text

PHPUnit Don’t forget about

Slide 111

Slide 111 text

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

Slide 112

Slide 112 text

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

Slide 113

Slide 113 text

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

Slide 114

Slide 114 text

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

Slide 115

Slide 115 text

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

Slide 116

Slide 116 text

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

Slide 117

Slide 117 text

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

Slide 118

Slide 118 text

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

Slide 119

Slide 119 text

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

Slide 120

Slide 120 text

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

Slide 121

Slide 121 text

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

Slide 122

Slide 122 text

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

Slide 123

Slide 123 text

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

Slide 124

Slide 124 text

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

Slide 125

Slide 125 text

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

Slide 126

Slide 126 text

rand() & mt_rand() changes PHP 7.1 Considerations

Slide 127

Slide 127 text

No content

Slide 128

Slide 128 text

Totes Samezies!

Slide 129

Slide 129 text

array_rand() Now with even distribution! PHP 7.1 Considerations

Slide 130

Slide 130 text

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

Slide 131

Slide 131 text

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

Slide 132

Slide 132 text

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

Slide 133

Slide 133 text

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

Slide 134

Slide 134 text

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

Slide 135

Slide 135 text

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

Slide 136

Slide 136 text

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

Slide 137

Slide 137 text

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

Slide 138

Slide 138 text

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

Slide 139

Slide 139 text

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

Slide 140

Slide 140 text

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

Slide 141

Slide 141 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 142

Slide 142 text

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

Slide 143

Slide 143 text

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

Slide 144

Slide 144 text

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

Slide 145

Slide 145 text

PHP 7.2 considerations re(moved) ext/mcrypt

Slide 146

Slide 146 text

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

Slide 147

Slide 147 text

Pending implementation… PHP 7.2

Slide 148

Slide 148 text

PHP 7.2 considerations sodium_*()

Slide 149

Slide 149 text

PHP 7.2 considerations Trailing Comma

Slide 150

Slide 150 text

7.2 Deprecations (Will be removed by PHP 8.0)

Slide 151

Slide 151 text

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

Slide 152

Slide 152 text

vlucas/phpdotenv .env No peeking! No peeking!

Slide 153

Slide 153 text

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

Slide 154

Slide 154 text

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

Slide 155

Slide 155 text

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

Slide 156

Slide 156 text

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

Slide 157

Slide 157 text

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

Slide 158

Slide 158 text

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

Slide 159

Slide 159 text

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

Slide 160

Slide 160 text

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

Slide 161

Slide 161 text

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

Slide 162

Slide 162 text

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

Slide 163

Slide 163 text

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

Slide 164

Slide 164 text

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

Slide 165

Slide 165 text

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