throwing laravel into your legacy app™

82
Throwing Laravel into your Legacy™ Application Joe Ferguson

Upload: joe-ferguson

Post on 28-Jan-2018

124 views

Category:

Software


4 download

TRANSCRIPT

Page 1: Throwing Laravel into your Legacy App™

Throwing Laravel into your Legacy™ Application

Joe Ferguson

Page 2: Throwing Laravel into your Legacy App™

Who Am I?

Joe Ferguson

PHP Developer

PHP Architect @ Ministry Brands

Twitter: @JoePFerguson

Organizer of @MemphisPHP

@NomadPHP Lightning Talks

Drone Racing Pilot

Page 3: Throwing Laravel into your Legacy App™

For Further Reading

leanpub.com/mlaphplaravelupandrunning.com

Page 4: Throwing Laravel into your Legacy App™

First: A Warning

Page 5: Throwing Laravel into your Legacy App™

Legacy Applications

Page 6: Throwing Laravel into your Legacy App™

“There are no solutions, only trade offs”

- Joe Ferguson

- Paul M. Jones

- Thomas Sowell

Page 7: Throwing Laravel into your Legacy App™

What is a "Legacy" application

Page 8: Throwing Laravel into your Legacy App™

Legacy is often used for an older production application that was written before, or without regard for modern technologies or practices.

Page 9: Throwing Laravel into your Legacy App™

If you ask Wikipedia…

"... no-longer supported …"

"... maintained by an administrator that did not develop the code …"

"... supporting older file formats ..."

https://en.wikipedia.org/wiki/Legacy_code

Page 10: Throwing Laravel into your Legacy App™

Modern Interpretation

"... source code inherited from someone else …"

"... source code inherited from an older version of the software ..."

Page 11: Throwing Laravel into your Legacy App™

Some people say…

"Any code in production is not legacy”

"Any code written X years ago is legacy"

Page 12: Throwing Laravel into your Legacy App™

Legacy is all these things

As we talk about Legacy code here, we're really talking about all of these definitions

Page 13: Throwing Laravel into your Legacy App™

This process is not Legacy Specific…

Page 14: Throwing Laravel into your Legacy App™

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?

Page 15: Throwing Laravel into your Legacy App™

Is there a framework?

Page 16: Throwing Laravel into your Legacy App™

Is there a coding standard?

Page 17: Throwing Laravel into your Legacy App™

There is a PSR for that…

Page 18: Throwing Laravel into your Legacy App™

How are dependencies behind handled?

Page 19: Throwing Laravel into your Legacy App™

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

Page 20: Throwing Laravel into your Legacy App™

Tools to help

Page 21: Throwing Laravel into your Legacy App™

PHP Coding Standards Fixerhttp://cs.sensiolabs.org/

Page 22: Throwing Laravel into your Legacy App™

PHP Coding Standards Fixerphp-cs-fixer fix app --dry-run

Page 23: Throwing Laravel into your Legacy App™

PHP Mess Detectorhttps://phpmd.org

Page 24: Throwing Laravel into your Legacy App™

PHP Mess Detectorhttps://phpmd.org

$ phpmd app html cleancode --reportfile report.html

Page 25: Throwing Laravel into your Legacy App™

Warning about Automating Code Changes

Page 26: Throwing Laravel into your Legacy App™

Triage

Address any critical things found in Contemplation phase

PHP version issues

Fixing critical vulnerabilities

Page 27: Throwing Laravel into your Legacy App™

“We should just rewrite the app”

Page 28: Throwing Laravel into your Legacy App™

"This would be so much easier in framework _____________”

Page 29: Throwing Laravel into your Legacy App™

Why not rewrite?

Progress halts entirely

Business logic is rarely reused

Most of the time is getting back to current functionality

Page 30: Throwing Laravel into your Legacy App™

Why you should refactor

Progress continues

Business logic reused

Modernizing functionality instead of recreating

Page 31: Throwing Laravel into your Legacy App™

Make Decisions

Build Proof of Concept(s)

Ensure stakeholder buy in

Avoid the allure of green fields**

** in some cases a rewrite is warranted

Refactoring Approach

Page 32: Throwing Laravel into your Legacy App™

Existing Tests

This is great!

Review the tests to get an idea of coverage

Passing tests become your control for any upcoming changes

Confidence++

Page 33: Throwing Laravel into your Legacy App™

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?

Page 34: Throwing Laravel into your Legacy App™

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

Page 35: Throwing Laravel into your Legacy App™

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

Page 36: Throwing Laravel into your Legacy App™

Digging into the Code

Code Consolidation

Page 37: Throwing Laravel into your Legacy App™

Digging into the Code

Code Consolidation

Replace globals with DI

Page 38: Throwing Laravel into your Legacy App™

Digging into the Code

Code Consolidation

Replace globals with Dependency Injection

Write tests

Page 39: Throwing Laravel into your Legacy App™

Digging into the Code

Code Consolidation

Replace globals with Dependency Injection

Write tests

Extract SQL (to ORM or other)

Page 40: Throwing Laravel into your Legacy App™

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)

Page 41: Throwing Laravel into your Legacy App™

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)

Page 42: Throwing Laravel into your Legacy App™

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)

Page 43: Throwing Laravel into your Legacy App™

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

Page 44: Throwing Laravel into your Legacy App™

Continuous Integration

Page 45: Throwing Laravel into your Legacy App™
Page 46: Throwing Laravel into your Legacy App™

Where does Laravel fit in?

Page 47: Throwing Laravel into your Legacy App™

You can easily** use Laravel components in your Legacy App

** Relatively speaking, and not all components

Page 48: Throwing Laravel into your Legacy App™

Laravel Component Goals

Implement Configuration

Implement Logging

Implement Routing

Implement Database

Implement Container

Page 49: Throwing Laravel into your Legacy App™

Configuration

https://github.com/mattstauffer/Torch/tree/master/components/config

Page 50: Throwing Laravel into your Legacy App™

Configuration

https://github.com/mattstauffer/Torch/tree/master/components/config

Page 51: Throwing Laravel into your Legacy App™

Configuration

https://github.com/mattstauffer/Torch/tree/master/components/config

Page 52: Throwing Laravel into your Legacy App™

Configuration

https://github.com/mattstauffer/Torch/tree/master/components/config

Page 53: Throwing Laravel into your Legacy App™

Configuration

https://github.com/mattstauffer/Torch/tree/master/components/config

Page 54: Throwing Laravel into your Legacy App™

What about secrets?

composer require vlucas/phpdotenv

Page 55: Throwing Laravel into your Legacy App™

composer require vlucas/phpdotenv

.env

config/app.php

Page 56: Throwing Laravel into your Legacy App™

Access Config the same way

Page 57: Throwing Laravel into your Legacy App™

Now our config is outside VCS

Page 58: Throwing Laravel into your Legacy App™

Do NOT version control your .env file!

Page 59: Throwing Laravel into your Legacy App™

Logging

https://github.com/mattstauffer/Torch/tree/master/components/log

Page 60: Throwing Laravel into your Legacy App™

Logging

https://github.com/mattstauffer/Torch/tree/master/components/log

Page 61: Throwing Laravel into your Legacy App™

Logging

Page 62: Throwing Laravel into your Legacy App™

Logging

Page 63: Throwing Laravel into your Legacy App™

You’ll likely want to configure your logger into a base class

that is extended

Page 64: Throwing Laravel into your Legacy App™

Routing

https://github.com/mattstauffer/Torch/tree/master/components/routing

Page 65: Throwing Laravel into your Legacy App™

Routing

https://github.com/mattstauffer/Torch/tree/master/components/routing

Page 66: Throwing Laravel into your Legacy App™

Routing

https://github.com/mattstauffer/Torch/tree/master/components/routing

Page 67: Throwing Laravel into your Legacy App™

Routing

https://github.com/mattstauffer/Torch/tree/master/components/routing

Page 68: Throwing Laravel into your Legacy App™

Routing

Page 69: Throwing Laravel into your Legacy App™

Routing

Page 70: Throwing Laravel into your Legacy App™

Database

https://github.com/mattstauffer/Torch/tree/master/components/database

Page 71: Throwing Laravel into your Legacy App™

Database

https://github.com/mattstauffer/Torch/tree/master/components/database

Page 72: Throwing Laravel into your Legacy App™

Database

https://github.com/mattstauffer/Torch/tree/master/components/database

Page 73: Throwing Laravel into your Legacy App™

Database

https://github.com/mattstauffer/Torch/tree/master/components/database

Page 74: Throwing Laravel into your Legacy App™

Container

https://github.com/mattstauffer/Torch/tree/master/components/container

Page 75: Throwing Laravel into your Legacy App™

Container

https://github.com/mattstauffer/Torch/tree/master/components/container

Page 76: Throwing Laravel into your Legacy App™

Container

https://github.com/mattstauffer/Torch/tree/master/components/container

Page 77: Throwing Laravel into your Legacy App™

Container

https://github.com/mattstauffer/Torch/tree/master/components/container

Page 78: Throwing Laravel into your Legacy App™

Container

https://github.com/mattstauffer/Torch/tree/master/components/container

Page 79: Throwing Laravel into your Legacy App™

Container

https://github.com/mattstauffer/Torch/tree/master/components/container

Page 80: Throwing Laravel into your Legacy App™

Containers are very powerful. Make sure you’re not adding

spaghetti code just to shoehorn into your App

Page 81: Throwing Laravel into your Legacy App™

Recap

Follow a refactoring process

Leverage Laravel components as you refactor

Make sure you write tests along the way!

Page 82: Throwing Laravel into your Legacy App™

Joe FergusonTwitter: @JoePFergusonEmail: [email protected]: joepferguson

Contact Info:

https://joind.in/talk/d306e

Feedback: