Slide 1

Slide 1 text

Modernising the Legacy Marek Matulka UK

Slide 2

Slide 2 text

Marek Matulka Software Engineer SensioLabs UK @super_marek

Slide 3

Slide 3 text

Legacy

Slide 4

Slide 4 text

What is legacy code? - someone else’s code - your old code - an old framework you don’t know - or too coupled to the framework - built over years

Slide 5

Slide 5 text

What is legacy code? Old code where... - most of it is procedural - no namespaces used - no code convention - no separation of concerns

Slide 6

Slide 6 text

What is legacy code? Old code where... - PHP mixed with HTML, CSS - overused Global variables - collection of include files - *.inc files in public folder

Slide 7

Slide 7 text

What is legacy code? Old code where... - no concept of environments - no error reporting - no logging - no version control system used

Slide 8

Slide 8 text

What is legacy code? Any code... - without tests

Slide 9

Slide 9 text

It’s too hard - understand the logic - add new features - debug - fix bugs - adopt new technologies

Slide 10

Slide 10 text

How do we get out of legacy?

Slide 11

Slide 11 text

Start with the goal

Slide 12

Slide 12 text

Why? Business goal

Slide 13

Slide 13 text

Why? Who? Who? Actors

Slide 14

Slide 14 text

Why? Who? Who? How? How? How? Impacts

Slide 15

Slide 15 text

Why? Who? Who? How? How? How? What? What? What? What? What? What? Deliverables

Slide 16

Slide 16 text

Why? Who? Who? How? How? How? What? What? What? What? What? What? Prioritise

Slide 17

Slide 17 text

Goal: Reduce drop-outs by 10% In order to see the total cost of order As a shopper I want to see postage cost Example Why? Who? How? What?

Slide 18

Slide 18 text

Write scenarios Feature: Shopper can see postage cost In order to see the total cost of order As a shopper I want to see a postage cost Scenario: Shopper can see a postage cost Given I have added a "Clean Code" book priced £34.50 to the basket And the postage cost for one book is £3.50 When I check the basket Then I should see the postage cost of £3.50 And the total of the basket should be £38.00

Slide 19

Slide 19 text

Implement scenarios

Slide 20

Slide 20 text

Given-When-Then Feature: Shopper can see postage cost In order to see the total cost of order As a shopper I want to see a postage cost Scenario: Shopper can see a postage cost Given I have added a "Clean Code" book priced £34.50 to the basket And the postage cost for one book is £3.50 When I check the basket Then I should see the postage cost of £3.50 And the total of the basket should be £38.00

Slide 21

Slide 21 text

Write a failing scenario (feature)

Slide 22

Slide 22 text

Write a failing scenario (feature) Write a failing test

Slide 23

Slide 23 text

Write a failing scenario (feature) Write a failing test Make your test pass

Slide 24

Slide 24 text

Write a failing scenario (feature) Write a failing test Make your test pass Refactor your code

Slide 25

Slide 25 text

Write a failing scenario (feature) Write a failing test Make your test pass Refactor your code Repeat the fail-pass-refactor cycle as necessary

Slide 26

Slide 26 text

Write a failing scenario (feature) Write a failing test Make your test pass Refactor your code Make your scenario pass

Slide 27

Slide 27 text

Write a failing scenario (feature) Write a failing test Make your test pass Refactor your code Make your scenario pass BDD TDD

Slide 28

Slide 28 text

Write a failing scenario (feature) Write a failing test Make your test pass Refactor your code Make your scenario pass BDD TDD External Quality Internal Quality

Slide 29

Slide 29 text

(everything?) What to test?

Slide 30

Slide 30 text

Unit tests Acceptance tests Integration tests Smoke tests

Slide 31

Slide 31 text

Unit tests Acceptance tests Integration tests Smoke tests Manual Automated

Slide 32

Slide 32 text

Unit tests Acceptance tests Integration tests Smoke tests Slowest Fastest

Slide 33

Slide 33 text

Why test?

Slide 34

Slide 34 text

Why test? - code level documentation - features described through examples - clean code - you’re not afraid to change your code - peace of mind

Slide 35

Slide 35 text

Why test? delivered features project’s life

Slide 36

Slide 36 text

Why use Symfony components?

Slide 37

Slide 37 text

Symfony Components The standard foundation on which the best PHP applications are built. - fully tested - decoupled - reusable - de-facto standard

Slide 38

Slide 38 text

Symfony Components Build a micro framework. Use components you need in your application.

Slide 39

Slide 39 text

symfony/dependency-injection Dependency Injection

Slide 40

Slide 40 text

Dependency Injection In software engineering, dependency injection is a software design pattern that implements inversion of control for resolving dependencies. Dependency injection means giving an object its instance variables. Really. That's it. https://en.wikipedia.org/wiki/Dependency_injection

Slide 41

Slide 41 text

Dependency Injection PostageCalculator PostageRepository

Slide 42

Slide 42 text

Dependency Inversion PostageCalculator PostageRepository

Slide 43

Slide 43 text

Dependency Inversion PostageCalculator PostageRepository PostageSqliteRepository

Slide 44

Slide 44 text

Dependency Inversion PostageCalculator PostageRepository PostageSqliteRepository PostageDoctrineRepository

Slide 45

Slide 45 text

Install dependency injection $ composer require symfony/dependency-injection $ composer require symfony/config

Slide 46

Slide 46 text

Create container.php load('services.yml'); $container->compile();

Slide 47

Slide 47 text

Create container.php load('services.yml'); $container->compile();

Slide 48

Slide 48 text

Create container.php load('services.yml'); $container->compile();

Slide 49

Slide 49 text

Create container.php load('services.yml'); $container->compile();

Slide 50

Slide 50 text

Create container.php load('services.yml'); $container->compile();

Slide 51

Slide 51 text

Create container.php load('services.yml'); $container->compile();

Slide 52

Slide 52 text

# app/config/services.yml services: acme.postage.repository: class: Acme\Infrastructure\Sqlite\PostageSqliteRepository acme.postage.calculator: class: Acme\Basket\PostageCalculator arguments: - @acme.postage.repository Create services.yml

Slide 53

Slide 53 text

# app/config/services.yml services: acme.postage.repository: class: Acme\Infrastructure\Sqlite\PostageSqliteRepository acme.postage.calculator: class: Acme\Basket\PostageCalculator arguments: - @acme.postage.repository Create services.yml

Slide 54

Slide 54 text

# app/config/services.yml services: acme.postage.repository: class: Acme\Infrastructure\Sqlite\PostageSqliteRepository acme.postage.calculator: class: Acme\Basket\PostageCalculator arguments: - @acme.postage.repository Create services.yml

Slide 55

Slide 55 text

Use container require '../_inc/db.php'; require '../app/container.php'; // ... $postageCalculator = $container->get('acme.postage.calculator'); if ($postage = $postageCalculator->calculateForBasket($_SESSION['basket'])) { $postage = $postage->toView(); echo sprintf("

Estimated postage for %d item(s): £%1.02f.

", $postage['quantity'], $postage['price'] ); } // ...

Slide 56

Slide 56 text

Use container require '../_inc/db.php'; require '../app/container.php'; // ... $postageCalculator = $container->get('acme.postage.calculator'); if ($postage = $postageCalculator->calculateForBasket($_SESSION['basket'])) { $postage = $postage->toView(); echo sprintf("

Estimated postage for %d item(s): £%1.02f.

", $postage['quantity'], $postage['price'] ); } // ...

Slide 57

Slide 57 text

Use container require '../_inc/db.php'; require '../app/container.php'; // ... $postageCalculator = $container->get('acme.postage.calculator'); if ($postage = $postageCalculator->calculateForBasket($_SESSION['basket'])) { $postage = $postage->toView(); echo sprintf("

Estimated postage for %d item(s): £%1.02f.

", $postage['quantity'], $postage['price'] ); } // ...

Slide 58

Slide 58 text

Dependency Injection with Drupal 7?

Slide 59

Slide 59 text

container.php load('services.yml'); $container->compile(); $conf['di_container'] = $container;

Slide 60

Slide 60 text

container.php load('services.yml'); $container->compile(); $conf['di_container'] = $container;

Slide 61

Slide 61 text

depot_locator.module get('acme.depot_locator')->locateDepot($postcode); $depot = $depotEntity->toArray(); set_depot_cookies($depot); return ['depot' => $depot]; }

Slide 62

Slide 62 text

twig/twig Twig

Slide 63

Slide 63 text

What is twig?

Slide 64

Slide 64 text

Install twig $ composer require twig/twig

Slide 65

Slide 65 text

How to use twig? $loader = new Twig_Loader_Filesystem('/path/to/templates'); $twig = new Twig_Environment($loader); echo $twig->render('index.html', ['variable' => 'value']);

Slide 66

Slide 66 text

# app/config/services.yml parameters: twig.paths: - app/Resources/views services: twig.template_loader: class: Twig_Loader_Filesystem arguments: - %twig.paths% twig.renderer: class: Twig_Environment arguments: - @twig.template_loader Enabling twig in DI container

Slide 67

Slide 67 text

# app/config/services.yml parameters: twig.paths: - app/Resources/views services: twig.template_loader: class: Twig_Loader_Filesystem arguments: - %twig.paths% twig.renderer: class: Twig_Environment arguments: - @twig.template_loader Enabling twig in DI container

Slide 68

Slide 68 text

# app/config/services.yml parameters: twig.paths: - app/Resources/views services: twig.template_loader: class: Twig_Loader_Filesystem arguments: - %twig.paths% twig.renderer: class: Twig_Environment arguments: - @twig.template_loader Enabling twig in DI container

Slide 69

Slide 69 text

# app/config/services.yml parameters: twig.paths: - app/Resources/views services: twig.template_loader: class: Twig_Loader_Filesystem arguments: - %twig.paths% twig.renderer: class: Twig_Environment arguments: - @twig.template_loader Enabling twig in DI container

Slide 70

Slide 70 text

Use twig $basketViewData = $container->get('acme.basket.view') ->toView($_SESSION['basket']); $basketView = $container->get('twig.renderer') ->render( 'basket.twig.html', $basketViewData ); echo $basketView;

Slide 71

Slide 71 text

symfony/console Add console commands

Slide 72

Slide 72 text

Install console component $ composer require symfony/console

Slide 73

Slide 73 text

class ImportAndPersistCommand extends Command { private $action; public function __construct(ImportAndPersistActionHandler $action) { $this->action = $action; parent::__construct(); } protected function configure() { $this->setName('products:import'); } protected function execute(InputInterface $input, OutputInterface $output) { $startTime = microtime(true); $this->action->importAndPersistProducts(); $executionTime = microtime(true) - $startTime; $output->writeln( sprintf("Products import completed in %1.02fs.", $executionTime) ); } }

Slide 74

Slide 74 text

app/console #!/usr/bin/php addCommands([ $container->get('acme.command.import_products'), $container->get('acme.command.delete_products'), $container->get('acme.command.depot_locate'), ]); $application->run();

Slide 75

Slide 75 text

symfony/http-foundation Decouple from global state

Slide 76

Slide 76 text

Http application’s flow Controller Request Response

Slide 77

Slide 77 text

Install HttpFoundation component $ composer require symfony/http-foundation

Slide 78

Slide 78 text

De-coupling from global state # /public/index.php get('acme.basket_view.controller') ->viewAction($request); $response->send();

Slide 79

Slide 79 text

De-coupling from global state # /public/index.php get('acme.basket_view.controller') ->viewAction($request); $response->send();

Slide 80

Slide 80 text

De-coupling from global state # /public/index.php get('acme.basket_view.controller') ->viewAction($request); $response->send();

Slide 81

Slide 81 text

De-coupling from global state # /public/index.php get('acme.basket_view.controller') ->viewAction($request); $response->send();

Slide 82

Slide 82 text

Going forward

Slide 83

Slide 83 text

Try it yourself!

Slide 84

Slide 84 text

Playground https://github.com/mareg/legacy-summercamp-2015 - four exercises: - add dependency injection - add twig - decouple from global state - controller as service

Slide 85

Slide 85 text

Questions? Slides: speakerdeck.com/super_marek Feedback: joind.in/talk/view/14977 Connect: @super_marek