Slide 1

Slide 1 text

Bringing Old Legacy Apps To October 19th, 2016 Sammy Kaye Powers @SammyK PHP 7 & Beyond

Slide 2

Slide 2 text

Codebase @SammyK #zendcon2016 joind.in/talk/6d127

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

rewrite from scratch? Why not @SammyK #zendcon2016 joind.in/talk/6d127

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 @SammyK #zendcon2016 joind.in/talk/6d127 (at least for this talk)

Slide 18

Slide 18 text

Expectations @SammyK #zendcon2016 joind.in/talk/6d127

Slide 19

Slide 19 text

elePHPant How to eat an

Slide 20

Slide 20 text

ZERO Step @SammyK #zendcon2016 joind.in/talk/6d127

Slide 21

Slide 21 text

Around Poke @SammyK #zendcon2016 joind.in/talk/6d127

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 #zendcon2016 joind.in/talk/6d127

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 #zendcon2016 joind.in/talk/6d127

Slide 28

Slide 28 text

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

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 #zendcon2016 joind.in/talk/6d127

Slide 31

Slide 31 text

Environment Get it running on your local @SammyK #zendcon2016 joind.in/talk/6d127

Slide 32

Slide 32 text

From Docker to production Chris Tankersley @SammyK #zendcon2016 joind.in/talk/6d127 Today @ 5:15PM in Artist 3

Slide 33

Slide 33 text

Git Get with master original local-config

Slide 34

Slide 34 text

5.(wat) @SammyK #zendcon2016 joind.in/talk/6d127

Slide 35

Slide 35 text

Fix all the errors @SammyK #zendcon2016 joind.in/talk/6d127

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

TWO Step @SammyK #zendcon2016 joind.in/talk/6d127

Slide 38

Slide 38 text

7.0 @SammyK #zendcon2016 joind.in/talk/6d127

Slide 39

Slide 39 text

7.0 ✓Performance increase PHP 7 features ✓

Slide 40

Slide 40 text

7.0

Slide 41

Slide 41 text

BC is totes amaze

Slide 42

Slide 42 text

PHP 7.0 enlightenment The path to 7.0 5.6

Slide 43

Slide 43 text

PHP 7.0 enlightenment The path to 7.0 5.5

Slide 44

Slide 44 text

PHP 7.0 enlightenment The path to 7.0 5.4

Slide 45

Slide 45 text

PHP 7.0 enlightenment The path to 7.0 >= 5.3

Slide 46

Slide 46 text

THREE Step @SammyK #zendcon2016 joind.in/talk/6d127

Slide 47

Slide 47 text

Fix all the PHP 7 errors @SammyK #zendcon2016 joind.in/talk/6d127

Slide 48

Slide 48 text

PHP 7.0 Quick tricks to refactor to (in order of ease) @SammyK #zendcon2016 joind.in/talk/6d127

Slide 49

Slide 49 text

PHP 7.0 “Gotchas” ASP and script PHP tags removed <% %> <%= %>

Slide 50

Slide 50 text

PHP 7.0 “Gotchas” removed php://input $HTTP_RAW_POST_DATA instead read from $HTTP_RAW_POST_DATA = file_get_contents('php://input');

Slide 51

Slide 51 text

PHP 7.0 “Gotchas” PHP-4-style constructors deprecated class foo { function foo() {} }

Slide 52

Slide 52 text

PHP 7.0 “Gotchas” PHP-4-style constructors deprecated class foo { function __construct() {} }

Slide 53

Slide 53 text

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

Slide 54

Slide 54 text

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

Slide 55

Slide 55 text

PHP 7.0 “Gotchas” “There’s a shim for that!” ereg* functions bbrala/php7-ereg-shim eregi($pattern, $sub, &$match) preg_match('/'.$pattern.'/i', $sub, $match)

Slide 56

Slide 56 text

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

Slide 57

Slide 57 text

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

Slide 58

Slide 58 text

PHP 7.0 “Gotchas” Caveats though… ext/mysql functions dshafik/php7-mysql-shim $con = mysql_connect(…); $connected = is_resource($con);

Slide 59

Slide 59 text

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

Slide 60

Slide 60 text

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

Slide 61

Slide 61 text

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

Slide 62

Slide 62 text

No content

Slide 63

Slide 63 text

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

Slide 64

Slide 64 text

PHP 7.0 “Gotchas” Uniform Variable Syntax $$foo['bar']['baz']

Slide 65

Slide 65 text

PHP 7.0 “Gotchas” Uniform Variable Syntax $foo = ['bar' => ['baz' => ‘apple']]; $apple = 'PHP 5.x I see...'; var_dump($$foo['bar']['baz']); string(16) "PHP 5.x I see..." 5.x

Slide 66

Slide 66 text

PHP 7.0 “Gotchas” Uniform Variable Syntax Notice: Array to string conversion in / foo.php on line 6 Notice: Undefined variable: Array in / foo.php on line 6 7.x

Slide 67

Slide 67 text

PHP 7.0 “Gotchas” Uniform Variable Syntax $$foo['bar']['baz'] 5.x

Slide 68

Slide 68 text

PHP 7.0 “Gotchas” Uniform Variable Syntax $$foo['bar']['baz'] 5.x

Slide 69

Slide 69 text

PHP 7.0 “Gotchas” Uniform Variable Syntax ${'apple'} 5.x

Slide 70

Slide 70 text

PHP 7.0 “Gotchas” Uniform Variable Syntax $apple 5.x string(16) "PHP 5.x I see..."

Slide 71

Slide 71 text

PHP 7.0 “Gotchas” Uniform Variable Syntax $$foo['bar']['baz'] 7.x

Slide 72

Slide 72 text

PHP 7.0 “Gotchas” Uniform Variable Syntax $$foo['bar']['baz'] 7.x

Slide 73

Slide 73 text

PHP 7.0 “Gotchas” Uniform Variable Syntax ${['bar' => ['baz' => ‘apple']} [‘bar’]['baz'] 7.x

Slide 74

Slide 74 text

PHP 7.0 “Gotchas” Uniform Variable Syntax ${‘Array’}[‘bar’]['baz'] 7.x

Slide 75

Slide 75 text

PHP 7.0 “Gotchas” Uniform Variable Syntax $$foo['bar']['baz'] 7.x

Slide 76

Slide 76 text

PHP 7.0 “Gotchas” Uniform Variable Syntax ${$foo[‘bar’]['baz']} 7.x

Slide 77

Slide 77 text

PHP 7.0 “Gotchas” $$foo['bar']['baz'] ${$foo['bar']['baz']} ($$foo)['bar']['baz'] 5.x 7.x Uniform Variable Syntax

Slide 78

Slide 78 text

FOUR Step @SammyK #zendcon2016 joind.in/talk/6d127

Slide 79

Slide 79 text

Code! @SammyK #zendcon2016 joind.in/talk/6d127

Slide 80

Slide 80 text

Tests TESTS Tests @SammyK #zendcon2016 joind.in/talk/6d127

Slide 81

Slide 81 text

PHPUnit? @SammyK #zendcon2016 joind.in/talk/6d127

Slide 82

Slide 82 text

Acceptance Tests @SammyK #zendcon2016 joind.in/talk/6d127

Slide 83

Slide 83 text

Codeception @SammyK #zendcon2016 joind.in/talk/6d127

Slide 84

Slide 84 text

Behavior-driven testing framework @SammyK #zendcon2016 joind.in/talk/6d127

Slide 85

Slide 85 text

Composer + Autoloading @SammyK #zendcon2016 joind.in/talk/6d127

Slide 86

Slide 86 text

@SammyK #zendcon2016 joind.in/talk/6d127

Slide 87

Slide 87 text

Feature (User Story)

Slide 88

Slide 88 text

Scenario

Slide 89

Slide 89 text

@SammyK #zendcon2016 joind.in/talk/6d127

Slide 90

Slide 90 text

Scenario @SammyK #zendcon2016 joind.in/talk/6d127

Slide 91

Slide 91 text

No content

Slide 92

Slide 92 text

FIVE Step “The Refactoring Loop” @SammyK #zendcon2016 joind.in/talk/6d127

Slide 93

Slide 93 text

refactorLoop: @SammyK #zendcon2016 joind.in/talk/6d127

Slide 94

Slide 94 text

Start small @SammyK #zendcon2016 joind.in/talk/6d127

Slide 95

Slide 95 text

No content

Slide 96

Slide 96 text

Make sure there’s a test @SammyK #zendcon2016 joind.in/talk/6d127

Slide 97

Slide 97 text

@SammyK #zendcon2016 joind.in/talk/6d127 Refactor

Slide 98

Slide 98 text

Run your tests @SammyK #zendcon2016 joind.in/talk/6d127

Slide 99

Slide 99 text

goto refactorLoop; @SammyK #zendcon2016 joind.in/talk/6d127

Slide 100

Slide 100 text

lets you refactor like a BEAST @SammyK #zendcon2016 joind.in/talk/6d127

Slide 101

Slide 101 text

PHPUnit Don’t forget about

Slide 102

Slide 102 text

PHP 7.0 Features @SammyK #zendcon2016 joind.in/talk/6d127

Slide 103

Slide 103 text

random_int() random_bytes() PHP 7.0 Features CSPRNG!! @SammyK #zendcon2016 joind.in/talk/6d127

Slide 104

Slide 104 text

JOURNEY MY CENTER TO THE OF #ShamelessPlug Tomorrow @ 11:30am in Festival 2

Slide 105

Slide 105 text

PHP 7.0 Features Scalar Type Declarations @SammyK #zendcon2016 joind.in/talk/6d127

Slide 106

Slide 106 text

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

Slide 107

Slide 107 text

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

Slide 108

Slide 108 text

99.99999999999942 I got problems but float $income NEVER USE FLOATS FOR MONEY!

Slide 109

Slide 109 text

PHP 7.0 Features Return type declarations @SammyK #zendcon2016 joind.in/talk/6d127

Slide 110

Slide 110 text

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

Slide 111

Slide 111 text

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

Slide 112

Slide 112 text

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

Slide 113

Slide 113 text

PHP 7.0 Features Null coalesce operator @SammyK #zendcon2016 joind.in/talk/6d127

Slide 114

Slide 114 text

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

Slide 115

Slide 115 text

PHP 7.0 Features Insane speed improvements @SammyK #zendcon2016 joind.in/talk/6d127

Slide 116

Slide 116 text

And beyond? @SammyK #zendcon2016 joind.in/talk/6d127

Slide 117

Slide 117 text

PHP 7.1 Considering RC3 @SammyK #zendcon2016 joind.in/talk/6d127

Slide 118

Slide 118 text

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

Slide 119

Slide 119 text

Void return types PHP 7.1 considerations @SammyK #zendcon2016 joind.in/talk/6d127

Slide 120

Slide 120 text

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

Slide 121

Slide 121 text

Nullable types PHP 7.1 considerations @SammyK #zendcon2016 joind.in/talk/6d127 (kinda like union types for null)

Slide 122

Slide 122 text

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

Slide 123

Slide 123 text

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

Slide 124

Slide 124 text

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

Slide 125

Slide 125 text

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

Slide 126

Slide 126 text

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

Slide 127

Slide 127 text

Class constant visibility PHP 7.1 considerations @SammyK #zendcon2016 joind.in/talk/6d127

Slide 128

Slide 128 text

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

Slide 129

Slide 129 text

`iterable` pseudo-type PHP 7.1 considerations @SammyK #zendcon2016 joind.in/talk/6d127

Slide 130

Slide 130 text

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

Slide 131

Slide 131 text

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

Slide 132

Slide 132 text

Some tools I like @SammyK #zendcon2016 joind.in/talk/6d127

Slide 133

Slide 133 text

vlucas/phpdotenv .env No peeking! No peeking!

Slide 134

Slide 134 text

Some tools I like pimple/pimple @SammyK #zendcon2016 joind.in/talk/6d127

Slide 135

Slide 135 text

Some tools I like monolog/monolog @SammyK #zendcon2016 joind.in/talk/6d127

Slide 136

Slide 136 text

Some tools I like nikic/fast-route @SammyK #zendcon2016 joind.in/talk/6d127

Slide 137

Slide 137 text

Some tools I like illuminate/database @SammyK #zendcon2016 joind.in/talk/6d127

Slide 138

Slide 138 text

Some tools I like symfony/console @SammyK #zendcon2016 joind.in/talk/6d127

Slide 139

Slide 139 text

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

Slide 140

Slide 140 text

Big-picture Refactoring @SammyK #zendcon2016 joind.in/talk/6d127

Slide 141

Slide 141 text

Constant refactoring is the key to managing legacy codebases @SammyK #zendcon2016 joind.in/talk/6d127

Slide 142

Slide 142 text

Constant refactoring is the key to managing legacy codebases @SammyK #zendcon2016 joind.in/talk/6d127

Slide 143

Slide 143 text

Resources 013: TDD & BDD In PHP Soon: Acceptance Testing with Behat 017: Modernizing Legacy Codebases in PHP November 3rd @ 12:00PM CDT

Slide 144

Slide 144 text

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

Slide 145

Slide 145 text

Refactoring Martin Fowler http://martinfowler.com/books/refactoring.html @SammyK #zendcon2016 joind.in/talk/6d127

Slide 146

Slide 146 text

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