Skip to content

Instantly share code, notes, and snippets.

@lavandosovich
Last active May 15, 2017 01:46
Show Gist options
  • Select an option

  • Save lavandosovich/43fed8a0df383f3ae3a876d996ef0bf7 to your computer and use it in GitHub Desktop.

Select an option

Save lavandosovich/43fed8a0df383f3ae3a876d996ef0bf7 to your computer and use it in GitHub Desktop.
PSR essense jl.elama

1

Based on: PSR-1 && PSR-2

  • <?php & <&= only
  • UTF-8 only
  • 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:
    • bad:
      <?php
        // side effect: change ini settings
        ini_set('error_reporting', E_ALL);
      
        // side effect: loads a file
        include "file.php";
      
        // side effect: generates output
        echo "<html>\n";
      
        // declaration
        function foo()
        {
            // function body
        }
    • good:
      <?php
      // declaration
      function foo()
      {
          // function body
      }
      
      // conditional declaration is *not* a side effect
      if (! function_exists('bar')) {
          function bar()
          {
              // function body
          }
      }
  • Class names MUST be declared in StudlyCaps.
  • Namespaces and classes MUST follow an "autoloading" PSR: [PSR-0, PSR-4]:
<?php
// PHP 5.3 and later:
namespace Vendor\Model;

class Foo
{
}
  • CONSTANT_EXMPLE_DECLARATION = 3.14
  • public function camelCase(){}
  • Code MUST use 4 spaces for indenting, not tabs.
  • lineLength: soft limit MUST be 120 characters; lines SHOULD be 80 characters or less.
  • Open braces for classes&methods: openBrace after gon on the next line, closing after the body
  • Blank line after namespace & use
  • 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.
  • 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.
  • Opening parentheses for control structures MUST NOT have a space after them, and closing parentheses for control structures MUST NOT have a space before.
<?php
namespace Vendor\Package;

use FooInterface;
use BarClass as Bar;
use OtherVendor\OtherPackage\BazClass;

class Foo extends Bar implements FooInterface
{
    public function sampleMethod($a, $b = null)
    {
        if ($a === $b) {
            bar();
        } elseif ($a > $b) {
            $foo->bar($arg1);
        } else {
            BazClass::bar($arg2, $arg3);
        }
    }

    final public static function bar()
    {
        // method body
    }
}

2

  • All PHP files MUST end with a single blank line.
  • The closing ?> tag MUST be omitted from files containing only PHP.
  • There MUST NOT be trailing whitespace at the end of non-blank lines.
  • Blank lines MAY be added to improve readability and to indicate related blocks of code.
  • There MUST NOT be more than one statement per line.
  • PHP keywords MUST be in lower case.
  • When present, all use declarations MUST go after the namespace declaration.
  • There MUST be one use keyword per declaration.
<?php
namespace Vendor\Package;

use FooClass;
use BarClass as Bar;
use OtherVendor\OtherPackage\BazClass;

// ... additional PHP code ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment