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

PSRs: what, why and how?

PSRs: what, why and how?

While increasingly important in our ecosystem, the PHP Standards Recommendations remain too little known and somewhat mystical for many of us. In this talk, I propose to understand what is a PSR and then to briefly review all the existing recommendations, whether validated, accepted or draft. We will also come back to the importance and purpose of these standards as well as their development process.

Julien Janvier

May 20, 2017
Tweet

More Decks by Julien Janvier

Other Decks in Programming

Transcript

  1. PSRs: what,
    why and how?
    ___
    Julien Janvier
    @jujanvier
    https://joind.in/talk/ebe72

    View full-size slide

  2. PHP
    Standard
    Recommendation

    View full-size slide

  3. What is a PSR?

    View full-size slide

  4. accepted
    PSR-1: Basic Coding Standard

    View full-size slide

  5. accepted
    PSR-2: Coding Style Guide

    View full-size slide

  6. draft
    PSR-12: Extended Coding Style Guide

    View full-size slide

  7. draft
    PSR-5: PHPDoc Standard
    /**
    * @method int MyMagicMethod(string $argument1) {
    * This is the summary for MyMagicMethod.
    *
    * @param string $argument1 Description for argument 1.
    *
    * @return int
    * }
    */
    class MyMagicClass
    {
    // ...
    }

    View full-size slide

  8. Who is behind these standards?

    View full-size slide

  9. Framework Interoperability Group
    www.php-fig.org
    www.github.com/php-fig/fig-standards
    @phpfig

    View full-size slide

  10. ...and many more...
    ...and many more...

    View full-size slide

  11. How these standards are written?

    View full-size slide

  12. Member Project
    Project Representative
    Core Committee

    View full-size slide

  13. Working Group =
    Editor + Sponsor +
    Contributors

    View full-size slide

  14. Pre draft >> Draft >>
    Review >> Accepted >>
    Dreprecate

    View full-size slide

  15. PSR-0: Autoloading Standard
    Nelmio\Alice\ => src/
    class Nelmio\Alice\Loader\Yaml
    src/Nelmio/Alice/Loader/Yaml.php
    class Nelmio_Alice_Loader_Yaml
    deprecated

    View full-size slide

  16. PSR-0: Autoloading Standard
    Nelmio\Alice\ => src/
    class Nelmio\Alice\Loader\Yaml
    src/Nelmio/Alice/Loader/Yaml.php
    accepted
    src/Loader/Yaml.php
    class Nelmio_Alice_Loader_Yaml
    PSR-4: Autoloader
    deprecated

    View full-size slide

  17. What prompted the
    FIG to write the first
    standards back in
    2009?

    View full-size slide

  18. What prompted the
    FIG to write the first
    standards back in
    2009?
    Almost impossible to
    reuse others’ code.

    View full-size slide

  19. Security issues

    View full-size slide

  20. draft
    PSR-9: Security Reporting Process

    [email protected]

    View full-size slide

  21. draft
    PSR-10: Security Disclosure Publication

    View full-size slide

  22. Why knowing
    existing standards
    is useful?

    View full-size slide

  23. Why knowing
    existing standards
    is useful?
    Let us take
    advantage of others’
    researches.

    View full-size slide

  24. Day-to-day tools

    View full-size slide

  25. accepted
    PSR-3: Logger Interface

    View full-size slide

  26. accepted
    PSR-3: Logger Interface
    namespace Psr\Log;
    interface LoggerInterface
    {
    /**
    * Action must be taken immediately.
    * Example: Entire website down, database unavailable, etc.
    * This should trigger the SMS alerts and wake you up.
    *
    * @param string $message
    * @param array $context
    *
    * @return void
    */
    public function alert($message, array $context = array());

    View full-size slide

  27. Why a standard
    when there
    is already “one”?

    View full-size slide

  28. Why a standard
    when there
    is already “one”?
    Our needs evolve.

    View full-size slide

  29. accepted
    PSR-11: Container Interface
    namespace Psr\Container;
    interface ContainerInterface
    {
    /**
    * Finds an entry of the container by its identifier and returns it.
    *
    * @param string $id Identifier of the entry to look for.
    *
    * @throws NotFoundExceptionInterface No entry was found for this id.
    * @throws ContainerExceptionInterface Error while retrieving the entry.
    *
    * @return mixed Entry.
    */
    public function get($id);

    View full-size slide

  30. accepted
    PSR-11: Container Interface

    View full-size slide

  31. Why a standard
    when there are
    already so many
    libraries?

    View full-size slide

  32. Why a standard
    when there are
    already so many
    libraries?
    Offer us a world of
    new possibilities.

    View full-size slide

  33. accepted
    PSR-6: Caching Interface
    // $pool is an instance of Psr\Cache\CacheItemPoolInterface
    // $item is an instance of Psr\Cache\CacheItemInterface
    $item = $pool->getItem('foo');
    if (!$item->isHit()) {
    $item->set('bar');
    $item->expiresAfter(3600);
    $pool->save($item);
    }
    $value = $item->get();

    View full-size slide

  34. accepted
    PSR-16: Simple Cache
    // $cache is an instance of Psr\SimpleCache\CacheInterface
    if (null === $cache->get('foo')) {
    $cache->set('foo', 'bar', 3600);
    }
    $value = $cache->get('foo');

    View full-size slide

  35. draft
    PSR-14: Event Manager
    namespace Psr\EventManager;
    interface EventManagerInterface
    {
    public function attach($event, $callback, $priority = 0);
    public function detach($event, $callback);
    public function clearListeners($event);
    public function trigger($event, $target = null, $argv =
    array());
    }
    interface EventInterface
    {
    // ...
    }

    View full-size slide

  36. Why a standard for
    “problems” that
    already have been
    resolved by our
    framework?

    View full-size slide

  37. Why a standard for
    “problems” that
    already have been
    resolved by our
    framework?
    Avoid to couple our
    business code to a
    vendor library.

    View full-size slide

  38. accepted
    PSR-7: HTTP Message Interface
    POST /path HTTP/1.1
    Host: example.com
    foo=bar&baz=bat
    HTTP/1.1 200 OK
    Content-Type: text/plain
    This is the response body

    View full-size slide

  39. accepted
    PSR-7: HTTP Message Interface
    Request Response
    Message
    ServerRequest
    Stream
    Uri
    UploadedFile
    getBody()
    getUploadedFiles()

    View full-size slide

  40. PSR-17: HTTP Factories
    namespace Psr\Http\Message;
    use Psr\Http\Message\StreamInterface;
    interface StreamFactoryInterface
    {
    public function createStream($content = '');
    public function createStreamFromFile($filename, $mode = 'r');
    public function createStreamFromResource($resource);
    }
    review

    View full-size slide

  41. accepted
    PSR-13: Hypermedia Links
    namespace Psr\Link;
    interface LinkInterface
    {
    public function getHref();
    public function isTemplated();
    public function getRels();
    public function getAttributes();
    }

    View full-size slide

  42. What standards
    about so low levels
    topics can bring?

    View full-size slide

  43. What standards
    about so low levels
    topics can bring?
    Maybe it opens the
    gates of PHP native
    implementations.

    View full-size slide

  44. PSR-15: HTTP Middlewares
    Working with Slim Middleware, by Timothy Boronczyk
    vote

    View full-size slide

  45. PSR-15: HTTP Middlewares
    use Psr\Http\ServerMiddleware\DelegateInterface;
    use Psr\Http\ServerMiddleware\MiddlewareInterface;
    use Psr\Http\Message\ServerRequestInterface;
    class CacheMiddleware implements MiddlewareInterface
    {
    public function process(
    ServerRequestInterface $request, DelegateInterface $delegate
    ) {
    if (null === $response = $this->cache->get(/**a key**/)) {
    $response = $delegate->process($request);
    $this->cache->set(/**a key**/, $response);
    }
    return $response;
    }
    vote

    View full-size slide

  46. Why those
    standards?

    View full-size slide

  47. Why those
    standards?
    Promote
    interoperability.

    View full-size slide

  48. PSR-8: Huggable Interface
    draft

    View full-size slide

  49. Only members can vote...

    View full-size slide

  50. ...but everybody can participate :)

    View full-size slide

  51. www.php-fig.org/get-involved/

    View full-size slide

  52. Thanks :)
    ___
    Julien Janvier
    @jujanvier
    https://joind.in/talk/ebe72

    View full-size slide

  53. Webography
    ● FIG / FIG 3.0
    ○ The PHP-FIG: Past, Present & Future - podcast with a few FIG members
    ○ Why PHP-FIG matters - by Evert Pot
    ○ FIG 3.0 Proposal - by Larry Garfield
    ○ The TL;DR of FIG 3.0 - by Michael Cullum
    ○ Michael Cullum Talks About PHP FIG, PSR & Symfony - with Michael Cullum
    ○ Phil Sturgeon Talks About API Development, PHP-FIG, PHP Books And The Future Of PHP -
    with Phil Sturgeon
    ○ PHP FIG: Breaking down the Boundaries - by Michael Cullum (video here)
    ● PSR-0
    ○ Is PSR-0 Shortsighted, or are you? - by Phil Sturgeon
    ○ Composer and PSR-0: Friends, Not Relatives - by Phil Sturgeon

    View full-size slide

  54. Webography
    ● PSR 6
    ○ An Open Letter To PHP-FIG - by Anthony Ferrara
    ○ PSR-6 Has Serious Problems - by Andrew Carter
    ○ PSR-6 Is About To Pass And Still Has Serious Problems - by Andrew Carter
    ○ [VOTE][Accept] PSR-6 Caching Interfaces - by Paul Dragoonis
    ● PSR 9 / PSR 10
    ○ Call for volunteers - by Michael Hess
    ● PSR-11
    ○ [PSR-11+] An overview of interoperable PHP modules - by David Négrier
    ○ [PSR-11+] An update on standard service providers - by David Négrier

    View full-size slide

  55. Webography
    ● PSR-7 / Middlewares
    ○ Two things to know about PSR-7 - by Rob Allen
    ○ Middleware concept - by Slim framework
    ○ Writing PSR-7 middleware - by Rob Allen
    ○ HTTP, PSR-7 and Middleware - by Rob Allen
    ○ PSR-7 And Middleware, The Future Of PHP - by Matthew Weier O'Phinney
    ○ All About Middleware - problems about PRS-7 by Anthony Ferrara
    ○ All About PSR-7 Middleware - answer about Anthony Ferrara’s post by Woody Gilk
    ○ Dependency Inversion and PSR-7 Bodies - by Woody Gilk
    ○ PSR-7 Objects Are Not Immutable - by Andrew Carter
    ○ Working with Slim Middleware - by Timothy Boronczyk
    ● PSR-15 / Middlewares
    ○ Using Anonymous Classes to Write Middleware - by Matthew Weier O'Phinney
    ○ Stack middlewares - by Igor Wiedler

    View full-size slide