Slide 1

Slide 1 text

Modernizing Legacy Code Step By Step Joe Ferguson November 15th 2018

Slide 2

Slide 2 text

Who Am I? Joe Ferguson PHP Developer PHP Architect @ Ministry Brands Twitter: @JoePFerguson OSMI Board Member Drone Racing Pilot

Slide 3

Slide 3 text

For Further Reading leanpub.com/mlaphp leanpub.com/minimumviabletests

Slide 4

Slide 4 text

Legacy Applications

Slide 5

Slide 5 text

“There are no solutions, only trade offs” - Joe Ferguson - Paul M. Jones - Thomas Sowell

Slide 6

Slide 6 text

Taking stock of what we’re working with

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

Initial Project Overview Is there a framework? Is there a coding standard? Is there any autoloading? How are dependencies behind handled? Is there an ORM or how is the database being utilized? Is there a development environment?

Slide 11

Slide 11 text

Is there a framework?

Slide 12

Slide 12 text

Is there a framework?

Slide 13

Slide 13 text

Is there a coding standard?

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

Is there a coding standard?

Slide 16

Slide 16 text

No Standard? No problem!

Slide 17

Slide 17 text

Is there any autoloading?

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

Is there any autoloading?

Slide 20

Slide 20 text

There is a PSR for that…

Slide 21

Slide 21 text

How are dependencies handled?

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

Is there an ORM or how is the database being utilized?

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

Tools to help

Slide 26

Slide 26 text

PHP Coding Standards Fixer http://cs.sensiolabs.org/

Slide 27

Slide 27 text

PHP Coding Standards Fixer php-cs-fixer fix app --dry-run

Slide 28

Slide 28 text

PHP Mess Detector https://phpmd.org

Slide 29

Slide 29 text

PHP Mess Detector https://phpmd.org $ phpmd app html cleancode --reportfile report.html

Slide 30

Slide 30 text

Warning about Automating Code Changes

Slide 31

Slide 31 text

Composer https://getcomposer.org/doc/01-basic-usage.md

Slide 32

Slide 32 text

Triage Address any critical things found PHP Short Tags! PHP version issues Current server is 5.2 lets test 7.2 Fixing critical vulnerabilities

Slide 33

Slide 33 text

Get into Git!

Slide 34

Slide 34 text

Get into Git!

Slide 35

Slide 35 text

“We should just rewrite the app”

Slide 36

Slide 36 text

"This would be so much easier in framework _____________”

Slide 37

Slide 37 text

Why not rewrite? Progress halts entirely Business logic is rarely reused Most of the time is getting back to current functionality

Slide 38

Slide 38 text

Why you should refactor Progress continues Business logic reused Modernizing functionality instead of recreating

Slide 39

Slide 39 text

Existing Tests This is great! Review the tests to get an idea of coverage Passing tests become your control for any upcoming changes Confidence++

Slide 40

Slide 40 text

No Existing Tests Not the end of the world! Inspect the code base, was it written to be easily testable? Could you easily mock dependencies?

Slide 41

Slide 41 text

Untestable Code Not all code is testable If you can’t easily unit test the code base, consider alternatives Functional testing and Acceptance testing are valid options

Slide 42

Slide 42 text

Acceptance Testing NOT a replacement for Unit Testing Test large parts of your application at a time Can be harder to pinpoint error location Gives large code coverage in short amount of time

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

Digging into the Code Code Consolidation

Slide 51

Slide 51 text

Code Consolidation

Slide 52

Slide 52 text

Code Consolidation

Slide 53

Slide 53 text

_config_vars.php

Slide 54

Slide 54 text

_db_connect.php

Slide 55

Slide 55 text

composer require vlucas/phpdotenv

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

Database Class

Slide 58

Slide 58 text

Progress

Slide 59

Slide 59 text

header.php

Slide 60

Slide 60 text

Digging into the Code Code Consolidation Replace globals with DI

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

No content

Slide 63

Slide 63 text

No content

Slide 64

Slide 64 text

Digging into the Code Code Consolidation Replace globals with Dependency Injection Write tests

Slide 65

Slide 65 text

No content

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

Digging into the Code Code Consolidation Replace globals with Dependency Injection Write tests Extract SQL (to ORM or other)

Slide 69

Slide 69 text

Digging into the Code Code Consolidation Replace globals with Dependency Injection Write tests Extract SQL (to ORM or other) Extract Business Logic (To domain logic)

Slide 70

Slide 70 text

No content

Slide 71

Slide 71 text

No content

Slide 72

Slide 72 text

_functions.php

Slide 73

Slide 73 text

Weather Class

Slide 74

Slide 74 text

Refactoring index.php

Slide 75

Slide 75 text

header.php

Slide 76

Slide 76 text

Digging into the Code Code Consolidation Replace globals with Dependency Injection Write tests Extract SQL (to ORM or other) Extract Business Logic (To domain logic) Extract Presentation Logic to Views (Twig, Blade, etc)

Slide 77

Slide 77 text

Digging into the Code Code Consolidation Replace globals with Dependency Injection Write tests Extract SQL (to ORM or other) Extract Business Logic (To domain logic) Extract Presentation Logic to Views (Twig, Blade, etc) Extract Action Logic (To Controllers)

Slide 78

Slide 78 text

Digging into the Code Code Consolidation Replace globals with Dependency Injection Write tests Extract SQL (to ORM or other) Extract Business Logic (To domain logic) Extract Presentation Logic to Views (Twig, Blade, etc) Extract Action Logic (To Controllers) Write (more) tests

Slide 79

Slide 79 text

Resources / Q & A Modernizing Legacy Applications In PHP - Paul M. Jones https://leanpub.com/mlaphp Minimum Viable Tests - Chris Hartjes https://leanpub.com/minimumviabletests Code Style https://github.com/FriendsOfPHP/PHP-CS-Fixer, http://editorconfig.org php mess detector https://phpmd.org Autoloading PSR-4 https://getcomposer.org/doc/01-basic-usage.md ORMs http://www.doctrine-project.org http://propelorm.org Docker https://leanpub.com/dockerfordevs, Vagrant https://puphpet.com

Slide 80

Slide 80 text

Joe Ferguson Twitter: @JoePFerguson Email: [email protected] Freenode: joepferguson Contact Info: https://joind.in/talk/4e1b4