Slide 1

Slide 1 text

PHP: PSRs LIGHT INTRO TO PSR

Slide 2

Slide 2 text

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.

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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.

Slide 5

Slide 5 text

PSR-0 – Autoloading standard Mandatory •A fully-qualified namespace and class must have the following structure \\(\)* •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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

PSR-0 – Basic implementation

Slide 8

Slide 8 text

PSR-1 Basic coding standards:Overview Files MUST use only

Slide 9

Slide 9 text

PSR-1: Files PHP Tags PHP code MUST use the long 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.

Slide 10

Slide 10 text

Examples

Slide 11

Slide 11 text

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.

Slide 12

Slide 12 text

Example

Slide 13

Slide 13 text

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().

Slide 14

Slide 14 text

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/

Slide 15

Slide 15 text

☺ No, I haven’t prepared 20+ slides http://www.php-fig.org/psr/2/

Slide 16

Slide 16 text

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.

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

PHPCS sudo pear install PHP_CodeSniffer

Slide 19

Slide 19 text

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.

Slide 20

Slide 20 text

Github https://github.com/php-fig/log

Slide 21

Slide 21 text

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.

Slide 22

Slide 22 text

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.

Slide 23

Slide 23 text

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.

Slide 24

Slide 24 text

Thanks Now we are ready to move forward with composer Vyacheslav Voronenko http://github.com/Voronenko