Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Package Development Brussels-PHP

Package Development Brussels-PHP

Intermediate and advanced tips for PHP package development. Laracon EU try-out.

Rates on https://joind.in/talk/view/14898

39eb3f3d313b13f05534e496285040b8?s=128

Hannes Van De Vreken

July 22, 2015
Tweet

Transcript

  1. Package development Laracon EU try-out

  2. Hi, my name is Hannes.

  3. madewithlove.be

  4. None
  5. Package development Intermediate and advanced techniques

  6. What makes packages great?

  7. 1. Beginner steps 2. Intermediate tips 3. Decouple for stability

  8. 1. Beginner steps

  9. None
  10. BEGINNER STEPS Naming my-company/product-sdk

  11. BEGINNER STEPS Setup git - packagist - ci - webhooks

  12. BEGINNER STEPS Versions release often

  13. BEGINNER STEPS Tests/Specs cover your ass

  14. BEGINNER STEPS Documentation first introduction

  15. BEGINNER STEPS - README.MD Code samples copy-paste

  16. BEGINNER STEPS - README.MD Badges stable version - code coverage

    - …
  17. BEGINNER STEPS - DOCUMENTATION License no license is the worst

  18. BEGINNER STEPS - DOCUMENTATION Changelog like releases: do it often

  19. - Naming - Setup github, packagist & ci - Version

    your code - Write tests BEGINNER STEPS - RECAP
  20. - README - Code samples - Badges (stable, coverage, …)

    - License - Changelog BEGINNER STEPS - RECAP
  21. BEGINNER STEPS - HINT jonathantorres/construct

  22. BEGINNER STEPS - HINT $ vendor/bin/construct generate --help Arguments: name

    The vendor/project name Options: --test (-t) Testing framework (default: "phpunit") --license (-l) License (default: "MIT") --namespace (-s) Namespace for project (default: "Vendor\\Pro --git (-g) Initialize an empty Git repo --phpcs (-p) Generate a PHP Coding Standards Fixer config --keywords (-k) Comma separated list of Composer keywords --vagrant Generate a Vagrantfile
  23. 2. Intermediate tips

  24. INTERMEDIATE TIPS Keep your tests out of other peoples production

  25. INTERMEDIATE TIPS - CLEAN DISTS .gitattributes

  26. None
  27. INTERMEDIATE TIPS - CLEAN DISTS prefer-dist export-ignore

  28. INTERMEDIATE TIPS - CLEAN DISTS autoload-dev

  29. INTERMEDIATE TIPS - CLEAN DISTS "autoload": { "psr-4": { "GuzzleHttp\\":

    "src/" } }, "autoload-dev": { "psr-4": { "GuzzleHttp\\Stubs\\": "tests/stubs/" } }
  30. INTERMEDIATE TIPS Composer scripts stop typing vendor/bin/

  31. INTERMEDIATE TIPS - COMPOSER SCRIPTS #!/usr/bin/env php <?php

  32. INTERMEDIATE TIPS - COMPOSER SCRIPTS "bin": ["construct"] vendor/bin/construct

  33. INTERMEDIATE TIPS - COMPOSER SCRIPTS "scripts": { "test": ["phpunit", "phpspec

    run"], "cs": "php-cs-fixer fix" } $ composer cs
  34. INTERMEDIATE TIPS Code Style

  35. INTERMEDIATE TIPS - CODE STYLE Recommendations: PSR-2 < Symfony

  36. INTERMEDIATE TIPS - CODE STYLE .php-cs config file $ vendor/bin/php-cs-fixer

    fix
  37. INTERMEDIATE TIPS - CODE STYLE Atomic commits easier to review

    w/o code style fixes
  38. INTERMEDIATE TIPS - CODE STYLE Recommendation: CS options for diffs

  39. None
  40. None
  41. None
  42. INTERMEDIATE TIPS - RECAP - only distribute production code -

    composer scripts - code style
  43. 3. Decouple for stability

  44. DECOUPLING Imagine… You’re creating an SDK

  45. DECOUPLING HTTP

  46. DECOUPLING - HTTP EXAMPLE Roll your own when?

  47. None
  48. None
  49. None
  50. None
  51. DECOUPLING - HTTP EXAMPLE Better: Use HTTP Client

  52. DECOUPLING - HTTP EXAMPLE Better: Use Guzzle v6

  53. DECOUPLING - HTTP EXAMPLE “But I’m already using Buzz HTTP

    client”
  54. DECOUPLING - HTTP EXAMPLE “But I’m already using Guzzle v4

    instead of v6”
  55. DECOUPLING - HTTP EXAMPLE Decouple from the implementation

  56. DECOUPLING - HTTP EXAMPLE php-http/adapter-interface

  57. DECOUPLING - HTTP EXAMPLE /** * @param \Http\Adapter\HttpAdapter $adapter */

    public function __construct(HttpAdapter $adapter) { $this->adapter = $adapter; }
  58. DECOUPLING - HTTP EXAMPLE /** * @param \Psr\Http\Message\RequestInterface $request *

    @return \Psr\Http\Message\ResponseInterface */ private function send(RequestInterface $request) { return $this->adapter->send($request); }
  59. DECOUPLING - HTTP EXAMPLE (optional) $client = new \Http\Adapter\Client($adapter) $psrResponse

    = $client->get($url);
  60. DECOUPLING - HTTP EXAMPLE Adapters: Guzzle5 Guzzle6 Mock Zend2 Zend1

    Cake Buzz
  61. DECOUPLING - OTHER CASES - HTTP - Logging - File

    system
  62. DECOUPLING - OTHER CASES Sadly not for caching

  63. DECOUPLING - INTERFACES Decoupling requires interfaces

  64. DECOUPLING - INTERFACES interfaces have implementations

  65. DECOUPLING - INTERFACES concept virtual package is a placeholder to

    be able to define a dependency on some implementation
  66. DECOUPLING - INTERFACES company/sdk requires (1.0.0) company/sdk - composer.json php-http/adapter-implementation

  67. DECOUPLING - INTERFACES php-http/adapter php-http/adapter-implementation provides (1.0.0) php-http/guzzle6-adapter requires (1.0.0)

    php-http/guzzle6-adapter - composer.json
  68. DECOUPLING - INTERFACES application - composer.json company/sdk requires (x.y.z) application

  69. DECOUPLING - INTERFACES php-http/guzzle6-adapter application - composer.json company/sdk requires (x.y.z)

    application requires (1.0.0)
  70. DECOUPLING - BENEFITS Added benefits let people use implementations they

    want
  71. DECOUPLING - BENEFITS Added benefits As package maintainer: more stable

    deps
  72. DECOUPLING - BENEFITS Added benefits No lock-in to a major

    version
  73. DECOUPLING - RECAP Prefer requirements on stable packages

  74. DECOUPLING - RECAP Prefer requirements on virtual packages

  75. DECOUPLING - RECAP Avoid having to follow an instable package’s

    major release cycle
  76. RECAP 1. Beginner steps 2. Intermediate tips 3. Decouple for

    stability
  77. Time for questions

  78. Thank you! @hannesvdvreken

  79. • http:/ /mwl.be REFERENCES