O slideshow foi denunciado.
Utilizamos seu perfil e dados de atividades no LinkedIn para personalizar e exibir anúncios mais relevantes. Altere suas preferências de anúncios quando desejar.

Coding standards PSR-1 & PSR-2

10.102 visualizações

Publicada em

Publicada em: Tecnologia
  • Entre para ver os comentários

Coding standards PSR-1 & PSR-2

  1. 1. Coding Standards by Aram Baghdasaryan Coding Standards PSR-1 & PSR-2
  2. 2. Coding Standards by Aram Baghdasaryan PHP Standards Recommendation What is PSR?
  3. 3. Coding Standards by Aram Baghdasaryan PHP Standards Recommendation PSR-0 - Autoloader Standard What is PSR?
  4. 4. Coding Standards by Aram Baghdasaryan PHP Standards Recommendation PSR-0 - Autoloader Standard PSR-1 - Basic Coding Standard What is PSR?
  5. 5. Coding Standards by Aram Baghdasaryan PHP Standards Recommendation PSR-0 - Autoloader Standard PSR-1 - Basic Coding Standard PSR-2 - Coding Style Guide What is PSR?
  6. 6. Coding Standards by Aram Baghdasaryan PHP Standards Recommendation PSR-0 - Autoloader Standard PSR-1 - Basic Coding Standard PSR-2 - Coding Style Guide PSR-3 - Logger Interface What is PSR?
  7. 7. Coding Standards by Aram Baghdasaryan PHP Standards Recommendation PSR-0 - Autoloader Standard PSR-1 - Basic Coding Standard PSR-2 - Coding Style Guide PSR-3 - Logger Interface PSR-4 - Autoloader Standard What is PSR?
  8. 8. Coding Standards by Aram Baghdasaryan Single style guide for PHP code that results in uniformly formatted shared code What it gives to us?
  9. 9. 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
  10. 10. Coding Standards by Aram Baghdasaryan Files SHOULD either declare symbols or cause side-effects but SHOULD NOT do both
  11. 11. Coding Standards by Aram Baghdasaryan // side effect: change ini settings ini_set('error_reporting', E_ALL); // side effect: loads a file include "file.php"; Files SHOULD either declare symbols or cause side-effects but SHOULD NOT do both
  12. 12. 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
  13. 13. 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
  14. 14. 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
  15. 15. Coding Standards by Aram Baghdasaryan <?php namespace App; use VendorLib1Tool; use VendorLib2Tool; class BloBlo { ... }
  16. 16. 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
  17. 17. Coding Standards by Aram Baghdasaryan final public static function getBlo() { ... }
  18. 18. 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
  19. 19. 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
  20. 20. Coding Standards by Aram Baghdasaryan function getBlo($param) use ($other) { if (true) { ... } } getBlo(‘value’);
  21. 21. Coding Standards by Aram Baghdasaryan Other Examples
  22. 22. Coding Standards by Aram Baghdasaryan function aLongMethodName( ClassTypeHint $arg1, &$arg2, array $arg3 = [] ) { // method body }
  23. 23. Coding Standards by Aram Baghdasaryan $foo->bar( $longArgument, $longerArgument, $muchLongerArgument );
  24. 24. Coding Standards by Aram Baghdasaryan if ($expr1) { // if body } elseif ($expr2) { // elseif body } else { // else body }
  25. 25. 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'; }
  26. 26. Coding Standards by Aram Baghdasaryan while ($expr) { // structure body } do { // structure body; } while ($expr);
  27. 27. Coding Standards by Aram Baghdasaryan for ($i = 0; $i < 10; $i++) { // for body } foreach ($iterable as $key => $value) { // foreach body }
  28. 28. Coding Standards by Aram Baghdasaryan try { // try body } catch (FirstExceptionType $e) { // catch body } catch (OtherExceptionType $e) { // catch body }
  29. 29. Coding Standards by Aram Baghdasaryan $closure = function ($arg1, $arg2) { // body }; $closure = function ($arg1) use ($var1) { // body };
  30. 30. Coding Standards by Aram Baghdasaryan Thank You!

×