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

Brief Intro PSR

Brief Intro PSR

Vyacheslav Voronenko

October 16, 2013
Tweet

More Decks by Vyacheslav Voronenko

Other Decks in Programming

Transcript

  1. PHP-FIG - http://www.php-fig.org/ The FIG stands for Framework Interoperability Group.

    The idea behind the group is for project representatives to talk about the commonalities between projects and find ways they can work together.
  2. Member Projects (some) AWS SDK for PHP (Amazon Web Services),

    CakePHP, Composer and Packagist, Doctrine, Drupal, Joomla, Laravel, PEAR, phpBB, phpDocumentor, SugarCRM, Symfony2, Yii framework, Zend Framework 2
  3. Standards so far PSR-0 - http://www.php-fig.org/psr/0/ Aims to provide a

    standard file, class and namespace convention to allow plug-and-play code. PSR-1 - http://www.php-fig.org/psr/1/ Aims to ensure a high level of technical interoperability between shared PHP code. PSR-2 - http://www.php-fig.org/psr/2/ Provides a Coding Style Guide for projects looking to standardize their code. PSR-3 - http://www.php-fig.org/psr/3/ Describes a common interface for logging libraries.
  4. PSR-0 – Autoloading standard Mandatory •A fully-qualified namespace and class

    must have the following structure \<Vendor Name>\(<Namespace>\)*<Class Name> •Each namespace must have a top-level namespace ("Vendor Name"). •Each namespace can have as many sub-namespaces as it wishes. •Each namespace separator is converted to a DIRECTORY_SEPARATOR when loading from the file system. •Each _ character in the CLASS NAME is converted to a DIRECTORY_SEPARATOR. The _ character has no special meaning in the namespace. •The fully-qualified namespace and class is suffixed with .php when loading from the file system. •Alphabetic characters in vendor names, namespaces, and class names may be of any combination of lower case and upper case. Examples \Doctrine\Common\IsolatedClassLoader => /path/to/project/lib/vendor/Doctrine/Common/IsolatedClassLoader.php \Symfony\Core\Request => /path/to/project/lib/vendor/Symfony/Core/Request.php \Zend\Acl => /path/to/project/lib/vendor/Zend/Acl.php \Zend\Mail\Message => /path/to/project/lib/vendor/Zend/Mail/Message.php
  5. PSR-0 – Autoloading standard Underscores in Namespaces and Class Names

    \namespace\package\Class_Name => /path/to/project/lib/vendor/namespace/package/Class/Name.php \namespace\package_name\Class_Name => /path/to/project/lib/vendor/namespace/package_name/Class/Name.php The standards set here should be the lowest common denominator for painless autoloader interoperability. You can test that you are following these standards by utilizing this sample SplClassLoader implementation which is able to load PHP 5.3 classes. http://gist.github.com/221634
  6. PSR-1 Basic coding standards:Overview Files MUST use only <?php and

    <?= tags. Files MUST use only UTF-8 without BOM for PHP code. Files SHOULD either declare symbols (classes, functions, constants, etc.) or cause side-effects (e.g. generate output, change .ini settings, etc.) but SHOULD NOT do both. Namespaces and classes MUST follow PSR-0. Class names MUST be declared in StudlyCaps. Class constants MUST be declared in all upper case with underscore separators. Method names MUST be declared in camelCase.
  7. PSR-1: Files PHP Tags PHP code MUST use the long

    <?php ?> tags or the short-echo <?= ?> tags; it MUST NOT use the other tag variations. Character Encoding PHP code MUST use only UTF-8 without BOM. Side Effects A file SHOULD declare new symbols (classes, functions, constants, etc.) and cause no other side effects, or it SHOULD execute logic with side effects, but SHOULD NOT do both. The phrase "side effects" means execution of logic not directly related to declaring classes, functions, constants, etc., merely from including the file. "Side effects" include but are not limited to: generating output, explicit use of require or include, connecting to external services, modifying ini settings, emitting errors or exceptions, modifying global or static variables, reading from or writing to a file, and so on.
  8. PSR-1: Namespace and Class Names Namespaces and classes MUST follow

    PSR-0. This means each class is in a file by itself, and is in a namespace of at least one level: a top-level vendor name. Class names MUST be declared in StudlyCaps. Code written for PHP 5.3 and after MUST use formal namespaces.
  9. PSR-1 Class/Interface/Traits Constants, Properties, and Methods Constants Class constants MUST

    be declared in all upper case with underscore separators. For example: Properties Whatever naming convention is used SHOULD be applied consistently within a reasonable scope. That scope may be vendor-level, package-level, class-level, or method-level. Most used: $StudlyCaps, $camelCase, or $under_score property names Methods Method names MUST be declared in camelCase().
  10. PSR-2 extends and expands on PSR-1, the basic coding standard.

    The intent is to reduce cognitive friction (:) when scanning code from different authors. It does so by enumerating a shared set of rules and expectations about how to format PHP code. The style rules herein are derived from commonalities among the various member projects. When various authors collaborate across multiple projects, it helps to have one set of guidelines to be used among all those projects. Thus, the benefit of this guide is not in the rules themselves, but in the sharing of those rules. http://www.php-fig.org/psr/2/
  11. Offtopic: Useful tools PHPMD - PHP Mess Detector What PHPMD

    does is: It takes a given PHP source code base and look for several potential problems within that source. These problems can be things like: Possible bugs Suboptimal code Overcomplicated expressions Unused parameters, methods, properties PHP_CodeSniffer PHP_CodeSniffer tokenises PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.
  12. PHPMD #sudo apt-get install php-pear #sudo apt-get install php5-dev sudo

    pear channel-discover pear.phpmd.org sudo pear channel-discover pear.pdepend.org sudo pear install --alldeps phpmd/PHP_PMD
  13. PSR-3 Logging The main goal is to allow libraries to

    receive a Psr\Log\LoggerInterface object write logs to it in a simple and universal way. This ensures that the third-party libraries an application uses can write to the centralized application logs. Frameworks and CMSs that have custom needs MAY extend the interface for their own purpose, but SHOULD remain compatible with this document.
  14. PSR-3 Basics The LoggerInterface exposes eight methods to write logs

    to the eight RFC 5424 levels (debug, info, notice, warning, error, critical, alert, emergency). A ninth method, log, accepts a log level as first argument. Calling this method with one of the log level constants MUST have the same result as calling the level-specific method. Calling this method with a level not defined by this specification MUST throw a Psr\Log\InvalidArgumentException if the implementation does not know about the level. Users SHOULD NOT use a custom level without knowing for sure the current implementation supports it.
  15. Helper classes and interfaces The Psr\Log\AbstractLogger class lets you implement

    the LoggerInterface very easily by extending it and implementing the generic log method. The other eight methods are forwarding the message and context to it. Similarly, using the Psr\Log\LoggerTrait only requires you to implement the generic log method. Note that since traits can not implement interfaces, in this case you still have to implement LoggerInterface. The Psr\Log\LoggerAwareInterface only contains a setLogger(LoggerInterface $logger) method and can be used by frameworks to auto-wire arbitrary instances with a logger. The Psr\Log\LoggerAwareTrait trait can be used to implement the equivalent interface easily in any class. It gives you access to $this->logger The Psr\Log\LogLevel class holds constants for the eight log levels.
  16. Coming: PSR-4 Extension for PSR-0 The purpose is to specify

    the rules for an interoperable PHP autoloader that maps namespaces to file system paths, and that can co-exist with any other SPL registered autoloader. This would be an addition to, not a replacement for, PSR-0.
  17. Thanks Now we are ready to move forward with composer

    Vyacheslav Voronenko http://github.com/Voronenko