Upload
aram-baghdasaryan
View
709
Download
12
Tags:
Embed Size (px)
Citation preview
Coding Standards by Aram Baghdasaryan
Coding StandardsPSR-1 & PSR-2
Coding Standards by Aram Baghdasaryan
PHP Standards Recommendation
What is PSR?
Coding Standards by Aram Baghdasaryan
PHP Standards Recommendation
PSR-0 - Autoloader Standard
What is PSR?
Coding Standards by Aram Baghdasaryan
PHP Standards Recommendation
PSR-0 - Autoloader StandardPSR-1 - Basic Coding Standard
What is PSR?
Coding Standards by Aram Baghdasaryan
PHP Standards Recommendation
PSR-0 - Autoloader StandardPSR-1 - Basic Coding StandardPSR-2 - Coding Style Guide
What is PSR?
Coding Standards by Aram Baghdasaryan
PHP Standards Recommendation
PSR-0 - Autoloader StandardPSR-1 - Basic Coding StandardPSR-2 - Coding Style GuidePSR-3 - Logger Interface
What is PSR?
Coding Standards by Aram Baghdasaryan
PHP Standards Recommendation
PSR-0 - Autoloader StandardPSR-1 - Basic Coding StandardPSR-2 - Coding Style GuidePSR-3 - Logger InterfacePSR-4 - Autoloader Standard
What is PSR?
Coding Standards by Aram Baghdasaryan
Single style guide for PHP code that results in uniformly formatted shared code
What it gives to us?
Coding Standards by Aram Baghdasaryan
· Files MUST use only <?php and <?= tags· Files MUST use only UTF-8 without BOM for PHP code.· Files SHOULD either declare symbols or cause side-effects but SHOULD NOT do both
PSR-1 Overview
Coding Standards by Aram Baghdasaryan
Files SHOULD either declare symbols or cause side-effects but SHOULD NOT do both
Coding Standards by Aram Baghdasaryan
// side effect: change ini settingsini_set('error_reporting', E_ALL);
// side effect: loads a fileinclude "file.php";
Files SHOULD either declare symbols or cause side-effects but SHOULD NOT do both
Coding Standards by Aram Baghdasaryan
· Namespaces and classes MUST follow an "autoloading" PSR· 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
PSR-1 Overview
Coding Standards by Aram Baghdasaryan
· Code MUST follow a "coding style guide" PSR-1· Code MUST use 4 spaces for indenting, not tabs· There MUST NOT be a hard limit on line length, the soft limit MUST be 120 characters, lines SHOULD be 80 characters or less
PSR-2 Overview
Coding Standards by Aram Baghdasaryan
· There MUST be one blank line after the namespace declaration, and there MUST be one blank line after the block of use declarations· Opening braces for classes MUST go on the next line, and closing braces MUST go on the next line after the body
PSR-2 Overview
Coding Standards by Aram Baghdasaryan
<?php
namespace App;
use \Vendor\Lib1\Tool;use \Vendor\Lib2\Tool;
class BloBlo { ...}
Coding Standards by Aram Baghdasaryan
· Opening braces for methods MUST go on the next line, and closing braces MUST go on the next line after the body· Visibility MUST be declared on all properties and methods, abstract and final MUST be declared before the visibility, static MUST be declared after the visibility
PSR-2 Overview
Coding Standards by Aram Baghdasaryan
final public static function getBlo(){ ...}
Coding Standards by Aram Baghdasaryan
· Control structure keywords MUST have one space after them, method and function calls MUST NOT· Opening braces for control structures MUST go on the same line, and closing braces MUST go on the next line after the body
PSR-2 Overview
Coding Standards by Aram Baghdasaryan
· Opening parentheses for control structures MUST NOT have a space after them, and closing parentheses for control structures MUST NOT have a space before
PSR-2 Overview
Coding Standards by Aram Baghdasaryan
function getBlo($param) use ($other){ if (true) { ... }}
getBlo(‘value’);
Coding Standards by Aram Baghdasaryan
Other Examples
Coding Standards by Aram Baghdasaryan
function aLongMethodName( ClassTypeHint $arg1, &$arg2, array $arg3 = []) { // method body}
Coding Standards by Aram Baghdasaryan
$foo->bar( $longArgument, $longerArgument, $muchLongerArgument);
Coding Standards by Aram Baghdasaryan
if ($expr1) { // if body} elseif ($expr2) { // elseif body} else { // else body}
Coding Standards by Aram Baghdasaryan
switch ($expr) { case 0: echo 'First case, with a break'; break; case 2: case 3: echo 'Third case'; return; default: echo 'Default case';}
Coding Standards by Aram Baghdasaryan
while ($expr) { // structure body}
do { // structure body;} while ($expr);
Coding Standards by Aram Baghdasaryan
for ($i = 0; $i < 10; $i++) { // for body}
foreach ($iterable as $key => $value) { // foreach body}
Coding Standards by Aram Baghdasaryan
try { // try body} catch (FirstExceptionType $e) { // catch body} catch (OtherExceptionType $e) { // catch body}
Coding Standards by Aram Baghdasaryan
$closure = function ($arg1, $arg2) { // body};
$closure = function ($arg1) use ($var1) { // body};
Coding Standards by Aram Baghdasaryan
Thank You!