Modernising the Legacy

Modernising the Legacy

No one likes to work with the legacy projects - it's not fun. There are no specs, there are no behat examples and you're afraid to touch it. The customer may not have time or budget to spend on rewriting it from the scratch, but is likely to keep asking for the new features.

Learn how to work with the legacy code, how to add new features without breaking existing ones. Learn to use Symfony components to support building sustainable features.

2bd48651cd01e0ca2e0a255a63da77aa?s=128

Marek Matulka

September 18, 2015
Tweet

Transcript

  1. 3.
  2. 4.

    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
  3. 5.

    What is legacy code? Old code where... - most of

    it is procedural - no namespaces used - no code convention - no separation of concerns
  4. 6.

    What is legacy code? Old code where... - PHP mixed

    with HTML, CSS - overused Global variables - collection of include files - *.inc files in public folder
  5. 7.

    What is legacy code? Old code where... - no concept

    of environments - no error reporting - no logging - no version control system used
  6. 9.

    It’s too hard - understand the logic - add new

    features - debug - fix bugs - adopt new technologies
  7. 17.

    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?
  8. 18.

    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
  9. 20.

    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
  10. 24.
  11. 25.

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

    your test pass Refactor your code Repeat the fail-pass-refactor cycle as necessary
  12. 26.

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

    your test pass Refactor your code Make your scenario pass
  13. 27.

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

    your test pass Refactor your code Make your scenario pass BDD TDD
  14. 28.

    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
  15. 33.
  16. 34.

    Why test? - code level documentation - features described through

    examples - clean code - you’re not afraid to change your code - peace of mind
  17. 37.

    Symfony Components The standard foundation on which the best PHP

    applications are built. - fully tested - decoupled - reusable - de-facto standard
  18. 40.

    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
  19. 46.

    Create container.php <?php // app/container.php use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; use

    Symfony\Component\Config\FileLocator; require __DIR__ . '/../vendor/autoload.php'; $container = new ContainerBuilder(); $loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/app/config')); $loader->load('services.yml'); $container->compile();
  20. 47.

    Create container.php <?php // app/container.php use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; use

    Symfony\Component\Config\FileLocator; require __DIR__ . '/../vendor/autoload.php'; $container = new ContainerBuilder(); $loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/app/config')); $loader->load('services.yml'); $container->compile();
  21. 48.

    Create container.php <?php // app/container.php use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; use

    Symfony\Component\Config\FileLocator; require __DIR__ . '/../vendor/autoload.php'; $container = new ContainerBuilder(); $loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/app/config')); $loader->load('services.yml'); $container->compile();
  22. 49.

    Create container.php <?php // app/container.php use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; use

    Symfony\Component\Config\FileLocator; require __DIR__ . '/../vendor/autoload.php'; $container = new ContainerBuilder(); $loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/app/config')); $loader->load('services.yml'); $container->compile();
  23. 50.

    Create container.php <?php // app/container.php use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; use

    Symfony\Component\Config\FileLocator; require __DIR__ . '/../vendor/autoload.php'; $container = new ContainerBuilder(); $loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/app/config')); $loader->load('services.yml'); $container->compile();
  24. 51.

    Create container.php <?php // app/container.php use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; use

    Symfony\Component\Config\FileLocator; require __DIR__ . '/../vendor/autoload.php'; $container = new ContainerBuilder(); $loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/app/config')); $loader->load('services.yml'); $container->compile();
  25. 55.

    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("<p>Estimated postage for %d item(s): £%1.02f.</p>", $postage['quantity'], $postage['price'] ); } // ...
  26. 56.

    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("<p>Estimated postage for %d item(s): £%1.02f.</p>", $postage['quantity'], $postage['price'] ); } // ...
  27. 57.

    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("<p>Estimated postage for %d item(s): £%1.02f.</p>", $postage['quantity'], $postage['price'] ); } // ...
  28. 59.

    container.php <?php use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; use Symfony\Component\Config\FileLocator; require_once DRUPAL_ROOT

    . '/../vendor/autoload.php'; $container = new ContainerBuilder(); $loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/app/config')); $loader->load('services.yml'); $container->compile(); $conf['di_container'] = $container;
  29. 60.

    container.php <?php use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; use Symfony\Component\Config\FileLocator; require_once DRUPAL_ROOT

    . '/../vendor/autoload.php'; $container = new ContainerBuilder(); $loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/app/config')); $loader->load('services.yml'); $container->compile(); $conf['di_container'] = $container;
  30. 61.

    depot_locator.module <?php function get_depot_json($postcode) { global $conf; $depotEntity = $conf['di_container']

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

    How to use twig? $loader = new Twig_Loader_Filesystem('/path/to/templates'); $twig =

    new Twig_Environment($loader); echo $twig->render('index.html', ['variable' => 'value']);
  32. 66.

    # 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
  33. 67.

    # 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
  34. 68.

    # 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
  35. 69.

    # 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
  36. 73.

    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) ); } }
  37. 74.

    app/console #!/usr/bin/php <?php require_once (__DIR__ . '/../vendor/autoload.php'); require_once (__DIR__ .

    '/app/container.php'); use Symfony\Component\Console\Application; $application = new Application(); $application->addCommands([ $container->get('acme.command.import_products'), $container->get('acme.command.delete_products'), $container->get('acme.command.depot_locate'), ]); $application->run();
  38. 78.

    De-coupling from global state # /public/index.php <?php require __DIR__ .

    '/../app/container.php'; use Symfony\Component\HttpFoundation\Request; $request = Request::createFromGlobals(); $response = $container->get('acme.basket_view.controller') ->viewAction($request); $response->send();
  39. 79.

    De-coupling from global state # /public/index.php <?php require __DIR__ .

    '/../app/container.php'; use Symfony\Component\HttpFoundation\Request; $request = Request::createFromGlobals(); $response = $container->get('acme.basket_view.controller') ->viewAction($request); $response->send();
  40. 80.

    De-coupling from global state # /public/index.php <?php require __DIR__ .

    '/../app/container.php'; use Symfony\Component\HttpFoundation\Request; $request = Request::createFromGlobals(); $response = $container->get('acme.basket_view.controller') ->viewAction($request); $response->send();
  41. 81.

    De-coupling from global state # /public/index.php <?php require __DIR__ .

    '/../app/container.php'; use Symfony\Component\HttpFoundation\Request; $request = Request::createFromGlobals(); $response = $container->get('acme.basket_view.controller') ->viewAction($request); $response->send();