30
Coding Standards by Aram Baghdasaryan Coding Standards PSR-1 & PSR-2

Coding standards PSR-1 & PSR-2

Embed Size (px)

Citation preview

Page 1: Coding standards PSR-1 & PSR-2

Coding Standards by Aram Baghdasaryan

Coding StandardsPSR-1 & PSR-2

Page 2: Coding standards PSR-1 & PSR-2

Coding Standards by Aram Baghdasaryan

PHP Standards Recommendation

What is PSR?

Page 3: Coding standards PSR-1 & PSR-2

Coding Standards by Aram Baghdasaryan

PHP Standards Recommendation

PSR-0 - Autoloader Standard

What is PSR?

Page 4: Coding standards PSR-1 & PSR-2

Coding Standards by Aram Baghdasaryan

PHP Standards Recommendation

PSR-0 - Autoloader StandardPSR-1 - Basic Coding Standard

What is PSR?

Page 5: Coding standards PSR-1 & PSR-2

Coding Standards by Aram Baghdasaryan

PHP Standards Recommendation

PSR-0 - Autoloader StandardPSR-1 - Basic Coding StandardPSR-2 - Coding Style Guide

What is PSR?

Page 6: Coding standards PSR-1 & PSR-2

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?

Page 7: Coding standards PSR-1 & PSR-2

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?

Page 8: Coding standards PSR-1 & PSR-2

Coding Standards by Aram Baghdasaryan

Single style guide for PHP code that results in uniformly formatted shared code

What it gives to us?

Page 9: Coding standards PSR-1 & PSR-2

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

Page 10: Coding standards PSR-1 & PSR-2

Coding Standards by Aram Baghdasaryan

Files SHOULD either declare symbols or cause side-effects but SHOULD NOT do both

Page 11: Coding standards PSR-1 & PSR-2

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

Page 12: Coding standards PSR-1 & PSR-2

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

Page 13: Coding standards PSR-1 & PSR-2

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

Page 14: Coding standards PSR-1 & PSR-2

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

Page 15: Coding standards PSR-1 & PSR-2

Coding Standards by Aram Baghdasaryan

<?php

namespace App;

use \Vendor\Lib1\Tool;use \Vendor\Lib2\Tool;

class BloBlo { ...}

Page 16: Coding standards PSR-1 & PSR-2

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

Page 17: Coding standards PSR-1 & PSR-2

Coding Standards by Aram Baghdasaryan

final public static function getBlo(){ ...}

Page 18: Coding standards PSR-1 & PSR-2

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

Page 19: Coding standards PSR-1 & PSR-2

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

Page 20: Coding standards PSR-1 & PSR-2

Coding Standards by Aram Baghdasaryan

function getBlo($param) use ($other){ if (true) { ... }}

getBlo(‘value’);

Page 21: Coding standards PSR-1 & PSR-2

Coding Standards by Aram Baghdasaryan

Other Examples

Page 22: Coding standards PSR-1 & PSR-2

Coding Standards by Aram Baghdasaryan

function aLongMethodName( ClassTypeHint $arg1, &$arg2, array $arg3 = []) { // method body}

Page 23: Coding standards PSR-1 & PSR-2

Coding Standards by Aram Baghdasaryan

$foo->bar( $longArgument, $longerArgument, $muchLongerArgument);

Page 24: Coding standards PSR-1 & PSR-2

Coding Standards by Aram Baghdasaryan

if ($expr1) { // if body} elseif ($expr2) { // elseif body} else { // else body}

Page 25: Coding standards PSR-1 & PSR-2

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';}

Page 26: Coding standards PSR-1 & PSR-2

Coding Standards by Aram Baghdasaryan

while ($expr) { // structure body}

do { // structure body;} while ($expr);

Page 27: Coding standards PSR-1 & PSR-2

Coding Standards by Aram Baghdasaryan

for ($i = 0; $i < 10; $i++) { // for body}

foreach ($iterable as $key => $value) { // foreach body}

Page 28: Coding standards PSR-1 & PSR-2

Coding Standards by Aram Baghdasaryan

try { // try body} catch (FirstExceptionType $e) { // catch body} catch (OtherExceptionType $e) { // catch body}

Page 29: Coding standards PSR-1 & PSR-2

Coding Standards by Aram Baghdasaryan

$closure = function ($arg1, $arg2) { // body};

$closure = function ($arg1) use ($var1) { // body};

Page 30: Coding standards PSR-1 & PSR-2

Coding Standards by Aram Baghdasaryan

Thank You!