Skip to content

PHP coding style that can be used as an alternative to the PHP-FIG's PSR-2.

License

Notifications You must be signed in to change notification settings

thiagodp/effective-php-coding-style

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 

Repository files navigation

effective-php-coding-style

An effective PHP coding style, based on the PSR-1. It can be used as an alternative to the PSR-2.

Current version: 1.0

Translations

About

PHP-FIG's PSR-1 presents a good basic coding standard for PHP applications and does not impose a style. However, the PSR-2 coding style guide has some inconvenient guidelines such as:

  • It recommends the use of spaces instead of TABs;
  • Opening braces start in a new line instead of starting in the same line;
  • Opening parentheses for control structures do not have a pharentheses after them.
  • etc.

Hence the coding style presented here can be used as an alternative to PSR-2. It is simpler, presents some code examples and have a different structure. We point out the differences between the guidelines during their definition, so take a look and make your conclusions.

Quick Tip

Are you in a hurry? Jump to the examples.

Basic Example

The following piece of code is the same presented in PSR-2 (here), but uses our coding style.

<?php
namespace Vendor\Package;

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

class Foo extends Bar implements FooInterface {

    public function sampleFunction( $a, $b = null ) {
        if ( $a === $b ) {
            bar();
        } else if ( $a > $b ) {
            $foo->bar( $arg1 );
        } else {
            BazClass::bar( $arg2, $arg3 );
        }
    }

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

The Coding Style

General

The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.

  1. Code MUST follow the PSR-1 "coding style guide"; (same as PSR-2)

Naming

  1. Directories MUST use dashed-case (examples: api, sample-code); (not defined in PSR-2)
  2. PHP files MUST be named with the .php extension. You MAY use .class.php or .inc.php but this is NOT RECOMMENDED; (not defined in PSR-2)
  3. Files that contain classes MUST be named using the main class' name (example: MyImportantClass.php);
  4. Files that do not containt classes MUST be named in dashed-case (example: hello-world.php)

Lines

  1. Files MAY use any line-ending (Windows, Unix or Mac). However, all project files SHOULD use the same line-ending; (different from PSR-2)
  2. There MUST NOT be a hard limit on line length; the soft limit MUST be 120 characters; lines SHOULD be 80 characters or less; (same as PSR-2)

Code

Identing

  1. Code MUST use tabs for indenting, not spaces. Tab size MUST be equivalent to 4 spaces; (different from PSR-2)

Character set

  1. PHP files MUST use only UTF-8 without Byte Order Mark (BOM); (same as PSR-2)

Blanks lines

  1. There MUST be one blank line after the declaration of:
  2. namespaces; (same as PSR-2)
  3. uses; (same as PSR-2)
  4. classes; (different from PSR-2)
  5. between methods or functions; (not defined in PSR-2)

Blank spaces

  1. There MUST be one blank space:
  2. After control structures; (same as PSR-2)
  3. After opening parenthesis; (different from PSR-2)
  4. Before closing parenthesis; (different from PSR-2)
  5. Before and after the following operators: (not defined in PSR-2) 1. Arithmetic operators, except for negation (e.g. -$x); 2. Assignment operators; 3. Bitwise operators; 4. Comparison operators; 5. Logical operators; 6. String operators; 7. Array operators; 8. Type operators;
  6. After comma; (not defined in PSR-2)
  7. Methods and function calls MUST NOT have one blank space after them; (same as PSR-2)

Braces

  1. Opening braces MUST go on the same line, and closing braces MUST go on the next line after the body, for any code constructions; (different from PSR-2)

Keywords

  1. PHP keywords MUST be in lower case; (same as PSR-2)
  2. The PHP constants true, false, and null MUST be in lower case; (same as PSR-2)
  3. else MUST be separated from if on else ifs; (different from PSR-2)
  4. abstract, final and static MUST be declared after the visibility, and static MUST be declared after final; (different from PSR-2)
  5. include, require, include_once, and require_once MUST not use parentheses (i.e.: require_once 'MyClass.php';);

Declarations

  1. Visibility MAY be declared on properties and methods (PHP assumes public as the default visibility); (different from PSR-2)
  2. Constants MUST be declared in all upper case with underscore separators. Example: MAX_ENERGY. (same as PSR-2)
  3. Classes, interfaces and traits MUST be declared using PascalCase. Example: MySimpleClass. (same as PSR-2)
  4. Methods MUST be declared using camelCase. Example: mySimpleMethod. (same as PSR-2)
  5. Closures MUST be declared with a space after the function keyword, and a space before and after the use keyword. (same as PSR-2)
  6. Argument lists MAY be split across multiple lines, where each subsequent line is indented once. When doing so, the first item in the list MUST be on the next line, and there MUST be only one argument per line. (same as PSR-2)
  7. The extends and implements keywords MUST be declared on the same line as the class name. (same as PSR-2)
  8. Lists of implements MAY be split across multiple lines, where each subsequent line is indented once. When doing so, the first item in the list MUST be on the next line, and there MUST be only one interface per line. (same as PSR-2)
  9. Control structures MUST always use braces; (not defined in PSR-2)
  10. Classes without a namespace MUST be referenced with a backslash (\) before their names. Example: \MyClass. (not defined in PSR-2)
  11. The PHPDoc style MAY be used for documenting files, namespaces, classes, interfaces, traits, methods, functions, and other code constructions.

Remarks

Some elements were not included in this guide, and may appear in a next version:

  • namespaces;
  • some good practices;

Do you want to improve it? Please open an Issue.

Examples

See some examples here.

About

PHP coding style that can be used as an alternative to the PHP-FIG's PSR-2.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages