Download the PHP package iadvize/php-convention without Composer
On this page you can find all versions of the php package iadvize/php-convention. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download iadvize/php-convention
More information about iadvize/php-convention
Files in iadvize/php-convention
Informations about the package php-convention
iAdvize PHP Style Guide
Table of Contents
- IDE Integration
- Files
- Lines
- Keywords
- Comments
- Naming
- Variables
- Constants
- Type casting
- Namespaces and use declarations
- String
- Arrays
- Classes, Properties, and Methods
- Interfaces, Traits
- Function and Method Calls
- Control Structures
- Closures
- Best practices
IDE integration
Troubleshooting
If you have this phpcs: PHP Fatal error: Uncaught exception 'PHP_CodeSniffer_Exception' with message 'Referenced sniff "Symfony2" does not exist'
Launch this: ./vendor/bin/phpcs --config-set installed_paths $PWD/vendor/escapestudios/symfony2-coding-standard,$PWD/vendor/iadvize/php-convention/phpcs
Files
-
Use only
UTF-8 without BOM
. -
Use only the Unix LF (linefeed) line ending.
-
All PHP files must end with a single blank line.
-
Use the long `` tags for non-views scripts.
-
Use the short-echo
<?= ?>
tags for view scripts. -
The closing
?>
tag must be omitted from files containing only PHP. -
Limit on line length limit must be
200 characters
. -
File must contain only one statement of namespace.
- Code must use
4 spaces
for indenting, not tabs.
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.
Keywords
- All PHP keywords must be in lower case (Eg:
true
,false
,null
, etc.)
Namespaces and use declarations
-
Namespaces names must be delcared in
UpperCamelCase
. -
Namespaces declaration never begin by a backslash
Vendor\Space\Space
. -
There must be one blank line before and after the
namepsace
declaration. -
There must be one blank line after the block of
use
declaration. -
use
declaration must not separated by comma. -
use
block declarations must be grouped by package: use
alias declaration should be composed with sub-namespaces names.
Comments
In-line code comments
- Comments should be on a separate line immediately before the code line or block they reference.
Block code comments
-
You must add PHPDoc blocks for all classes, methods, and functions, but you can omit the
@return
tag if the method does not return anything. -
You must add PHPDoc blocks for all variable references.
-
You must not use full qualified class name in PHPDoc blocks. This means you have to declare the class name with a use declaration even if she is not referenced elsewhere from the PHPBlock.
@todo
and@fixme
must be used in PHPDoc blocks like annotations.
Qualify objects you use
-
you should add
@var
tag when you get object from abstract method - you shouldn't add
@var
tag when you get object from explicit method
Naming
-
Clarity over brevity in variable, method and class names
-
Boolean
variable names should be either an adjective or a past participle form. Associated getter method should begin withis
orhas
. DateTime
variable names should be a past participle form ending withAt
.
Variables
User variables
-
Variables should be in
lowerCamelCase
. -
You must set off operators with spaces.
- You must conserve a great alignment.
Global variables
- You must used
$_POST
,$_GET
and$_COOKIE
instead of$_REQUEST
. If you use a framework, useRequest
component.
Strings
-
A string must be enclosed in single quotes
'hello'
. -
A concatenated string must use single quotes
'foo' . $bar
-
A concatenated string must use spaces around points
'foo' . $bar
-
A string declaration in multiline must be aligned
- A string must not concatenate with functions or methods
Constants
Class constants
-
Constants must be in
UPPER_SNAKE_CASE
. -
You must set assignment operator with spaces.
- You must conserve a great alignment.
Type casting
-
You must use
(int) $foo
instead ofintval($foo)
. -
You must use
(bool) $foo
instead ofboolval($foo)
. -
You must use
(float) $foo
instead offloatval($foo)
. - You must use
(string) $foo
instead ofstrval($foo)
.
Arrays
-
You must use
[]
notation instead ofarray()
. -
Arrays with few data must be declared like this:
-
Arrays with lots of data must be declared like this:
- For the arrays with lots of data, lines must be terminated by a comma. (Easy to copy/paste)
Classes, Properties, and Methods
Classes
-
The
extends
andimplements
keywords should be declared on the same line as the class name. -
The opening brace for the class must go on its own line; the closing brace for the class must go on the next line after the body.
- 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.
Properties
-
Visibility must be declared on all properties.
-
There must not be more than one property declared per statement.
-
Property names must not be prefixed with a single underscore to indicate
protected
orprivate
visibility. - When present, the
static
declaration must come after the visibility declaration.
Methods
-
Visibility must be declared on all methods. (Eg:
public|protected|private foo()
) -
Method names should not be prefixed with a single underscore to indicate protected or private visibility.
-
Method names must not be declared with a space after the method name.
-
There must not be a space after the opening parenthesis, and there must not be a space before the closing parenthesis.
-
The opening brace must go on its own line, and the closing brace must go on the next line following the body.
-
In the argument list, there must not be a space before each comma, and there must be one space after each comma.
-
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.
- When the argument list is split across multiple lines, the closing parenthesis and opening brace must be placed together on their own line with one space between them.
-
When present, the
abstract
andfinal
declarations must precede the visibility declaration. - When present, the
static
declaration must come after the visibility declaration.
Interfaces, Traits
Interfaces
- The interface name must be suffixed with
Interface
.
Traits
- The trait name must be suffixed with
Trait
.
Function and Method Calls
-
There must not be a space between the method or function name and the opening parenthesis.
-
There must not be a space after the opening parenthesis and there must not be a space before the closing parenthesis. In the argument list.
-
There must not be a space before each comma and there must be one space after each comma.
-
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.
-
Chained method calls must be wrapped before the first call and indented once.
- When you pass an array as the only argument, the array brackets should be on the same lines as the method parenthesis.
Control structures
General
-
There must be one space after the control structure keyword
-
There must not be a space after the opening parenthesis
-
There must not be a space before the closing parenthesis
-
There must be one space between the closing parenthesis and the opening brace
-
The structure body must be indented once
-
The closing brace must be on the next line after the body
- The body of each structure must be enclosed by braces. This standardizes how the structures look, and reduces the likelihood of introducing errors as new lines get added to the body.
if
, elseif
, else
- The keyword
elseif
should be used instead ofelse if
so that all control keywords look like single words.
Example
Ternary (?:
)
- You should not use nesting ternary.
Example
switch
and case
-
The
case
statement must be indented once fromswitch
, and thebreak
keyword (or other terminating keyword) must be indented at the same level as thecase
body. - There must be a comment such as
// no break
when fall-through is intentional in a non-empty case body.
Example
while
and do while
Example
for
Example
foreach
Example
try
and catch
Example
Closures
-
Closures must be declared with a space after the function keyword, and a space before and after the use keyword.
-
The opening brace must go on the same line, and the closing brace MUST go on the next line following the body.
-
There must not be a space after the opening parenthesis of the argument list or variable list, and there must not be a space before the closing parenthesis of the argument list or variable list.
-
In the argument list and variable list, there must not be a space before each comma, and there must be one space after each comma.
-
Closure arguments with default values must go at the end of the argument list.
-
Argument lists and variable 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 or variable per line.
- When the ending list (whether or arguments or variables) is split across multiple lines, the closing parenthesis and opening brace must be placed together on their own line with one space between them.
Example (declaration)
Example (usage)
Best practices
Date
-
You must use
new \DateTime('2014-01-01 00:00:00')
instead ofdate('2014-01-01 00:00:00')
. - You must use
new \DateTime('Sunday')
instead ofstrtotime('Sunday')
.
Readibility
- When possible, avoid nestings of more than 2 levels and prefer "return early" structures.
All versions of php-convention with dependencies
squizlabs/php_codesniffer Version 3.*
phpmd/phpmd Version ^2.3.2
friendsofphp/php-cs-fixer Version *
escapestudios/symfony2-coding-standard Version ~3.0