This repository was archived by the owner on May 13, 2025. It is now read-only.

Repository files navigation

iAdvize PHP Style Guide

  1. IDE Integration
  2. Files
  3. Lines
  4. Keywords
  5. Comments
  6. Naming
  7. Variables
  8. Constants
  9. Type casting
  10. Namespaces and use declarations
  11. String
  12. Arrays
  13. Classes, Properties, and Methods
  14. Interfaces, Traits
  15. Function and Method Calls
  16. Control Structures
  17. Closures
  18. Best practices

Configure your PHPStorm

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

  • 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 <?php ?> tags for non-views scripts.

<?phpecho'test';
  • Use the short-echo <?= ?> tags for view scripts.
<title><?=$title;∙?></title>
  • 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.

  • Blank lines may be added to improve readability and to indicate related blocks of code.
// nahfunctionfoo()
{
$foo = 'test';
$foo = strtoupper($foo);
return$foo;
}
// goodfunctionbar()
{
$foo = 'test';
$foo = strtoupper($foo);
return$foo;
}
  • There must not be more than one statement per line.
// bad$foo = substr(strtoupper('test'), 0, 1);
// good$foo = 'test';
$foo = strtoupper($foo);
$foo = substr($foo, 0, 1);
  • All PHP keywords must be in lower case (Eg: true, false, null, etc.)
  • Namespaces names must be delcared in UpperCamelCase.
// badnamespaceVendor\fooBar;
// badnamespaceVendor\foo_bar;
// goodnamespaceVendor\FooBar;
  • 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:

// baduseFoo\Bar,
Qux\Quux,
Foo\Baz;
// baduseFoo\Bar;
useQux\Quux;
useFoo\Baz;
// gooduseFoo\Bar;
useFoo\Baz;
useQux\Quux;
useQux\Corge\Grault;
  • use alias declaration should be composed with sub-namespaces names.
// baduseFoo\BarasBaz;
// baduseBaz\Qux\QuuxasBQQ;
// gooduseFoo\BarasFooBar;
// gooduseBaz\Qux\QuuxasBazQuxQuux;

In-line code comments

  • Comments should be on a separate line immediately before the code line or block they reference.
// bad$foo = 'bar'; // Bar in foo// good// Foo assignment for example$foo = 'bar';
// good// Foo assignment// for example$foo = 'bar';

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.
/** * Foo * */class Foo
{
/** * The description of bar * * @param string $baz The baz * * @return string The return of bar */publicfunctionbar($baz)
{
// Returned valuereturn'Do something...';
}
}
  • You must add PHPDoc blocks for all variable references.
/** @var Bar\Baz $foo Baz object */
$foo = $bar->baz();
/** * Foo * */class Foo
{
/** @var string $bar It's bar! */public$bar = '';
}
  • 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.
// BadnamespaceVendor\Bar\Baz;
/** * Foo * */class Foo
{
/** @var \Other\MyClass $myClass */protected$myClass;
/** * @return \Other\MyClass */publicfunctiongetMyClass()
{
return$this->myClass;
}
}
// GoodnamespaceVendor\Bar\Baz;
useOther\MyClass;
/** * Foo * */class Foo
{
/** @var MyClass $myClass */protected$myClass;
/** * @return MyClass */publicfunctiongetMyClass()
{
return$this->myClass;
}
}
  • @todo and @fixme must be used in PHPDoc blocks like annotations.
/** @todo Think to check value */$foo = 'bar';
/** @fixme Change qux to quux */$baz = 'qux';

Qualify objects you use

  • you should add @var tag when you get object from abstract method
// bad$logger = $this->getServiceLocator()->get('logger');
// bad$this->getServiceLocator()->get('AwesomeFactory')->createAwesomeness();
// good/** @var LoggerInterface $logger */$logger = $this->getServiceLocator()->get('logger');
// good/** @var AwesomeFactory $awesomeFactory */$awesomeFactory = $this->getServiceLocator()->get('AwesomeFactory');
$awesomeFactory->createAwesomeness()
  • you shouldn't add @var tag when you get object from explicit method
// bad/** * Class AwesomeFactory */class AwesomeFactory
{
/** * @return Awesome */publicfunctioncreateAwesomeness()
{
returnAwesome();
}
}
$awesomeFactory = newAwesomeFactory();
/** @var Awesome $awesome */$awesome = $awesomeFactory->createAwesomeness();
// good/** * Class AwesomeFactory */class AwesomeFactory
{
/** * @return Awesome */publicfunctioncreateAwesomeness()
{
returnAwesome();
}
}
$awesomeFactory = newAwesomeFactory();
$awesome = $awesomeFactory->createAwesomeness();
  • Clarity over brevity in variable, method and class names
// bad$o = newObject();
// badclass A
{
}
// badpublicfunctiondoIt()
{
}
// good$object = newObject();
// goodclass Substracter
{
}
// goodpublicfunctionassociateChannelToOperator()
{
}
  • Boolean variable names should be either an adjective or a past participle form. Associated getter method should begin with is or has.
// bad$enablePlugin = true;
// badpublicfunctiongetEnablePlugin() {}
// badpublicfunctiongetPluginEnabled() {}
// good$pluginEnabled = true;
// good$visible = true;
// goodpublicfunctionisPluginEnabled() {}
// goodpublicfunctionisVisible() {}
  • DateTime variable names should be a past participle form ending with At.
// bad$dateUpdate = new \DateTime;
// bad$endDate = new \DateTime;
// good$updatedAt = new \DateTime;
// good$lastLoggedAt = new \DateTime;

User variables

  • Variables should be in lowerCamelCase.
// bad$_foo='';
// bad$foo_bar = '';
// bad$fooBar='';
// good$fooBar∙=∙'';
  • You must set off operators with spaces.
// bad$foo = (5+6)/5;
// good$foo∙=(5∙+∙6)∙/∙5;
  • You must conserve a great alignment.
// bad$foo = 'Ba';
$foo .= 'r';
$quux = 'Qu';
$quux .= 'x';
// good$foo = 'Ba';
$foo .= 'r';
$quux = 'Qu';
$quux .= 'x';
// bad$fooBarBazQux->bar()->
baz()->qux();
// good$fooBarBazQux∙∙∙∙->bar()
∙∙∙∙->baz()
∙∙∙∙->qux();

Global variables

  • You must used $_POST, $_GET and $_COOKIE instead of $_REQUEST. If you use a framework, use Request component.
  • 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

$foo = 'Bar'
.'Baz'
.'Qux';
  • A string must not concatenate with functions or methods
// bad$foo = ucfisrt('bar') . ' baz';
// good$foo = ucfirst('bar');
$foo = $foo . ' baz';
// very good$foo = ucfirst('bar');
$foo .= ' baz';

Class constants

  • Constants must be in UPPER_SNAKE_CASE.
// badconstMAXSIZE=5;
// badconstmax_size∙=∙4;
// goodconstMAX_SIZE∙=∙5;
  • You must set assignment operator with spaces.
// badconstMAX_SIZE=5;
// goodconstMAX_SIZE∙=∙5;
  • You must conserve a great alignment.
// badconstFOO∙∙='Ba';
constFOO∙∙='r';
constQUUX∙='Qu';
constQUUX∙='x';
// goodconstFOO∙∙='Ba';
constFOO∙∙='r';
constQUUX∙='Qu';
constQUUX∙='x';
  • You must use (int) $foo instead of intval($foo).

  • You must use (bool) $foo instead of boolval($foo).

  • You must use (float) $foo instead of floatval($foo).

  • You must use (string) $foo instead of strval($foo).

// bad$foo∙=(string)$bar;
// good$foo∙=(string)∙$bar;
  • You must use [] notation instead of array().

  • Arrays with few data must be declared like this:

$foo∙=∙['Bar',∙'Baz',∙'Qux'];
  • Arrays with lots of data must be declared like this:
$foo = [
∙∙∙∙'bar'∙∙=>∙'abc',
∙∙∙∙'baz'∙∙=>∙123,
∙∙∙∙'qux'∙∙=>∙true,
∙∙∙∙'quux'∙=>∙[
∙∙∙∙∙∙∙∙'corge'∙∙=>∙[],
∙∙∙∙∙∙∙∙'grault'∙=>∙123.456,
∙∙∙∙],
];
  • For the arrays with lots of data, lines must be terminated by a comma. (Easy to copy/paste)

Classes

  • The extends and implements 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.

<?phpnamespaceVendor\Foo;
class Foo extends Bar implements Baz, Qux, Quux
{
// Do something...
}
  • 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.
<?phpnamespaceVendor\Foo;
class Foo extends Bar implements
∙∙∙∙Baz,
∙∙∙∙Qux,
∙∙∙∙Quux
{
// Do something...
}

Properties

  • Visibility must be declared on all properties.
// bad/** @var string Property description */$foo = '';
// good/** @var string Property description */public$foo = '';
  • There must not be more than one property declared per statement.
// badpublic$foo = '',
$bar = '';
// good/** @var string Property description */public$foo = '';
/** @var string Property description */protected$bar = '';
  • Property names must not be prefixed with a single underscore to indicate protected or private visibility.
// bad/** @var string Property description */protected$_bar = '';
/** @var string Property description */private$_baz = '';
// good/** @var string Property description */protected$bar = '';
/** @var string Property description */private$baz = '';
  • When present, the static declaration must come after the visibility declaration.
// bad/** @var string $foo Property description */staticpublic$foo = '';
// good/** @var string $foo Property description */publicstatic$foo = '';

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.

// badprotectedfunction_foo()
{
// Do something...
}
// goodprotectedfunctionfoo()
{
// Do something...
}
  • Method names must not be declared with a space after the method name.
// badpublicfunctionfoo∙()
{
// Do something...
}
// goodpublicfunctionfoo()
{
// Do something...
}
  • There must not be a space after the opening parenthesis, and there must not be a space before the closing parenthesis.
// badpublicfunction foo()∙{∙
// Do something...
∙}
// goodpublicfunctionfoo()
{
// Do something...
}
  • The opening brace must go on its own line, and the closing brace must go on the next line following the body.
// badpublicfunction foo()∙{
// Do something...}// goodpublicfunctionfoo()
{
// Do something...
}
  • In the argument list, there must not be a space before each comma, and there must be one space after each comma.
// badpublicfunctionfoo($bar∙,&$baz∙,$qux = [])
{
// Do something...
}
// badpublicfunctionfoo($bar, &$baz, $qux = []∙)
{
// Do something...
}
// goodpublicfunction foo($bar,&$baz,$qux∙=∙[])
{
// Do something...
}
  • 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.

// goodpublicfunctionfoo(
∙∙∙∙$bar,
∙∙∙∙&$baz,
∙∙∙∙$qux = []
) {
// Do something...
}
  • When present, the abstract and final declarations must precede the visibility declaration.

  • When present, the static declaration must come after the visibility declaration.

// badprotectedabstractfunctionfoo();
staticpublicfinalfunctionbar()
{
// Do something...
}
// goodabstractprotectedfunctionfoo();
finalpublicstaticfunctionbar()
{
// Do something...
}

Interfaces

  • The interface name must be suffixed with Interface.
<?phpnamespaceVendor\Foo;
/** * Interface Foo * */interface FooInterface
{
/** * Set Foo * * @param string $foo */publicfunctionsetFoo($foo);
}

Traits

  • The trait name must be suffixed with Trait.
<?phpnamespaceVendor\Foo;
/** * Trait Foo * */trait FooTrait
{
/** @var \Vendor\Bar */protected$bar;
/** * Set Bar * * @param string $bar */publicfunctionsetBar($bar)
{
$this->bar = $bar;
}
}
  • There must not be a space between the method or function name and the opening parenthesis.
// badfoo∙();
$bar->baz∙();
// goodfoo();
$bar->baz();
  • 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.
// badfoo(∙$qux∙);
$bar->baz(∙$qux∙);
// goodfoo($qux);
$bar->baz($qux);
  • There must not be a space before each comma and there must be one space after each comma.
// badfoo($bar∙,∙$baz∙,∙$qux);
// goodfoo($bar,∙$baz,∙$qux);
  • 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.
// badfoo($longFoo,
$longBar,
$longBaz
);
// badfoo($longFoo,
$longBar,
$longBaz);
// goodfoo(
∙∙∙∙$longFoo,
∙∙∙∙$longBar,
∙∙∙∙$longBaz
);
  • Chained method calls must be wrapped before the first call and indented once.
// bad$fooBar->baz()->qux($param);
// good$fooBar
->baz()
->qux($param);
  • When you pass an array as the only argument, the array brackets should be on the same lines as the method parenthesis.
// badfoo(
[
'foo' => 'bar',
]
);
// goodfoo([
'foo' => 'bar',
]);

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 of else if so that all control keywords look like single words.

Example

// badif(EXPRESSION){
// Do something...
}
// badif (EXPRESSION) {
// Do something...
}
// badif (EXPRESSION)
{
// Do something...
}
// badif (EXPRESSION) {
// Do something...
}
else {
// Do something...
}
// goodif∙(EXPRESSION)∙{
∙∙∙∙// Do something...
}∙elseif∙(OTHER_EXPRESSION)∙{
∙∙∙∙// Do something...
}∙else∙{
∙∙∙∙// Do something...
}

Ternary (?:)

  • You should not use nesting ternary.

Example

// bad$foo = EXPRESSION ? 'bar' : OTHER_EXPRESSION ? 'baz' : 'qux';
// good$foo∙=∙EXPRESSION∙?∙'bar'∙:∙'baz';
// good$foo∙=∙EXPRESSION
∙∙∙∙?∙'bar'
∙∙∙∙:∙'baz';

switch and case

  • The case statement must be indented once from switch, and the break keyword (or other terminating keyword) must be indented at the same level as the case body.

  • There must be a comment such as // no break when fall-through is intentional in a non-empty case body.

Example

// badswitch(EXPRESSION)
{
case0:
// Do something...break;
}
// goodswitch∙(EXPRESSION)∙{
∙∙∙∙case∙0:
∙∙∙∙∙∙∙∙// Do something...
∙∙∙∙∙∙∙∙break;
∙∙∙∙case∙1:
∙∙∙∙∙∙∙∙// Do something with no break...
∙∙∙∙∙∙∙∙// no break
∙∙∙∙case∙2:
∙∙∙∙case∙3:
∙∙∙∙case∙4:
∙∙∙∙∙∙∙∙// Do something with return instead of break...
∙∙∙∙∙∙∙∙return;
∙∙∙∙default:
∙∙∙∙∙∙∙∙// Do something in default case...
∙∙∙∙∙∙∙∙break;
}

while and do while

Example

// badwhile(EXPRESSION)
{
// Do something...
}
// baddo
{
// Do something...
} while(EXPRESSION);
// goodwhile∙(EXPRESSION)∙{
∙∙∙∙// Do something...
}
// good
do∙{
∙∙∙∙// Do something...
}∙while∙(EXPRESSION);

for

Example

// badfor( $i=0;$i<10;$i++ )
{
// Do something...
}
// goodfor∙($i∙=∙0;∙$i∙<∙10;∙$i++)∙{
∙∙∙∙// Do something...
}

foreach

Example

// badforeach( $fooas$key=>$value )
{
// Do something...
}
// goodforeach∙($foo∙as∙$key∙=>∙$value)∙{
∙∙∙∙// Do something...
}

try and catch

Example

// badtry
{
// Do something...
}
catch(FooException$e)
{
// Do something...
}
// good
try∙{
∙∙∙∙// Do something...
}∙catch∙(FooException∙$exception)∙{
∙∙∙∙// Do something...
}∙catch∙(BarException∙$exception)∙{
∙∙∙∙// Do something...
}∙finally∙{
∙∙∙∙// Do something...
}
  • 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)

// good$closureWithArguments∙=∙function∙($foo,∙$bar)∙{
∙∙∙∙// Do something...
};
// good$closureWithArgumentsAndVariables∙=∙function∙($foo,∙$bar)∙use∙($baz,∙$qux)∙{
∙∙∙∙// Do something...
};
// good$longArgumentsNoVariables∙=∙function∙(
∙∙∙∙$longArgumentFoo,
∙∙∙∙$longArgumentBar,
∙∙∙∙$longArgumentBaz
)∙{
∙∙∙∙// Do something...
};
// good$noArgumentsLongVariables∙=∙function∙()∙use∙(
∙∙∙∙$longVariableFoo,
∙∙∙∙$longVariablBar,
∙∙∙∙$longVariableBaz
)∙{
∙∙∙∙// Do something...
};
// good$longArgumentsLongVariables∙=∙function∙(
∙∙∙∙$longArgumentFoo,
∙∙∙∙$longArgumentBar,
∙∙∙∙$longArgumentBaz
)∙use∙(
∙∙∙∙$longVariableFoo,
∙∙∙∙$longVariableBar,
∙∙∙∙$longVariableBaz
)∙{
∙∙∙∙// Do something...
};
// good$longArgumentsShortVariables∙=∙function∙(
∙∙∙∙$longArgumentFoo,
∙∙∙∙$longArgumentBar,
∙∙∙∙$longArgumentBaz
)∙use∙($variableFoo)∙{
∙∙∙∙// Do something...
};
// good$shortArgumentsLongVariables∙=∙function∙($argumentFoo)∙use∙(
∙∙∙∙$longVariableFoo,
∙∙∙∙$longVariableBar,
∙∙∙∙$longVariableBaz
)∙{
∙∙∙∙// Do something...
};

Example (usage)

$foo->bar(
∙∙∙∙$argumentFoo,
∙∙∙∙function∙($argumentBar)∙use∙($variableFoo)∙{
∙∙∙∙∙∙∙∙// Do something...
∙∙∙∙},
∙∙∙∙$argumentBaz
);

Date

  • You must use new \DateTime('2014-01-01 00:00:00') instead of date('2014-01-01 00:00:00').

  • You must use new \DateTime('Sunday') instead of strtotime('Sunday').

Readibility

  • When possible, avoid nestings of more than 2 levels and prefer "return early" structures.
// bad$response = [];
if ($foo) {
foreach ($foo->getBars() as$bar) {
if ($bar->hasBaz()) {
// 3 nested levels
}
}
}
return$response;
// good$response = [];
if (!$foo) {
return$response;
}
foreach ($foo->getBars() as$bar) {
if ($bar->hasBaz()) {
// only 2 nested levels
}
}
return$response;

About

PHP conventions at iAdvize

Resources

Stars

16 stars

Watchers

11 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content
This repository was archived by the owner on May 13, 2025. It is now read-only.

Repository files navigation

iAdvize PHP Style Guide

  1. IDE Integration
  2. Files
  3. Lines
  4. Keywords
  5. Comments
  6. Naming
  7. Variables
  8. Constants
  9. Type casting
  10. Namespaces and use declarations
  11. String
  12. Arrays
  13. Classes, Properties, and Methods
  14. Interfaces, Traits
  15. Function and Method Calls
  16. Control Structures
  17. Closures
  18. Best practices

Configure your PHPStorm

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

  • 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 <?php ?> tags for non-views scripts.

<?phpecho'test';
  • Use the short-echo <?= ?> tags for view scripts.
<title><?=$title;∙?></title>
  • 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.

  • Blank lines may be added to improve readability and to indicate related blocks of code.
// nahfunctionfoo()
{
$foo = 'test';
$foo = strtoupper($foo);
return$foo;
}
// goodfunctionbar()
{
$foo = 'test';
$foo = strtoupper($foo);
return$foo;
}
  • There must not be more than one statement per line.
// bad$foo = substr(strtoupper('test'), 0, 1);
// good$foo = 'test';
$foo = strtoupper($foo);
$foo = substr($foo, 0, 1);
  • All PHP keywords must be in lower case (Eg: true, false, null, etc.)
  • Namespaces names must be delcared in UpperCamelCase.
// badnamespaceVendor\fooBar;
// badnamespaceVendor\foo_bar;
// goodnamespaceVendor\FooBar;
  • 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:

// baduseFoo\Bar,
Qux\Quux,
Foo\Baz;
// baduseFoo\Bar;
useQux\Quux;
useFoo\Baz;
// gooduseFoo\Bar;
useFoo\Baz;
useQux\Quux;
useQux\Corge\Grault;
  • use alias declaration should be composed with sub-namespaces names.
// baduseFoo\BarasBaz;
// baduseBaz\Qux\QuuxasBQQ;
// gooduseFoo\BarasFooBar;
// gooduseBaz\Qux\QuuxasBazQuxQuux;

In-line code comments

  • Comments should be on a separate line immediately before the code line or block they reference.
// bad$foo = 'bar'; // Bar in foo// good// Foo assignment for example$foo = 'bar';
// good// Foo assignment// for example$foo = 'bar';

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.
/** * Foo * */class Foo
{
/** * The description of bar * * @param string $baz The baz * * @return string The return of bar */publicfunctionbar($baz)
{
// Returned valuereturn'Do something...';
}
}
  • You must add PHPDoc blocks for all variable references.
/** @var Bar\Baz $foo Baz object */
$foo = $bar->baz();
/** * Foo * */class Foo
{
/** @var string $bar It's bar! */public$bar = '';
}
  • 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.
// BadnamespaceVendor\Bar\Baz;
/** * Foo * */class Foo
{
/** @var \Other\MyClass $myClass */protected$myClass;
/** * @return \Other\MyClass */publicfunctiongetMyClass()
{
return$this->myClass;
}
}
// GoodnamespaceVendor\Bar\Baz;
useOther\MyClass;
/** * Foo * */class Foo
{
/** @var MyClass $myClass */protected$myClass;
/** * @return MyClass */publicfunctiongetMyClass()
{
return$this->myClass;
}
}
  • @todo and @fixme must be used in PHPDoc blocks like annotations.
/** @todo Think to check value */$foo = 'bar';
/** @fixme Change qux to quux */$baz = 'qux';

Qualify objects you use

  • you should add @var tag when you get object from abstract method
// bad$logger = $this->getServiceLocator()->get('logger');
// bad$this->getServiceLocator()->get('AwesomeFactory')->createAwesomeness();
// good/** @var LoggerInterface $logger */$logger = $this->getServiceLocator()->get('logger');
// good/** @var AwesomeFactory $awesomeFactory */$awesomeFactory = $this->getServiceLocator()->get('AwesomeFactory');
$awesomeFactory->createAwesomeness()
  • you shouldn't add @var tag when you get object from explicit method
// bad/** * Class AwesomeFactory */class AwesomeFactory
{
/** * @return Awesome */publicfunctioncreateAwesomeness()
{
returnAwesome();
}
}
$awesomeFactory = newAwesomeFactory();
/** @var Awesome $awesome */$awesome = $awesomeFactory->createAwesomeness();
// good/** * Class AwesomeFactory */class AwesomeFactory
{
/** * @return Awesome */publicfunctioncreateAwesomeness()
{
returnAwesome();
}
}
$awesomeFactory = newAwesomeFactory();
$awesome = $awesomeFactory->createAwesomeness();
  • Clarity over brevity in variable, method and class names
// bad$o = newObject();
// badclass A
{
}
// badpublicfunctiondoIt()
{
}
// good$object = newObject();
// goodclass Substracter
{
}
// goodpublicfunctionassociateChannelToOperator()
{
}
  • Boolean variable names should be either an adjective or a past participle form. Associated getter method should begin with is or has.
// bad$enablePlugin = true;
// badpublicfunctiongetEnablePlugin() {}
// badpublicfunctiongetPluginEnabled() {}
// good$pluginEnabled = true;
// good$visible = true;
// goodpublicfunctionisPluginEnabled() {}
// goodpublicfunctionisVisible() {}
  • DateTime variable names should be a past participle form ending with At.
// bad$dateUpdate = new \DateTime;
// bad$endDate = new \DateTime;
// good$updatedAt = new \DateTime;
// good$lastLoggedAt = new \DateTime;

User variables

  • Variables should be in lowerCamelCase.
// bad$_foo='';
// bad$foo_bar = '';
// bad$fooBar='';
// good$fooBar∙=∙'';
  • You must set off operators with spaces.
// bad$foo = (5+6)/5;
// good$foo∙=(5∙+∙6)∙/∙5;
  • You must conserve a great alignment.
// bad$foo = 'Ba';
$foo .= 'r';
$quux = 'Qu';
$quux .= 'x';
// good$foo = 'Ba';
$foo .= 'r';
$quux = 'Qu';
$quux .= 'x';
// bad$fooBarBazQux->bar()->
baz()->qux();
// good$fooBarBazQux∙∙∙∙->bar()
∙∙∙∙->baz()
∙∙∙∙->qux();

Global variables

  • You must used $_POST, $_GET and $_COOKIE instead of $_REQUEST. If you use a framework, use Request component.
  • 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

$foo = 'Bar'
.'Baz'
.'Qux';
  • A string must not concatenate with functions or methods
// bad$foo = ucfisrt('bar') . ' baz';
// good$foo = ucfirst('bar');
$foo = $foo . ' baz';
// very good$foo = ucfirst('bar');
$foo .= ' baz';

Class constants

  • Constants must be in UPPER_SNAKE_CASE.
// badconstMAXSIZE=5;
// badconstmax_size∙=∙4;
// goodconstMAX_SIZE∙=∙5;
  • You must set assignment operator with spaces.
// badconstMAX_SIZE=5;
// goodconstMAX_SIZE∙=∙5;
  • You must conserve a great alignment.
// badconstFOO∙∙='Ba';
constFOO∙∙='r';
constQUUX∙='Qu';
constQUUX∙='x';
// goodconstFOO∙∙='Ba';
constFOO∙∙='r';
constQUUX∙='Qu';
constQUUX∙='x';
  • You must use (int) $foo instead of intval($foo).

  • You must use (bool) $foo instead of boolval($foo).

  • You must use (float) $foo instead of floatval($foo).

  • You must use (string) $foo instead of strval($foo).

// bad$foo∙=(string)$bar;
// good$foo∙=(string)∙$bar;
  • You must use [] notation instead of array().

  • Arrays with few data must be declared like this:

$foo∙=∙['Bar',∙'Baz',∙'Qux'];
  • Arrays with lots of data must be declared like this:
$foo = [
∙∙∙∙'bar'∙∙=>∙'abc',
∙∙∙∙'baz'∙∙=>∙123,
∙∙∙∙'qux'∙∙=>∙true,
∙∙∙∙'quux'∙=>∙[
∙∙∙∙∙∙∙∙'corge'∙∙=>∙[],
∙∙∙∙∙∙∙∙'grault'∙=>∙123.456,
∙∙∙∙],
];
  • For the arrays with lots of data, lines must be terminated by a comma. (Easy to copy/paste)

Classes

  • The extends and implements 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.

<?phpnamespaceVendor\Foo;
class Foo extends Bar implements Baz, Qux, Quux
{
// Do something...
}
  • 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.
<?phpnamespaceVendor\Foo;
class Foo extends Bar implements
∙∙∙∙Baz,
∙∙∙∙Qux,
∙∙∙∙Quux
{
// Do something...
}

Properties

  • Visibility must be declared on all properties.
// bad/** @var string Property description */$foo = '';
// good/** @var string Property description */public$foo = '';
  • There must not be more than one property declared per statement.
// badpublic$foo = '',
$bar = '';
// good/** @var string Property description */public$foo = '';
/** @var string Property description */protected$bar = '';
  • Property names must not be prefixed with a single underscore to indicate protected or private visibility.
// bad/** @var string Property description */protected$_bar = '';
/** @var string Property description */private$_baz = '';
// good/** @var string Property description */protected$bar = '';
/** @var string Property description */private$baz = '';
  • When present, the static declaration must come after the visibility declaration.
// bad/** @var string $foo Property description */staticpublic$foo = '';
// good/** @var string $foo Property description */publicstatic$foo = '';

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.

// badprotectedfunction_foo()
{
// Do something...
}
// goodprotectedfunctionfoo()
{
// Do something...
}
  • Method names must not be declared with a space after the method name.
// badpublicfunctionfoo∙()
{
// Do something...
}
// goodpublicfunctionfoo()
{
// Do something...
}
  • There must not be a space after the opening parenthesis, and there must not be a space before the closing parenthesis.
// badpublicfunction foo()∙{∙
// Do something...
∙}
// goodpublicfunctionfoo()
{
// Do something...
}
  • The opening brace must go on its own line, and the closing brace must go on the next line following the body.
// badpublicfunction foo()∙{
// Do something...}// goodpublicfunctionfoo()
{
// Do something...
}
  • In the argument list, there must not be a space before each comma, and there must be one space after each comma.
// badpublicfunctionfoo($bar∙,&$baz∙,$qux = [])
{
// Do something...
}
// badpublicfunctionfoo($bar, &$baz, $qux = []∙)
{
// Do something...
}
// goodpublicfunction foo($bar,&$baz,$qux∙=∙[])
{
// Do something...
}
  • 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.

// goodpublicfunctionfoo(
∙∙∙∙$bar,
∙∙∙∙&$baz,
∙∙∙∙$qux = []
) {
// Do something...
}
  • When present, the abstract and final declarations must precede the visibility declaration.

  • When present, the static declaration must come after the visibility declaration.

// badprotectedabstractfunctionfoo();
staticpublicfinalfunctionbar()
{
// Do something...
}
// goodabstractprotectedfunctionfoo();
finalpublicstaticfunctionbar()
{
// Do something...
}

Interfaces

  • The interface name must be suffixed with Interface.
<?phpnamespaceVendor\Foo;
/** * Interface Foo * */interface FooInterface
{
/** * Set Foo * * @param string $foo */publicfunctionsetFoo($foo);
}

Traits

  • The trait name must be suffixed with Trait.
<?phpnamespaceVendor\Foo;
/** * Trait Foo * */trait FooTrait
{
/** @var \Vendor\Bar */protected$bar;
/** * Set Bar * * @param string $bar */publicfunctionsetBar($bar)
{
$this->bar = $bar;
}
}
  • There must not be a space between the method or function name and the opening parenthesis.
// badfoo∙();
$bar->baz∙();
// goodfoo();
$bar->baz();
  • 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.
// badfoo(∙$qux∙);
$bar->baz(∙$qux∙);
// goodfoo($qux);
$bar->baz($qux);
  • There must not be a space before each comma and there must be one space after each comma.
// badfoo($bar∙,∙$baz∙,∙$qux);
// goodfoo($bar,∙$baz,∙$qux);
  • 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.
// badfoo($longFoo,
$longBar,
$longBaz
);
// badfoo($longFoo,
$longBar,
$longBaz);
// goodfoo(
∙∙∙∙$longFoo,
∙∙∙∙$longBar,
∙∙∙∙$longBaz
);
  • Chained method calls must be wrapped before the first call and indented once.
// bad$fooBar->baz()->qux($param);
// good$fooBar
->baz()
->qux($param);
  • When you pass an array as the only argument, the array brackets should be on the same lines as the method parenthesis.
// badfoo(
[
'foo' => 'bar',
]
);
// goodfoo([
'foo' => 'bar',
]);

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 of else if so that all control keywords look like single words.

Example

// badif(EXPRESSION){
// Do something...
}
// badif (EXPRESSION) {
// Do something...
}
// badif (EXPRESSION)
{
// Do something...
}
// badif (EXPRESSION) {
// Do something...
}
else {
// Do something...
}
// goodif∙(EXPRESSION)∙{
∙∙∙∙// Do something...
}∙elseif∙(OTHER_EXPRESSION)∙{
∙∙∙∙// Do something...
}∙else∙{
∙∙∙∙// Do something...
}

Ternary (?:)

  • You should not use nesting ternary.

Example

// bad$foo = EXPRESSION ? 'bar' : OTHER_EXPRESSION ? 'baz' : 'qux';
// good$foo∙=∙EXPRESSION∙?∙'bar'∙:∙'baz';
// good$foo∙=∙EXPRESSION
∙∙∙∙?∙'bar'
∙∙∙∙:∙'baz';

switch and case

  • The case statement must be indented once from switch, and the break keyword (or other terminating keyword) must be indented at the same level as the case body.

  • There must be a comment such as // no break when fall-through is intentional in a non-empty case body.

Example

// badswitch(EXPRESSION)
{
case0:
// Do something...break;
}
// goodswitch∙(EXPRESSION)∙{
∙∙∙∙case∙0:
∙∙∙∙∙∙∙∙// Do something...
∙∙∙∙∙∙∙∙break;
∙∙∙∙case∙1:
∙∙∙∙∙∙∙∙// Do something with no break...
∙∙∙∙∙∙∙∙// no break
∙∙∙∙case∙2:
∙∙∙∙case∙3:
∙∙∙∙case∙4:
∙∙∙∙∙∙∙∙// Do something with return instead of break...
∙∙∙∙∙∙∙∙return;
∙∙∙∙default:
∙∙∙∙∙∙∙∙// Do something in default case...
∙∙∙∙∙∙∙∙break;
}

while and do while

Example

// badwhile(EXPRESSION)
{
// Do something...
}
// baddo
{
// Do something...
} while(EXPRESSION);
// goodwhile∙(EXPRESSION)∙{
∙∙∙∙// Do something...
}
// good
do∙{
∙∙∙∙// Do something...
}∙while∙(EXPRESSION);

for

Example

// badfor( $i=0;$i<10;$i++ )
{
// Do something...
}
// goodfor∙($i∙=∙0;∙$i∙<∙10;∙$i++)∙{
∙∙∙∙// Do something...
}

foreach

Example

// badforeach( $fooas$key=>$value )
{
// Do something...
}
// goodforeach∙($foo∙as∙$key∙=>∙$value)∙{
∙∙∙∙// Do something...
}

try and catch

Example

// badtry
{
// Do something...
}
catch(FooException$e)
{
// Do something...
}
// good
try∙{
∙∙∙∙// Do something...
}∙catch∙(FooException∙$exception)∙{
∙∙∙∙// Do something...
}∙catch∙(BarException∙$exception)∙{
∙∙∙∙// Do something...
}∙finally∙{
∙∙∙∙// Do something...
}
  • 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)

// good$closureWithArguments∙=∙function∙($foo,∙$bar)∙{
∙∙∙∙// Do something...
};
// good$closureWithArgumentsAndVariables∙=∙function∙($foo,∙$bar)∙use∙($baz,∙$qux)∙{
∙∙∙∙// Do something...
};
// good$longArgumentsNoVariables∙=∙function∙(
∙∙∙∙$longArgumentFoo,
∙∙∙∙$longArgumentBar,
∙∙∙∙$longArgumentBaz
)∙{
∙∙∙∙// Do something...
};
// good$noArgumentsLongVariables∙=∙function∙()∙use∙(
∙∙∙∙$longVariableFoo,
∙∙∙∙$longVariablBar,
∙∙∙∙$longVariableBaz
)∙{
∙∙∙∙// Do something...
};
// good$longArgumentsLongVariables∙=∙function∙(
∙∙∙∙$longArgumentFoo,
∙∙∙∙$longArgumentBar,
∙∙∙∙$longArgumentBaz
)∙use∙(
∙∙∙∙$longVariableFoo,
∙∙∙∙$longVariableBar,
∙∙∙∙$longVariableBaz
)∙{
∙∙∙∙// Do something...
};
// good$longArgumentsShortVariables∙=∙function∙(
∙∙∙∙$longArgumentFoo,
∙∙∙∙$longArgumentBar,
∙∙∙∙$longArgumentBaz
)∙use∙($variableFoo)∙{
∙∙∙∙// Do something...
};
// good$shortArgumentsLongVariables∙=∙function∙($argumentFoo)∙use∙(
∙∙∙∙$longVariableFoo,
∙∙∙∙$longVariableBar,
∙∙∙∙$longVariableBaz
)∙{
∙∙∙∙// Do something...
};

Example (usage)

$foo->bar(
∙∙∙∙$argumentFoo,
∙∙∙∙function∙($argumentBar)∙use∙($variableFoo)∙{
∙∙∙∙∙∙∙∙// Do something...
∙∙∙∙},
∙∙∙∙$argumentBaz
);

Date

  • You must use new \DateTime('2014-01-01 00:00:00') instead of date('2014-01-01 00:00:00').

  • You must use new \DateTime('Sunday') instead of strtotime('Sunday').

Readibility

  • When possible, avoid nestings of more than 2 levels and prefer "return early" structures.
// bad$response = [];
if ($foo) {
foreach ($foo->getBars() as$bar) {
if ($bar->hasBaz()) {
// 3 nested levels
}
}
}
return$response;
// good$response = [];
if (!$foo) {
return$response;
}
foreach ($foo->getBars() as$bar) {
if ($bar->hasBaz()) {
// only 2 nested levels
}
}
return$response;

About

PHP conventions at iAdvize

Resources

Stars

16 stars

Watchers

11 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
This repository was archived by the owner on May 13, 2025. It is now read-only.

Repository files navigation

iAdvize PHP Style Guide

  1. IDE Integration
  2. Files
  3. Lines
  4. Keywords
  5. Comments
  6. Naming
  7. Variables
  8. Constants
  9. Type casting
  10. Namespaces and use declarations
  11. String
  12. Arrays
  13. Classes, Properties, and Methods
  14. Interfaces, Traits
  15. Function and Method Calls
  16. Control Structures
  17. Closures
  18. Best practices

Configure your PHPStorm

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

  • 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 <?php ?> tags for non-views scripts.

<?phpecho'test';
  • Use the short-echo <?= ?> tags for view scripts.
<title><?=$title;∙?></title>
  • 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.

  • Blank lines may be added to improve readability and to indicate related blocks of code.
// nahfunctionfoo()
{
$foo = 'test';
$foo = strtoupper($foo);
return$foo;
}
// goodfunctionbar()
{
$foo = 'test';
$foo = strtoupper($foo);
return$foo;
}
  • There must not be more than one statement per line.
// bad$foo = substr(strtoupper('test'), 0, 1);
// good$foo = 'test';
$foo = strtoupper($foo);
$foo = substr($foo, 0, 1);
  • All PHP keywords must be in lower case (Eg: true, false, null, etc.)
  • Namespaces names must be delcared in UpperCamelCase.
// badnamespaceVendor\fooBar;
// badnamespaceVendor\foo_bar;
// goodnamespaceVendor\FooBar;
  • 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:

// baduseFoo\Bar,
Qux\Quux,
Foo\Baz;
// baduseFoo\Bar;
useQux\Quux;
useFoo\Baz;
// gooduseFoo\Bar;
useFoo\Baz;
useQux\Quux;
useQux\Corge\Grault;
  • use alias declaration should be composed with sub-namespaces names.
// baduseFoo\BarasBaz;
// baduseBaz\Qux\QuuxasBQQ;
// gooduseFoo\BarasFooBar;
// gooduseBaz\Qux\QuuxasBazQuxQuux;

In-line code comments

  • Comments should be on a separate line immediately before the code line or block they reference.
// bad$foo = 'bar'; // Bar in foo// good// Foo assignment for example$foo = 'bar';
// good// Foo assignment// for example$foo = 'bar';

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.
/** * Foo * */class Foo
{
/** * The description of bar * * @param string $baz The baz * * @return string The return of bar */publicfunctionbar($baz)
{
// Returned valuereturn'Do something...';
}
}
  • You must add PHPDoc blocks for all variable references.
/** @var Bar\Baz $foo Baz object */
$foo = $bar->baz();
/** * Foo * */class Foo
{
/** @var string $bar It's bar! */public$bar = '';
}
  • 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.
// BadnamespaceVendor\Bar\Baz;
/** * Foo * */class Foo
{
/** @var \Other\MyClass $myClass */protected$myClass;
/** * @return \Other\MyClass */publicfunctiongetMyClass()
{
return$this->myClass;
}
}
// GoodnamespaceVendor\Bar\Baz;
useOther\MyClass;
/** * Foo * */class Foo
{
/** @var MyClass $myClass */protected$myClass;
/** * @return MyClass */publicfunctiongetMyClass()
{
return$this->myClass;
}
}
  • @todo and @fixme must be used in PHPDoc blocks like annotations.
/** @todo Think to check value */$foo = 'bar';
/** @fixme Change qux to quux */$baz = 'qux';

Qualify objects you use

  • you should add @var tag when you get object from abstract method
// bad$logger = $this->getServiceLocator()->get('logger');
// bad$this->getServiceLocator()->get('AwesomeFactory')->createAwesomeness();
// good/** @var LoggerInterface $logger */$logger = $this->getServiceLocator()->get('logger');
// good/** @var AwesomeFactory $awesomeFactory */$awesomeFactory = $this->getServiceLocator()->get('AwesomeFactory');
$awesomeFactory->createAwesomeness()
  • you shouldn't add @var tag when you get object from explicit method
// bad/** * Class AwesomeFactory */class AwesomeFactory
{
/** * @return Awesome */publicfunctioncreateAwesomeness()
{
returnAwesome();
}
}
$awesomeFactory = newAwesomeFactory();
/** @var Awesome $awesome */$awesome = $awesomeFactory->createAwesomeness();
// good/** * Class AwesomeFactory */class AwesomeFactory
{
/** * @return Awesome */publicfunctioncreateAwesomeness()
{
returnAwesome();
}
}
$awesomeFactory = newAwesomeFactory();
$awesome = $awesomeFactory->createAwesomeness();
  • Clarity over brevity in variable, method and class names
// bad$o = newObject();
// badclass A
{
}
// badpublicfunctiondoIt()
{
}
// good$object = newObject();
// goodclass Substracter
{
}
// goodpublicfunctionassociateChannelToOperator()
{
}
  • Boolean variable names should be either an adjective or a past participle form. Associated getter method should begin with is or has.
// bad$enablePlugin = true;
// badpublicfunctiongetEnablePlugin() {}
// badpublicfunctiongetPluginEnabled() {}
// good$pluginEnabled = true;
// good$visible = true;
// goodpublicfunctionisPluginEnabled() {}
// goodpublicfunctionisVisible() {}
  • DateTime variable names should be a past participle form ending with At.
// bad$dateUpdate = new \DateTime;
// bad$endDate = new \DateTime;
// good$updatedAt = new \DateTime;
// good$lastLoggedAt = new \DateTime;

User variables

  • Variables should be in lowerCamelCase.
// bad$_foo='';
// bad$foo_bar = '';
// bad$fooBar='';
// good$fooBar∙=∙'';
  • You must set off operators with spaces.
// bad$foo = (5+6)/5;
// good$foo∙=(5∙+∙6)∙/∙5;
  • You must conserve a great alignment.
// bad$foo = 'Ba';
$foo .= 'r';
$quux = 'Qu';
$quux .= 'x';
// good$foo = 'Ba';
$foo .= 'r';
$quux = 'Qu';
$quux .= 'x';
// bad$fooBarBazQux->bar()->
baz()->qux();
// good$fooBarBazQux∙∙∙∙->bar()
∙∙∙∙->baz()
∙∙∙∙->qux();

Global variables

  • You must used $_POST, $_GET and $_COOKIE instead of $_REQUEST. If you use a framework, use Request component.
  • 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

$foo = 'Bar'
.'Baz'
.'Qux';
  • A string must not concatenate with functions or methods
// bad$foo = ucfisrt('bar') . ' baz';
// good$foo = ucfirst('bar');
$foo = $foo . ' baz';
// very good$foo = ucfirst('bar');
$foo .= ' baz';

Class constants

  • Constants must be in UPPER_SNAKE_CASE.
// badconstMAXSIZE=5;
// badconstmax_size∙=∙4;
// goodconstMAX_SIZE∙=∙5;
  • You must set assignment operator with spaces.
// badconstMAX_SIZE=5;
// goodconstMAX_SIZE∙=∙5;
  • You must conserve a great alignment.
// badconstFOO∙∙='Ba';
constFOO∙∙='r';
constQUUX∙='Qu';
constQUUX∙='x';
// goodconstFOO∙∙='Ba';
constFOO∙∙='r';
constQUUX∙='Qu';
constQUUX∙='x';
  • You must use (int) $foo instead of intval($foo).

  • You must use (bool) $foo instead of boolval($foo).

  • You must use (float) $foo instead of floatval($foo).

  • You must use (string) $foo instead of strval($foo).

// bad$foo∙=(string)$bar;
// good$foo∙=(string)∙$bar;
  • You must use [] notation instead of array().

  • Arrays with few data must be declared like this:

$foo∙=∙['Bar',∙'Baz',∙'Qux'];
  • Arrays with lots of data must be declared like this:
$foo = [
∙∙∙∙'bar'∙∙=>∙'abc',
∙∙∙∙'baz'∙∙=>∙123,
∙∙∙∙'qux'∙∙=>∙true,
∙∙∙∙'quux'∙=>∙[
∙∙∙∙∙∙∙∙'corge'∙∙=>∙[],
∙∙∙∙∙∙∙∙'grault'∙=>∙123.456,
∙∙∙∙],
];
  • For the arrays with lots of data, lines must be terminated by a comma. (Easy to copy/paste)

Classes

  • The extends and implements 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.

<?phpnamespaceVendor\Foo;
class Foo extends Bar implements Baz, Qux, Quux
{
// Do something...
}
  • 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.
<?phpnamespaceVendor\Foo;
class Foo extends Bar implements
∙∙∙∙Baz,
∙∙∙∙Qux,
∙∙∙∙Quux
{
// Do something...
}

Properties

  • Visibility must be declared on all properties.
// bad/** @var string Property description */$foo = '';
// good/** @var string Property description */public$foo = '';
  • There must not be more than one property declared per statement.
// badpublic$foo = '',
$bar = '';
// good/** @var string Property description */public$foo = '';
/** @var string Property description */protected$bar = '';
  • Property names must not be prefixed with a single underscore to indicate protected or private visibility.
// bad/** @var string Property description */protected$_bar = '';
/** @var string Property description */private$_baz = '';
// good/** @var string Property description */protected$bar = '';
/** @var string Property description */private$baz = '';
  • When present, the static declaration must come after the visibility declaration.
// bad/** @var string $foo Property description */staticpublic$foo = '';
// good/** @var string $foo Property description */publicstatic$foo = '';

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.

// badprotectedfunction_foo()
{
// Do something...
}
// goodprotectedfunctionfoo()
{
// Do something...
}
  • Method names must not be declared with a space after the method name.
// badpublicfunctionfoo∙()
{
// Do something...
}
// goodpublicfunctionfoo()
{
// Do something...
}
  • There must not be a space after the opening parenthesis, and there must not be a space before the closing parenthesis.
// badpublicfunction foo()∙{∙
// Do something...
∙}
// goodpublicfunctionfoo()
{
// Do something...
}
  • The opening brace must go on its own line, and the closing brace must go on the next line following the body.
// badpublicfunction foo()∙{
// Do something...}// goodpublicfunctionfoo()
{
// Do something...
}
  • In the argument list, there must not be a space before each comma, and there must be one space after each comma.
// badpublicfunctionfoo($bar∙,&$baz∙,$qux = [])
{
// Do something...
}
// badpublicfunctionfoo($bar, &$baz, $qux = []∙)
{
// Do something...
}
// goodpublicfunction foo($bar,&$baz,$qux∙=∙[])
{
// Do something...
}
  • 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.

// goodpublicfunctionfoo(
∙∙∙∙$bar,
∙∙∙∙&$baz,
∙∙∙∙$qux = []
) {
// Do something...
}
  • When present, the abstract and final declarations must precede the visibility declaration.

  • When present, the static declaration must come after the visibility declaration.

// badprotectedabstractfunctionfoo();
staticpublicfinalfunctionbar()
{
// Do something...
}
// goodabstractprotectedfunctionfoo();
finalpublicstaticfunctionbar()
{
// Do something...
}

Interfaces

  • The interface name must be suffixed with Interface.
<?phpnamespaceVendor\Foo;
/** * Interface Foo * */interface FooInterface
{
/** * Set Foo * * @param string $foo */publicfunctionsetFoo($foo);
}

Traits

  • The trait name must be suffixed with Trait.
<?phpnamespaceVendor\Foo;
/** * Trait Foo * */trait FooTrait
{
/** @var \Vendor\Bar */protected$bar;
/** * Set Bar * * @param string $bar */publicfunctionsetBar($bar)
{
$this->bar = $bar;
}
}
  • There must not be a space between the method or function name and the opening parenthesis.
// badfoo∙();
$bar->baz∙();
// goodfoo();
$bar->baz();
  • 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.
// badfoo(∙$qux∙);
$bar->baz(∙$qux∙);
// goodfoo($qux);
$bar->baz($qux);
  • There must not be a space before each comma and there must be one space after each comma.
// badfoo($bar∙,∙$baz∙,∙$qux);
// goodfoo($bar,∙$baz,∙$qux);
  • 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.
// badfoo($longFoo,
$longBar,
$longBaz
);
// badfoo($longFoo,
$longBar,
$longBaz);
// goodfoo(
∙∙∙∙$longFoo,
∙∙∙∙$longBar,
∙∙∙∙$longBaz
);
  • Chained method calls must be wrapped before the first call and indented once.
// bad$fooBar->baz()->qux($param);
// good$fooBar
->baz()
->qux($param);
  • When you pass an array as the only argument, the array brackets should be on the same lines as the method parenthesis.
// badfoo(
[
'foo' => 'bar',
]
);
// goodfoo([
'foo' => 'bar',
]);

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 of else if so that all control keywords look like single words.

Example

// badif(EXPRESSION){
// Do something...
}
// badif (EXPRESSION) {
// Do something...
}
// badif (EXPRESSION)
{
// Do something...
}
// badif (EXPRESSION) {
// Do something...
}
else {
// Do something...
}
// goodif∙(EXPRESSION)∙{
∙∙∙∙// Do something...
}∙elseif∙(OTHER_EXPRESSION)∙{
∙∙∙∙// Do something...
}∙else∙{
∙∙∙∙// Do something...
}

Ternary (?:)

  • You should not use nesting ternary.

Example

// bad$foo = EXPRESSION ? 'bar' : OTHER_EXPRESSION ? 'baz' : 'qux';
// good$foo∙=∙EXPRESSION∙?∙'bar'∙:∙'baz';
// good$foo∙=∙EXPRESSION
∙∙∙∙?∙'bar'
∙∙∙∙:∙'baz';

switch and case

  • The case statement must be indented once from switch, and the break keyword (or other terminating keyword) must be indented at the same level as the case body.

  • There must be a comment such as // no break when fall-through is intentional in a non-empty case body.

Example

// badswitch(EXPRESSION)
{
case0:
// Do something...break;
}
// goodswitch∙(EXPRESSION)∙{
∙∙∙∙case∙0:
∙∙∙∙∙∙∙∙// Do something...
∙∙∙∙∙∙∙∙break;
∙∙∙∙case∙1:
∙∙∙∙∙∙∙∙// Do something with no break...
∙∙∙∙∙∙∙∙// no break
∙∙∙∙case∙2:
∙∙∙∙case∙3:
∙∙∙∙case∙4:
∙∙∙∙∙∙∙∙// Do something with return instead of break...
∙∙∙∙∙∙∙∙return;
∙∙∙∙default:
∙∙∙∙∙∙∙∙// Do something in default case...
∙∙∙∙∙∙∙∙break;
}

while and do while

Example

// badwhile(EXPRESSION)
{
// Do something...
}
// baddo
{
// Do something...
} while(EXPRESSION);
// goodwhile∙(EXPRESSION)∙{
∙∙∙∙// Do something...
}
// good
do∙{
∙∙∙∙// Do something...
}∙while∙(EXPRESSION);

for

Example

// badfor( $i=0;$i<10;$i++ )
{
// Do something...
}
// goodfor∙($i∙=∙0;∙$i∙<∙10;∙$i++)∙{
∙∙∙∙// Do something...
}

foreach

Example

// badforeach( $fooas$key=>$value )
{
// Do something...
}
// goodforeach∙($foo∙as∙$key∙=>∙$value)∙{
∙∙∙∙// Do something...
}

try and catch

Example

// badtry
{
// Do something...
}
catch(FooException$e)
{
// Do something...
}
// good
try∙{
∙∙∙∙// Do something...
}∙catch∙(FooException∙$exception)∙{
∙∙∙∙// Do something...
}∙catch∙(BarException∙$exception)∙{
∙∙∙∙// Do something...
}∙finally∙{
∙∙∙∙// Do something...
}
  • 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)

// good$closureWithArguments∙=∙function∙($foo,∙$bar)∙{
∙∙∙∙// Do something...
};
// good$closureWithArgumentsAndVariables∙=∙function∙($foo,∙$bar)∙use∙($baz,∙$qux)∙{
∙∙∙∙// Do something...
};
// good$longArgumentsNoVariables∙=∙function∙(
∙∙∙∙$longArgumentFoo,
∙∙∙∙$longArgumentBar,
∙∙∙∙$longArgumentBaz
)∙{
∙∙∙∙// Do something...
};
// good$noArgumentsLongVariables∙=∙function∙()∙use∙(
∙∙∙∙$longVariableFoo,
∙∙∙∙$longVariablBar,
∙∙∙∙$longVariableBaz
)∙{
∙∙∙∙// Do something...
};
// good$longArgumentsLongVariables∙=∙function∙(
∙∙∙∙$longArgumentFoo,
∙∙∙∙$longArgumentBar,
∙∙∙∙$longArgumentBaz
)∙use∙(
∙∙∙∙$longVariableFoo,
∙∙∙∙$longVariableBar,
∙∙∙∙$longVariableBaz
)∙{
∙∙∙∙// Do something...
};
// good$longArgumentsShortVariables∙=∙function∙(
∙∙∙∙$longArgumentFoo,
∙∙∙∙$longArgumentBar,
∙∙∙∙$longArgumentBaz
)∙use∙($variableFoo)∙{
∙∙∙∙// Do something...
};
// good$shortArgumentsLongVariables∙=∙function∙($argumentFoo)∙use∙(
∙∙∙∙$longVariableFoo,
∙∙∙∙$longVariableBar,
∙∙∙∙$longVariableBaz
)∙{
∙∙∙∙// Do something...
};

Example (usage)

$foo->bar(
∙∙∙∙$argumentFoo,
∙∙∙∙function∙($argumentBar)∙use∙($variableFoo)∙{
∙∙∙∙∙∙∙∙// Do something...
∙∙∙∙},
∙∙∙∙$argumentBaz
);

Date

  • You must use new \DateTime('2014-01-01 00:00:00') instead of date('2014-01-01 00:00:00').

  • You must use new \DateTime('Sunday') instead of strtotime('Sunday').

Readibility

  • When possible, avoid nestings of more than 2 levels and prefer "return early" structures.
// bad$response = [];
if ($foo) {
foreach ($foo->getBars() as$bar) {
if ($bar->hasBaz()) {
// 3 nested levels
}
}
}
return$response;
// good$response = [];
if (!$foo) {
return$response;
}
foreach ($foo->getBars() as$bar) {
if ($bar->hasBaz()) {
// only 2 nested levels
}
}
return$response;

About

PHP conventions at iAdvize

Resources

Stars

16 stars

Watchers

11 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
This repository was archived by the owner on May 13, 2025. It is now read-only.

Repository files navigation

iAdvize PHP Style Guide

  1. IDE Integration
  2. Files
  3. Lines
  4. Keywords
  5. Comments
  6. Naming
  7. Variables
  8. Constants
  9. Type casting
  10. Namespaces and use declarations
  11. String
  12. Arrays
  13. Classes, Properties, and Methods
  14. Interfaces, Traits
  15. Function and Method Calls
  16. Control Structures
  17. Closures
  18. Best practices

Configure your PHPStorm

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

  • 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 <?php ?> tags for non-views scripts.

<?phpecho'test';
  • Use the short-echo <?= ?> tags for view scripts.
<title><?=$title;∙?></title>
  • 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.

  • Blank lines may be added to improve readability and to indicate related blocks of code.
// nahfunctionfoo()
{
$foo = 'test';
$foo = strtoupper($foo);
return$foo;
}
// goodfunctionbar()
{
$foo = 'test';
$foo = strtoupper($foo);
return$foo;
}
  • There must not be more than one statement per line.
// bad$foo = substr(strtoupper('test'), 0, 1);
// good$foo = 'test';
$foo = strtoupper($foo);
$foo = substr($foo, 0, 1);
  • All PHP keywords must be in lower case (Eg: true, false, null, etc.)
  • Namespaces names must be delcared in UpperCamelCase.
// badnamespaceVendor\fooBar;
// badnamespaceVendor\foo_bar;
// goodnamespaceVendor\FooBar;
  • 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:

// baduseFoo\Bar,
Qux\Quux,
Foo\Baz;
// baduseFoo\Bar;
useQux\Quux;
useFoo\Baz;
// gooduseFoo\Bar;
useFoo\Baz;
useQux\Quux;
useQux\Corge\Grault;
  • use alias declaration should be composed with sub-namespaces names.
// baduseFoo\BarasBaz;
// baduseBaz\Qux\QuuxasBQQ;
// gooduseFoo\BarasFooBar;
// gooduseBaz\Qux\QuuxasBazQuxQuux;

In-line code comments

  • Comments should be on a separate line immediately before the code line or block they reference.
// bad$foo = 'bar'; // Bar in foo// good// Foo assignment for example$foo = 'bar';
// good// Foo assignment// for example$foo = 'bar';

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.
/** * Foo * */class Foo
{
/** * The description of bar * * @param string $baz The baz * * @return string The return of bar */publicfunctionbar($baz)
{
// Returned valuereturn'Do something...';
}
}
  • You must add PHPDoc blocks for all variable references.
/** @var Bar\Baz $foo Baz object */
$foo = $bar->baz();
/** * Foo * */class Foo
{
/** @var string $bar It's bar! */public$bar = '';
}
  • 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.
// BadnamespaceVendor\Bar\Baz;
/** * Foo * */class Foo
{
/** @var \Other\MyClass $myClass */protected$myClass;
/** * @return \Other\MyClass */publicfunctiongetMyClass()
{
return$this->myClass;
}
}
// GoodnamespaceVendor\Bar\Baz;
useOther\MyClass;
/** * Foo * */class Foo
{
/** @var MyClass $myClass */protected$myClass;
/** * @return MyClass */publicfunctiongetMyClass()
{
return$this->myClass;
}
}
  • @todo and @fixme must be used in PHPDoc blocks like annotations.
/** @todo Think to check value */$foo = 'bar';
/** @fixme Change qux to quux */$baz = 'qux';

Qualify objects you use

  • you should add @var tag when you get object from abstract method
// bad$logger = $this->getServiceLocator()->get('logger');
// bad$this->getServiceLocator()->get('AwesomeFactory')->createAwesomeness();
// good/** @var LoggerInterface $logger */$logger = $this->getServiceLocator()->get('logger');
// good/** @var AwesomeFactory $awesomeFactory */$awesomeFactory = $this->getServiceLocator()->get('AwesomeFactory');
$awesomeFactory->createAwesomeness()
  • you shouldn't add @var tag when you get object from explicit method
// bad/** * Class AwesomeFactory */class AwesomeFactory
{
/** * @return Awesome */publicfunctioncreateAwesomeness()
{
returnAwesome();
}
}
$awesomeFactory = newAwesomeFactory();
/** @var Awesome $awesome */$awesome = $awesomeFactory->createAwesomeness();
// good/** * Class AwesomeFactory */class AwesomeFactory
{
/** * @return Awesome */publicfunctioncreateAwesomeness()
{
returnAwesome();
}
}
$awesomeFactory = newAwesomeFactory();
$awesome = $awesomeFactory->createAwesomeness();
  • Clarity over brevity in variable, method and class names
// bad$o = newObject();
// badclass A
{
}
// badpublicfunctiondoIt()
{
}
// good$object = newObject();
// goodclass Substracter
{
}
// goodpublicfunctionassociateChannelToOperator()
{
}
  • Boolean variable names should be either an adjective or a past participle form. Associated getter method should begin with is or has.
// bad$enablePlugin = true;
// badpublicfunctiongetEnablePlugin() {}
// badpublicfunctiongetPluginEnabled() {}
// good$pluginEnabled = true;
// good$visible = true;
// goodpublicfunctionisPluginEnabled() {}
// goodpublicfunctionisVisible() {}
  • DateTime variable names should be a past participle form ending with At.
// bad$dateUpdate = new \DateTime;
// bad$endDate = new \DateTime;
// good$updatedAt = new \DateTime;
// good$lastLoggedAt = new \DateTime;

User variables

  • Variables should be in lowerCamelCase.
// bad$_foo='';
// bad$foo_bar = '';
// bad$fooBar='';
// good$fooBar∙=∙'';
  • You must set off operators with spaces.
// bad$foo = (5+6)/5;
// good$foo∙=(5∙+∙6)∙/∙5;
  • You must conserve a great alignment.
// bad$foo = 'Ba';
$foo .= 'r';
$quux = 'Qu';
$quux .= 'x';
// good$foo = 'Ba';
$foo .= 'r';
$quux = 'Qu';
$quux .= 'x';
// bad$fooBarBazQux->bar()->
baz()->qux();
// good$fooBarBazQux∙∙∙∙->bar()
∙∙∙∙->baz()
∙∙∙∙->qux();

Global variables

  • You must used $_POST, $_GET and $_COOKIE instead of $_REQUEST. If you use a framework, use Request component.
  • 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

$foo = 'Bar'
.'Baz'
.'Qux';
  • A string must not concatenate with functions or methods
// bad$foo = ucfisrt('bar') . ' baz';
// good$foo = ucfirst('bar');
$foo = $foo . ' baz';
// very good$foo = ucfirst('bar');
$foo .= ' baz';

Class constants

  • Constants must be in UPPER_SNAKE_CASE.
// badconstMAXSIZE=5;
// badconstmax_size∙=∙4;
// goodconstMAX_SIZE∙=∙5;
  • You must set assignment operator with spaces.
// badconstMAX_SIZE=5;
// goodconstMAX_SIZE∙=∙5;
  • You must conserve a great alignment.
// badconstFOO∙∙='Ba';
constFOO∙∙='r';
constQUUX∙='Qu';
constQUUX∙='x';
// goodconstFOO∙∙='Ba';
constFOO∙∙='r';
constQUUX∙='Qu';
constQUUX∙='x';
  • You must use (int) $foo instead of intval($foo).

  • You must use (bool) $foo instead of boolval($foo).

  • You must use (float) $foo instead of floatval($foo).

  • You must use (string) $foo instead of strval($foo).

// bad$foo∙=(string)$bar;
// good$foo∙=(string)∙$bar;
  • You must use [] notation instead of array().

  • Arrays with few data must be declared like this:

$foo∙=∙['Bar',∙'Baz',∙'Qux'];
  • Arrays with lots of data must be declared like this:
$foo = [
∙∙∙∙'bar'∙∙=>∙'abc',
∙∙∙∙'baz'∙∙=>∙123,
∙∙∙∙'qux'∙∙=>∙true,
∙∙∙∙'quux'∙=>∙[
∙∙∙∙∙∙∙∙'corge'∙∙=>∙[],
∙∙∙∙∙∙∙∙'grault'∙=>∙123.456,
∙∙∙∙],
];
  • For the arrays with lots of data, lines must be terminated by a comma. (Easy to copy/paste)

Classes

  • The extends and implements 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.

<?phpnamespaceVendor\Foo;
class Foo extends Bar implements Baz, Qux, Quux
{
// Do something...
}
  • 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.
<?phpnamespaceVendor\Foo;
class Foo extends Bar implements
∙∙∙∙Baz,
∙∙∙∙Qux,
∙∙∙∙Quux
{
// Do something...
}

Properties

  • Visibility must be declared on all properties.
// bad/** @var string Property description */$foo = '';
// good/** @var string Property description */public$foo = '';
  • There must not be more than one property declared per statement.
// badpublic$foo = '',
$bar = '';
// good/** @var string Property description */public$foo = '';
/** @var string Property description */protected$bar = '';
  • Property names must not be prefixed with a single underscore to indicate protected or private visibility.
// bad/** @var string Property description */protected$_bar = '';
/** @var string Property description */private$_baz = '';
// good/** @var string Property description */protected$bar = '';
/** @var string Property description */private$baz = '';
  • When present, the static declaration must come after the visibility declaration.
// bad/** @var string $foo Property description */staticpublic$foo = '';
// good/** @var string $foo Property description */publicstatic$foo = '';

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.

// badprotectedfunction_foo()
{
// Do something...
}
// goodprotectedfunctionfoo()
{
// Do something...
}
  • Method names must not be declared with a space after the method name.
// badpublicfunctionfoo∙()
{
// Do something...
}
// goodpublicfunctionfoo()
{
// Do something...
}
  • There must not be a space after the opening parenthesis, and there must not be a space before the closing parenthesis.
// badpublicfunction foo()∙{∙
// Do something...
∙}
// goodpublicfunctionfoo()
{
// Do something...
}
  • The opening brace must go on its own line, and the closing brace must go on the next line following the body.
// badpublicfunction foo()∙{
// Do something...}// goodpublicfunctionfoo()
{
// Do something...
}
  • In the argument list, there must not be a space before each comma, and there must be one space after each comma.
// badpublicfunctionfoo($bar∙,&$baz∙,$qux = [])
{
// Do something...
}
// badpublicfunctionfoo($bar, &$baz, $qux = []∙)
{
// Do something...
}
// goodpublicfunction foo($bar,&$baz,$qux∙=∙[])
{
// Do something...
}
  • 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.

// goodpublicfunctionfoo(
∙∙∙∙$bar,
∙∙∙∙&$baz,
∙∙∙∙$qux = []
) {
// Do something...
}
  • When present, the abstract and final declarations must precede the visibility declaration.

  • When present, the static declaration must come after the visibility declaration.

// badprotectedabstractfunctionfoo();
staticpublicfinalfunctionbar()
{
// Do something...
}
// goodabstractprotectedfunctionfoo();
finalpublicstaticfunctionbar()
{
// Do something...
}

Interfaces

  • The interface name must be suffixed with Interface.
<?phpnamespaceVendor\Foo;
/** * Interface Foo * */interface FooInterface
{
/** * Set Foo * * @param string $foo */publicfunctionsetFoo($foo);
}

Traits

  • The trait name must be suffixed with Trait.
<?phpnamespaceVendor\Foo;
/** * Trait Foo * */trait FooTrait
{
/** @var \Vendor\Bar */protected$bar;
/** * Set Bar * * @param string $bar */publicfunctionsetBar($bar)
{
$this->bar = $bar;
}
}
  • There must not be a space between the method or function name and the opening parenthesis.
// badfoo∙();
$bar->baz∙();
// goodfoo();
$bar->baz();
  • 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.
// badfoo(∙$qux∙);
$bar->baz(∙$qux∙);
// goodfoo($qux);
$bar->baz($qux);
  • There must not be a space before each comma and there must be one space after each comma.
// badfoo($bar∙,∙$baz∙,∙$qux);
// goodfoo($bar,∙$baz,∙$qux);
  • 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.
// badfoo($longFoo,
$longBar,
$longBaz
);
// badfoo($longFoo,
$longBar,
$longBaz);
// goodfoo(
∙∙∙∙$longFoo,
∙∙∙∙$longBar,
∙∙∙∙$longBaz
);
  • Chained method calls must be wrapped before the first call and indented once.
// bad$fooBar->baz()->qux($param);
// good$fooBar
->baz()
->qux($param);
  • When you pass an array as the only argument, the array brackets should be on the same lines as the method parenthesis.
// badfoo(
[
'foo' => 'bar',
]
);
// goodfoo([
'foo' => 'bar',
]);

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 of else if so that all control keywords look like single words.

Example

// badif(EXPRESSION){
// Do something...
}
// badif (EXPRESSION) {
// Do something...
}
// badif (EXPRESSION)
{
// Do something...
}
// badif (EXPRESSION) {
// Do something...
}
else {
// Do something...
}
// goodif∙(EXPRESSION)∙{
∙∙∙∙// Do something...
}∙elseif∙(OTHER_EXPRESSION)∙{
∙∙∙∙// Do something...
}∙else∙{
∙∙∙∙// Do something...
}

Ternary (?:)

  • You should not use nesting ternary.

Example

// bad$foo = EXPRESSION ? 'bar' : OTHER_EXPRESSION ? 'baz' : 'qux';
// good$foo∙=∙EXPRESSION∙?∙'bar'∙:∙'baz';
// good$foo∙=∙EXPRESSION
∙∙∙∙?∙'bar'
∙∙∙∙:∙'baz';

switch and case

  • The case statement must be indented once from switch, and the break keyword (or other terminating keyword) must be indented at the same level as the case body.

  • There must be a comment such as // no break when fall-through is intentional in a non-empty case body.

Example

// badswitch(EXPRESSION)
{
case0:
// Do something...break;
}
// goodswitch∙(EXPRESSION)∙{
∙∙∙∙case∙0:
∙∙∙∙∙∙∙∙// Do something...
∙∙∙∙∙∙∙∙break;
∙∙∙∙case∙1:
∙∙∙∙∙∙∙∙// Do something with no break...
∙∙∙∙∙∙∙∙// no break
∙∙∙∙case∙2:
∙∙∙∙case∙3:
∙∙∙∙case∙4:
∙∙∙∙∙∙∙∙// Do something with return instead of break...
∙∙∙∙∙∙∙∙return;
∙∙∙∙default:
∙∙∙∙∙∙∙∙// Do something in default case...
∙∙∙∙∙∙∙∙break;
}

while and do while

Example

// badwhile(EXPRESSION)
{
// Do something...
}
// baddo
{
// Do something...
} while(EXPRESSION);
// goodwhile∙(EXPRESSION)∙{
∙∙∙∙// Do something...
}
// good
do∙{
∙∙∙∙// Do something...
}∙while∙(EXPRESSION);

for

Example

// badfor( $i=0;$i<10;$i++ )
{
// Do something...
}
// goodfor∙($i∙=∙0;∙$i∙<∙10;∙$i++)∙{
∙∙∙∙// Do something...
}

foreach

Example

// badforeach( $fooas$key=>$value )
{
// Do something...
}
// goodforeach∙($foo∙as∙$key∙=>∙$value)∙{
∙∙∙∙// Do something...
}

try and catch

Example

// badtry
{
// Do something...
}
catch(FooException$e)
{
// Do something...
}
// good
try∙{
∙∙∙∙// Do something...
}∙catch∙(FooException∙$exception)∙{
∙∙∙∙// Do something...
}∙catch∙(BarException∙$exception)∙{
∙∙∙∙// Do something...
}∙finally∙{
∙∙∙∙// Do something...
}
  • 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)

// good$closureWithArguments∙=∙function∙($foo,∙$bar)∙{
∙∙∙∙// Do something...
};
// good$closureWithArgumentsAndVariables∙=∙function∙($foo,∙$bar)∙use∙($baz,∙$qux)∙{
∙∙∙∙// Do something...
};
// good$longArgumentsNoVariables∙=∙function∙(
∙∙∙∙$longArgumentFoo,
∙∙∙∙$longArgumentBar,
∙∙∙∙$longArgumentBaz
)∙{
∙∙∙∙// Do something...
};
// good$noArgumentsLongVariables∙=∙function∙()∙use∙(
∙∙∙∙$longVariableFoo,
∙∙∙∙$longVariablBar,
∙∙∙∙$longVariableBaz
)∙{
∙∙∙∙// Do something...
};
// good$longArgumentsLongVariables∙=∙function∙(
∙∙∙∙$longArgumentFoo,
∙∙∙∙$longArgumentBar,
∙∙∙∙$longArgumentBaz
)∙use∙(
∙∙∙∙$longVariableFoo,
∙∙∙∙$longVariableBar,
∙∙∙∙$longVariableBaz
)∙{
∙∙∙∙// Do something...
};
// good$longArgumentsShortVariables∙=∙function∙(
∙∙∙∙$longArgumentFoo,
∙∙∙∙$longArgumentBar,
∙∙∙∙$longArgumentBaz
)∙use∙($variableFoo)∙{
∙∙∙∙// Do something...
};
// good$shortArgumentsLongVariables∙=∙function∙($argumentFoo)∙use∙(
∙∙∙∙$longVariableFoo,
∙∙∙∙$longVariableBar,
∙∙∙∙$longVariableBaz
)∙{
∙∙∙∙// Do something...
};

Example (usage)

$foo->bar(
∙∙∙∙$argumentFoo,
∙∙∙∙function∙($argumentBar)∙use∙($variableFoo)∙{
∙∙∙∙∙∙∙∙// Do something...
∙∙∙∙},
∙∙∙∙$argumentBaz
);

Date

  • You must use new \DateTime('2014-01-01 00:00:00') instead of date('2014-01-01 00:00:00').

  • You must use new \DateTime('Sunday') instead of strtotime('Sunday').

Readibility

  • When possible, avoid nestings of more than 2 levels and prefer "return early" structures.
// bad$response = [];
if ($foo) {
foreach ($foo->getBars() as$bar) {
if ($bar->hasBaz()) {
// 3 nested levels
}
}
}
return$response;
// good$response = [];
if (!$foo) {
return$response;
}
foreach ($foo->getBars() as$bar) {
if ($bar->hasBaz()) {
// only 2 nested levels
}
}
return$response;

About

PHP conventions at iAdvize

Resources

Stars

16 stars

Watchers

11 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content
This repository was archived by the owner on May 13, 2025. It is now read-only.

Repository files navigation

iAdvize PHP Style Guide

  1. IDE Integration
  2. Files
  3. Lines
  4. Keywords
  5. Comments
  6. Naming
  7. Variables
  8. Constants
  9. Type casting
  10. Namespaces and use declarations
  11. String
  12. Arrays
  13. Classes, Properties, and Methods
  14. Interfaces, Traits
  15. Function and Method Calls
  16. Control Structures
  17. Closures
  18. Best practices

Configure your PHPStorm

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

  • 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 <?php ?> tags for non-views scripts.

<?phpecho'test';
  • Use the short-echo <?= ?> tags for view scripts.
<title><?=$title;∙?></title>
  • 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.

  • Blank lines may be added to improve readability and to indicate related blocks of code.
// nahfunctionfoo()
{
$foo = 'test';
$foo = strtoupper($foo);
return$foo;
}
// goodfunctionbar()
{
$foo = 'test';
$foo = strtoupper($foo);
return$foo;
}
  • There must not be more than one statement per line.
// bad$foo = substr(strtoupper('test'), 0, 1);
// good$foo = 'test';
$foo = strtoupper($foo);
$foo = substr($foo, 0, 1);
  • All PHP keywords must be in lower case (Eg: true, false, null, etc.)
  • Namespaces names must be delcared in UpperCamelCase.
// badnamespaceVendor\fooBar;
// badnamespaceVendor\foo_bar;
// goodnamespaceVendor\FooBar;
  • 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:

// baduseFoo\Bar,
Qux\Quux,
Foo\Baz;
// baduseFoo\Bar;
useQux\Quux;
useFoo\Baz;
// gooduseFoo\Bar;
useFoo\Baz;
useQux\Quux;
useQux\Corge\Grault;
  • use alias declaration should be composed with sub-namespaces names.
// baduseFoo\BarasBaz;
// baduseBaz\Qux\QuuxasBQQ;
// gooduseFoo\BarasFooBar;
// gooduseBaz\Qux\QuuxasBazQuxQuux;

In-line code comments

  • Comments should be on a separate line immediately before the code line or block they reference.
// bad$foo = 'bar'; // Bar in foo// good// Foo assignment for example$foo = 'bar';
// good// Foo assignment// for example$foo = 'bar';

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.
/** * Foo * */class Foo
{
/** * The description of bar * * @param string $baz The baz * * @return string The return of bar */publicfunctionbar($baz)
{
// Returned valuereturn'Do something...';
}
}
  • You must add PHPDoc blocks for all variable references.
/** @var Bar\Baz $foo Baz object */
$foo = $bar->baz();
/** * Foo * */class Foo
{
/** @var string $bar It's bar! */public$bar = '';
}
  • 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.
// BadnamespaceVendor\Bar\Baz;
/** * Foo * */class Foo
{
/** @var \Other\MyClass $myClass */protected$myClass;
/** * @return \Other\MyClass */publicfunctiongetMyClass()
{
return$this->myClass;
}
}
// GoodnamespaceVendor\Bar\Baz;
useOther\MyClass;
/** * Foo * */class Foo
{
/** @var MyClass $myClass */protected$myClass;
/** * @return MyClass */publicfunctiongetMyClass()
{
return$this->myClass;
}
}
  • @todo and @fixme must be used in PHPDoc blocks like annotations.
/** @todo Think to check value */$foo = 'bar';
/** @fixme Change qux to quux */$baz = 'qux';

Qualify objects you use

  • you should add @var tag when you get object from abstract method
// bad$logger = $this->getServiceLocator()->get('logger');
// bad$this->getServiceLocator()->get('AwesomeFactory')->createAwesomeness();
// good/** @var LoggerInterface $logger */$logger = $this->getServiceLocator()->get('logger');
// good/** @var AwesomeFactory $awesomeFactory */$awesomeFactory = $this->getServiceLocator()->get('AwesomeFactory');
$awesomeFactory->createAwesomeness()
  • you shouldn't add @var tag when you get object from explicit method
// bad/** * Class AwesomeFactory */class AwesomeFactory
{
/** * @return Awesome */publicfunctioncreateAwesomeness()
{
returnAwesome();
}
}
$awesomeFactory = newAwesomeFactory();
/** @var Awesome $awesome */$awesome = $awesomeFactory->createAwesomeness();
// good/** * Class AwesomeFactory */class AwesomeFactory
{
/** * @return Awesome */publicfunctioncreateAwesomeness()
{
returnAwesome();
}
}
$awesomeFactory = newAwesomeFactory();
$awesome = $awesomeFactory->createAwesomeness();
  • Clarity over brevity in variable, method and class names
// bad$o = newObject();
// badclass A
{
}
// badpublicfunctiondoIt()
{
}
// good$object = newObject();
// goodclass Substracter
{
}
// goodpublicfunctionassociateChannelToOperator()
{
}
  • Boolean variable names should be either an adjective or a past participle form. Associated getter method should begin with is or has.
// bad$enablePlugin = true;
// badpublicfunctiongetEnablePlugin() {}
// badpublicfunctiongetPluginEnabled() {}
// good$pluginEnabled = true;
// good$visible = true;
// goodpublicfunctionisPluginEnabled() {}
// goodpublicfunctionisVisible() {}
  • DateTime variable names should be a past participle form ending with At.
// bad$dateUpdate = new \DateTime;
// bad$endDate = new \DateTime;
// good$updatedAt = new \DateTime;
// good$lastLoggedAt = new \DateTime;

User variables

  • Variables should be in lowerCamelCase.
// bad$_foo='';
// bad$foo_bar = '';
// bad$fooBar='';
// good$fooBar∙=∙'';
  • You must set off operators with spaces.
// bad$foo = (5+6)/5;
// good$foo∙=(5∙+∙6)∙/∙5;
  • You must conserve a great alignment.
// bad$foo = 'Ba';
$foo .= 'r';
$quux = 'Qu';
$quux .= 'x';
// good$foo = 'Ba';
$foo .= 'r';
$quux = 'Qu';
$quux .= 'x';
// bad$fooBarBazQux->bar()->
baz()->qux();
// good$fooBarBazQux∙∙∙∙->bar()
∙∙∙∙->baz()
∙∙∙∙->qux();

Global variables

  • You must used $_POST, $_GET and $_COOKIE instead of $_REQUEST. If you use a framework, use Request component.
  • 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

$foo = 'Bar'
.'Baz'
.'Qux';
  • A string must not concatenate with functions or methods
// bad$foo = ucfisrt('bar') . ' baz';
// good$foo = ucfirst('bar');
$foo = $foo . ' baz';
// very good$foo = ucfirst('bar');
$foo .= ' baz';

Class constants

  • Constants must be in UPPER_SNAKE_CASE.
// badconstMAXSIZE=5;
// badconstmax_size∙=∙4;
// goodconstMAX_SIZE∙=∙5;
  • You must set assignment operator with spaces.
// badconstMAX_SIZE=5;
// goodconstMAX_SIZE∙=∙5;
  • You must conserve a great alignment.
// badconstFOO∙∙='Ba';
constFOO∙∙='r';
constQUUX∙='Qu';
constQUUX∙='x';
// goodconstFOO∙∙='Ba';
constFOO∙∙='r';
constQUUX∙='Qu';
constQUUX∙='x';
  • You must use (int) $foo instead of intval($foo).

  • You must use (bool) $foo instead of boolval($foo).

  • You must use (float) $foo instead of floatval($foo).

  • You must use (string) $foo instead of strval($foo).

// bad$foo∙=(string)$bar;
// good$foo∙=(string)∙$bar;
  • You must use [] notation instead of array().

  • Arrays with few data must be declared like this:

$foo∙=∙['Bar',∙'Baz',∙'Qux'];
  • Arrays with lots of data must be declared like this:
$foo = [
∙∙∙∙'bar'∙∙=>∙'abc',
∙∙∙∙'baz'∙∙=>∙123,
∙∙∙∙'qux'∙∙=>∙true,
∙∙∙∙'quux'∙=>∙[
∙∙∙∙∙∙∙∙'corge'∙∙=>∙[],
∙∙∙∙∙∙∙∙'grault'∙=>∙123.456,
∙∙∙∙],
];
  • For the arrays with lots of data, lines must be terminated by a comma. (Easy to copy/paste)

Classes

  • The extends and implements 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.

<?phpnamespaceVendor\Foo;
class Foo extends Bar implements Baz, Qux, Quux
{
// Do something...
}
  • 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.
<?phpnamespaceVendor\Foo;
class Foo extends Bar implements
∙∙∙∙Baz,
∙∙∙∙Qux,
∙∙∙∙Quux
{
// Do something...
}

Properties

  • Visibility must be declared on all properties.
// bad/** @var string Property description */$foo = '';
// good/** @var string Property description */public$foo = '';
  • There must not be more than one property declared per statement.
// badpublic$foo = '',
$bar = '';
// good/** @var string Property description */public$foo = '';
/** @var string Property description */protected$bar = '';
  • Property names must not be prefixed with a single underscore to indicate protected or private visibility.
// bad/** @var string Property description */protected$_bar = '';
/** @var string Property description */private$_baz = '';
// good/** @var string Property description */protected$bar = '';
/** @var string Property description */private$baz = '';
  • When present, the static declaration must come after the visibility declaration.
// bad/** @var string $foo Property description */staticpublic$foo = '';
// good/** @var string $foo Property description */publicstatic$foo = '';

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.

// badprotectedfunction_foo()
{
// Do something...
}
// goodprotectedfunctionfoo()
{
// Do something...
}
  • Method names must not be declared with a space after the method name.
// badpublicfunctionfoo∙()
{
// Do something...
}
// goodpublicfunctionfoo()
{
// Do something...
}
  • There must not be a space after the opening parenthesis, and there must not be a space before the closing parenthesis.
// badpublicfunction foo()∙{∙
// Do something...
∙}
// goodpublicfunctionfoo()
{
// Do something...
}
  • The opening brace must go on its own line, and the closing brace must go on the next line following the body.
// badpublicfunction foo()∙{
// Do something...}// goodpublicfunctionfoo()
{
// Do something...
}
  • In the argument list, there must not be a space before each comma, and there must be one space after each comma.
// badpublicfunctionfoo($bar∙,&$baz∙,$qux = [])
{
// Do something...
}
// badpublicfunctionfoo($bar, &$baz, $qux = []∙)
{
// Do something...
}
// goodpublicfunction foo($bar,&$baz,$qux∙=∙[])
{
// Do something...
}
  • 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.

// goodpublicfunctionfoo(
∙∙∙∙$bar,
∙∙∙∙&$baz,
∙∙∙∙$qux = []
) {
// Do something...
}
  • When present, the abstract and final declarations must precede the visibility declaration.

  • When present, the static declaration must come after the visibility declaration.

// badprotectedabstractfunctionfoo();
staticpublicfinalfunctionbar()
{
// Do something...
}
// goodabstractprotectedfunctionfoo();
finalpublicstaticfunctionbar()
{
// Do something...
}

Interfaces

  • The interface name must be suffixed with Interface.
<?phpnamespaceVendor\Foo;
/** * Interface Foo * */interface FooInterface
{
/** * Set Foo * * @param string $foo */publicfunctionsetFoo($foo);
}

Traits

  • The trait name must be suffixed with Trait.
<?phpnamespaceVendor\Foo;
/** * Trait Foo * */trait FooTrait
{
/** @var \Vendor\Bar */protected$bar;
/** * Set Bar * * @param string $bar */publicfunctionsetBar($bar)
{
$this->bar = $bar;
}
}
  • There must not be a space between the method or function name and the opening parenthesis.
// badfoo∙();
$bar->baz∙();
// goodfoo();
$bar->baz();
  • 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.
// badfoo(∙$qux∙);
$bar->baz(∙$qux∙);
// goodfoo($qux);
$bar->baz($qux);
  • There must not be a space before each comma and there must be one space after each comma.
// badfoo($bar∙,∙$baz∙,∙$qux);
// goodfoo($bar,∙$baz,∙$qux);
  • 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.
// badfoo($longFoo,
$longBar,
$longBaz
);
// badfoo($longFoo,
$longBar,
$longBaz);
// goodfoo(
∙∙∙∙$longFoo,
∙∙∙∙$longBar,
∙∙∙∙$longBaz
);
  • Chained method calls must be wrapped before the first call and indented once.
// bad$fooBar->baz()->qux($param);
// good$fooBar
->baz()
->qux($param);
  • When you pass an array as the only argument, the array brackets should be on the same lines as the method parenthesis.
// badfoo(
[
'foo' => 'bar',
]
);
// goodfoo([
'foo' => 'bar',
]);

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 of else if so that all control keywords look like single words.

Example

// badif(EXPRESSION){
// Do something...
}
// badif (EXPRESSION) {
// Do something...
}
// badif (EXPRESSION)
{
// Do something...
}
// badif (EXPRESSION) {
// Do something...
}
else {
// Do something...
}
// goodif∙(EXPRESSION)∙{
∙∙∙∙// Do something...
}∙elseif∙(OTHER_EXPRESSION)∙{
∙∙∙∙// Do something...
}∙else∙{
∙∙∙∙// Do something...
}

Ternary (?:)

  • You should not use nesting ternary.

Example

// bad$foo = EXPRESSION ? 'bar' : OTHER_EXPRESSION ? 'baz' : 'qux';
// good$foo∙=∙EXPRESSION∙?∙'bar'∙:∙'baz';
// good$foo∙=∙EXPRESSION
∙∙∙∙?∙'bar'
∙∙∙∙:∙'baz';

switch and case

  • The case statement must be indented once from switch, and the break keyword (or other terminating keyword) must be indented at the same level as the case body.

  • There must be a comment such as // no break when fall-through is intentional in a non-empty case body.

Example

// badswitch(EXPRESSION)
{
case0:
// Do something...break;
}
// goodswitch∙(EXPRESSION)∙{
∙∙∙∙case∙0:
∙∙∙∙∙∙∙∙// Do something...
∙∙∙∙∙∙∙∙break;
∙∙∙∙case∙1:
∙∙∙∙∙∙∙∙// Do something with no break...
∙∙∙∙∙∙∙∙// no break
∙∙∙∙case∙2:
∙∙∙∙case∙3:
∙∙∙∙case∙4:
∙∙∙∙∙∙∙∙// Do something with return instead of break...
∙∙∙∙∙∙∙∙return;
∙∙∙∙default:
∙∙∙∙∙∙∙∙// Do something in default case...
∙∙∙∙∙∙∙∙break;
}

while and do while

Example

// badwhile(EXPRESSION)
{
// Do something...
}
// baddo
{
// Do something...
} while(EXPRESSION);
// goodwhile∙(EXPRESSION)∙{
∙∙∙∙// Do something...
}
// good
do∙{
∙∙∙∙// Do something...
}∙while∙(EXPRESSION);

for

Example

// badfor( $i=0;$i<10;$i++ )
{
// Do something...
}
// goodfor∙($i∙=∙0;∙$i∙<∙10;∙$i++)∙{
∙∙∙∙// Do something...
}

foreach

Example

// badforeach( $fooas$key=>$value )
{
// Do something...
}
// goodforeach∙($foo∙as∙$key∙=>∙$value)∙{
∙∙∙∙// Do something...
}

try and catch

Example

// badtry
{
// Do something...
}
catch(FooException$e)
{
// Do something...
}
// good
try∙{
∙∙∙∙// Do something...
}∙catch∙(FooException∙$exception)∙{
∙∙∙∙// Do something...
}∙catch∙(BarException∙$exception)∙{
∙∙∙∙// Do something...
}∙finally∙{
∙∙∙∙// Do something...
}
  • 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)

// good$closureWithArguments∙=∙function∙($foo,∙$bar)∙{
∙∙∙∙// Do something...
};
// good$closureWithArgumentsAndVariables∙=∙function∙($foo,∙$bar)∙use∙($baz,∙$qux)∙{
∙∙∙∙// Do something...
};
// good$longArgumentsNoVariables∙=∙function∙(
∙∙∙∙$longArgumentFoo,
∙∙∙∙$longArgumentBar,
∙∙∙∙$longArgumentBaz
)∙{
∙∙∙∙// Do something...
};
// good$noArgumentsLongVariables∙=∙function∙()∙use∙(
∙∙∙∙$longVariableFoo,
∙∙∙∙$longVariablBar,
∙∙∙∙$longVariableBaz
)∙{
∙∙∙∙// Do something...
};
// good$longArgumentsLongVariables∙=∙function∙(
∙∙∙∙$longArgumentFoo,
∙∙∙∙$longArgumentBar,
∙∙∙∙$longArgumentBaz
)∙use∙(
∙∙∙∙$longVariableFoo,
∙∙∙∙$longVariableBar,
∙∙∙∙$longVariableBaz
)∙{
∙∙∙∙// Do something...
};
// good$longArgumentsShortVariables∙=∙function∙(
∙∙∙∙$longArgumentFoo,
∙∙∙∙$longArgumentBar,
∙∙∙∙$longArgumentBaz
)∙use∙($variableFoo)∙{
∙∙∙∙// Do something...
};
// good$shortArgumentsLongVariables∙=∙function∙($argumentFoo)∙use∙(
∙∙∙∙$longVariableFoo,
∙∙∙∙$longVariableBar,
∙∙∙∙$longVariableBaz
)∙{
∙∙∙∙// Do something...
};

Example (usage)

$foo->bar(
∙∙∙∙$argumentFoo,
∙∙∙∙function∙($argumentBar)∙use∙($variableFoo)∙{
∙∙∙∙∙∙∙∙// Do something...
∙∙∙∙},
∙∙∙∙$argumentBaz
);

Date

  • You must use new \DateTime('2014-01-01 00:00:00') instead of date('2014-01-01 00:00:00').

  • You must use new \DateTime('Sunday') instead of strtotime('Sunday').

Readibility

  • When possible, avoid nestings of more than 2 levels and prefer "return early" structures.
// bad$response = [];
if ($foo) {
foreach ($foo->getBars() as$bar) {
if ($bar->hasBaz()) {
// 3 nested levels
}
}
}
return$response;
// good$response = [];
if (!$foo) {
return$response;
}
foreach ($foo->getBars() as$bar) {
if ($bar->hasBaz()) {
// only 2 nested levels
}
}
return$response;

About

PHP conventions at iAdvize

Resources

Stars

16 stars

Watchers

11 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
This repository was archived by the owner on May 13, 2025. It is now read-only.

Repository files navigation

iAdvize PHP Style Guide

  1. IDE Integration
  2. Files
  3. Lines
  4. Keywords
  5. Comments
  6. Naming
  7. Variables
  8. Constants
  9. Type casting
  10. Namespaces and use declarations
  11. String
  12. Arrays
  13. Classes, Properties, and Methods
  14. Interfaces, Traits
  15. Function and Method Calls
  16. Control Structures
  17. Closures
  18. Best practices

Configure your PHPStorm

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

  • 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 <?php ?> tags for non-views scripts.

<?phpecho'test';
  • Use the short-echo <?= ?> tags for view scripts.
<title><?=$title;∙?></title>
  • 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.

  • Blank lines may be added to improve readability and to indicate related blocks of code.
// nahfunctionfoo()
{
$foo = 'test';
$foo = strtoupper($foo);
return$foo;
}
// goodfunctionbar()
{
$foo = 'test';
$foo = strtoupper($foo);
return$foo;
}
  • There must not be more than one statement per line.
// bad$foo = substr(strtoupper('test'), 0, 1);
// good$foo = 'test';
$foo = strtoupper($foo);
$foo = substr($foo, 0, 1);
  • All PHP keywords must be in lower case (Eg: true, false, null, etc.)
  • Namespaces names must be delcared in UpperCamelCase.
// badnamespaceVendor\fooBar;
// badnamespaceVendor\foo_bar;
// goodnamespaceVendor\FooBar;
  • 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:

// baduseFoo\Bar,
Qux\Quux,
Foo\Baz;
// baduseFoo\Bar;
useQux\Quux;
useFoo\Baz;
// gooduseFoo\Bar;
useFoo\Baz;
useQux\Quux;
useQux\Corge\Grault;
  • use alias declaration should be composed with sub-namespaces names.
// baduseFoo\BarasBaz;
// baduseBaz\Qux\QuuxasBQQ;
// gooduseFoo\BarasFooBar;
// gooduseBaz\Qux\QuuxasBazQuxQuux;

In-line code comments

  • Comments should be on a separate line immediately before the code line or block they reference.
// bad$foo = 'bar'; // Bar in foo// good// Foo assignment for example$foo = 'bar';
// good// Foo assignment// for example$foo = 'bar';

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.
/** * Foo * */class Foo
{
/** * The description of bar * * @param string $baz The baz * * @return string The return of bar */publicfunctionbar($baz)
{
// Returned valuereturn'Do something...';
}
}
  • You must add PHPDoc blocks for all variable references.
/** @var Bar\Baz $foo Baz object */
$foo = $bar->baz();
/** * Foo * */class Foo
{
/** @var string $bar It's bar! */public$bar = '';
}
  • 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.
// BadnamespaceVendor\Bar\Baz;
/** * Foo * */class Foo
{
/** @var \Other\MyClass $myClass */protected$myClass;
/** * @return \Other\MyClass */publicfunctiongetMyClass()
{
return$this->myClass;
}
}
// GoodnamespaceVendor\Bar\Baz;
useOther\MyClass;
/** * Foo * */class Foo
{
/** @var MyClass $myClass */protected$myClass;
/** * @return MyClass */publicfunctiongetMyClass()
{
return$this->myClass;
}
}
  • @todo and @fixme must be used in PHPDoc blocks like annotations.
/** @todo Think to check value */$foo = 'bar';
/** @fixme Change qux to quux */$baz = 'qux';

Qualify objects you use

  • you should add @var tag when you get object from abstract method
// bad$logger = $this->getServiceLocator()->get('logger');
// bad$this->getServiceLocator()->get('AwesomeFactory')->createAwesomeness();
// good/** @var LoggerInterface $logger */$logger = $this->getServiceLocator()->get('logger');
// good/** @var AwesomeFactory $awesomeFactory */$awesomeFactory = $this->getServiceLocator()->get('AwesomeFactory');
$awesomeFactory->createAwesomeness()
  • you shouldn't add @var tag when you get object from explicit method
// bad/** * Class AwesomeFactory */class AwesomeFactory
{
/** * @return Awesome */publicfunctioncreateAwesomeness()
{
returnAwesome();
}
}
$awesomeFactory = newAwesomeFactory();
/** @var Awesome $awesome */$awesome = $awesomeFactory->createAwesomeness();
// good/** * Class AwesomeFactory */class AwesomeFactory
{
/** * @return Awesome */publicfunctioncreateAwesomeness()
{
returnAwesome();
}
}
$awesomeFactory = newAwesomeFactory();
$awesome = $awesomeFactory->createAwesomeness();
  • Clarity over brevity in variable, method and class names
// bad$o = newObject();
// badclass A
{
}
// badpublicfunctiondoIt()
{
}
// good$object = newObject();
// goodclass Substracter
{
}
// goodpublicfunctionassociateChannelToOperator()
{
}
  • Boolean variable names should be either an adjective or a past participle form. Associated getter method should begin with is or has.
// bad$enablePlugin = true;
// badpublicfunctiongetEnablePlugin() {}
// badpublicfunctiongetPluginEnabled() {}
// good$pluginEnabled = true;
// good$visible = true;
// goodpublicfunctionisPluginEnabled() {}
// goodpublicfunctionisVisible() {}
  • DateTime variable names should be a past participle form ending with At.
// bad$dateUpdate = new \DateTime;
// bad$endDate = new \DateTime;
// good$updatedAt = new \DateTime;
// good$lastLoggedAt = new \DateTime;

User variables

  • Variables should be in lowerCamelCase.
// bad$_foo='';
// bad$foo_bar = '';
// bad$fooBar='';
// good$fooBar∙=∙'';
  • You must set off operators with spaces.
// bad$foo = (5+6)/5;
// good$foo∙=(5∙+∙6)∙/∙5;
  • You must conserve a great alignment.
// bad$foo = 'Ba';
$foo .= 'r';
$quux = 'Qu';
$quux .= 'x';
// good$foo = 'Ba';
$foo .= 'r';
$quux = 'Qu';
$quux .= 'x';
// bad$fooBarBazQux->bar()->
baz()->qux();
// good$fooBarBazQux∙∙∙∙->bar()
∙∙∙∙->baz()
∙∙∙∙->qux();

Global variables

  • You must used $_POST, $_GET and $_COOKIE instead of $_REQUEST. If you use a framework, use Request component.
  • 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

$foo = 'Bar'
.'Baz'
.'Qux';
  • A string must not concatenate with functions or methods
// bad$foo = ucfisrt('bar') . ' baz';
// good$foo = ucfirst('bar');
$foo = $foo . ' baz';
// very good$foo = ucfirst('bar');
$foo .= ' baz';

Class constants

  • Constants must be in UPPER_SNAKE_CASE.
// badconstMAXSIZE=5;
// badconstmax_size∙=∙4;
// goodconstMAX_SIZE∙=∙5;
  • You must set assignment operator with spaces.
// badconstMAX_SIZE=5;
// goodconstMAX_SIZE∙=∙5;
  • You must conserve a great alignment.
// badconstFOO∙∙='Ba';
constFOO∙∙='r';
constQUUX∙='Qu';
constQUUX∙='x';
// goodconstFOO∙∙='Ba';
constFOO∙∙='r';
constQUUX∙='Qu';
constQUUX∙='x';
  • You must use (int) $foo instead of intval($foo).

  • You must use (bool) $foo instead of boolval($foo).

  • You must use (float) $foo instead of floatval($foo).

  • You must use (string) $foo instead of strval($foo).

// bad$foo∙=(string)$bar;
// good$foo∙=(string)∙$bar;
  • You must use [] notation instead of array().

  • Arrays with few data must be declared like this:

$foo∙=∙['Bar',∙'Baz',∙'Qux'];
  • Arrays with lots of data must be declared like this:
$foo = [
∙∙∙∙'bar'∙∙=>∙'abc',
∙∙∙∙'baz'∙∙=>∙123,
∙∙∙∙'qux'∙∙=>∙true,
∙∙∙∙'quux'∙=>∙[
∙∙∙∙∙∙∙∙'corge'∙∙=>∙[],
∙∙∙∙∙∙∙∙'grault'∙=>∙123.456,
∙∙∙∙],
];
  • For the arrays with lots of data, lines must be terminated by a comma. (Easy to copy/paste)

Classes

  • The extends and implements 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.

<?phpnamespaceVendor\Foo;
class Foo extends Bar implements Baz, Qux, Quux
{
// Do something...
}
  • 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.
<?phpnamespaceVendor\Foo;
class Foo extends Bar implements
∙∙∙∙Baz,
∙∙∙∙Qux,
∙∙∙∙Quux
{
// Do something...
}

Properties

  • Visibility must be declared on all properties.
// bad/** @var string Property description */$foo = '';
// good/** @var string Property description */public$foo = '';
  • There must not be more than one property declared per statement.
// badpublic$foo = '',
$bar = '';
// good/** @var string Property description */public$foo = '';
/** @var string Property description */protected$bar = '';
  • Property names must not be prefixed with a single underscore to indicate protected or private visibility.
// bad/** @var string Property description */protected$_bar = '';
/** @var string Property description */private$_baz = '';
// good/** @var string Property description */protected$bar = '';
/** @var string Property description */private$baz = '';
  • When present, the static declaration must come after the visibility declaration.
// bad/** @var string $foo Property description */staticpublic$foo = '';
// good/** @var string $foo Property description */publicstatic$foo = '';

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.

// badprotectedfunction_foo()
{
// Do something...
}
// goodprotectedfunctionfoo()
{
// Do something...
}
  • Method names must not be declared with a space after the method name.
// badpublicfunctionfoo∙()
{
// Do something...
}
// goodpublicfunctionfoo()
{
// Do something...
}
  • There must not be a space after the opening parenthesis, and there must not be a space before the closing parenthesis.
// badpublicfunction foo()∙{∙
// Do something...
∙}
// goodpublicfunctionfoo()
{
// Do something...
}
  • The opening brace must go on its own line, and the closing brace must go on the next line following the body.
// badpublicfunction foo()∙{
// Do something...}// goodpublicfunctionfoo()
{
// Do something...
}
  • In the argument list, there must not be a space before each comma, and there must be one space after each comma.
// badpublicfunctionfoo($bar∙,&$baz∙,$qux = [])
{
// Do something...
}
// badpublicfunctionfoo($bar, &$baz, $qux = []∙)
{
// Do something...
}
// goodpublicfunction foo($bar,&$baz,$qux∙=∙[])
{
// Do something...
}
  • 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.

// goodpublicfunctionfoo(
∙∙∙∙$bar,
∙∙∙∙&$baz,
∙∙∙∙$qux = []
) {
// Do something...
}
  • When present, the abstract and final declarations must precede the visibility declaration.

  • When present, the static declaration must come after the visibility declaration.

// badprotectedabstractfunctionfoo();
staticpublicfinalfunctionbar()
{
// Do something...
}
// goodabstractprotectedfunctionfoo();
finalpublicstaticfunctionbar()
{
// Do something...
}

Interfaces

  • The interface name must be suffixed with Interface.
<?phpnamespaceVendor\Foo;
/** * Interface Foo * */interface FooInterface
{
/** * Set Foo * * @param string $foo */publicfunctionsetFoo($foo);
}

Traits

  • The trait name must be suffixed with Trait.
<?phpnamespaceVendor\Foo;
/** * Trait Foo * */trait FooTrait
{
/** @var \Vendor\Bar */protected$bar;
/** * Set Bar * * @param string $bar */publicfunctionsetBar($bar)
{
$this->bar = $bar;
}
}
  • There must not be a space between the method or function name and the opening parenthesis.
// badfoo∙();
$bar->baz∙();
// goodfoo();
$bar->baz();
  • 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.
// badfoo(∙$qux∙);
$bar->baz(∙$qux∙);
// goodfoo($qux);
$bar->baz($qux);
  • There must not be a space before each comma and there must be one space after each comma.
// badfoo($bar∙,∙$baz∙,∙$qux);
// goodfoo($bar,∙$baz,∙$qux);
  • 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.
// badfoo($longFoo,
$longBar,
$longBaz
);
// badfoo($longFoo,
$longBar,
$longBaz);
// goodfoo(
∙∙∙∙$longFoo,
∙∙∙∙$longBar,
∙∙∙∙$longBaz
);
  • Chained method calls must be wrapped before the first call and indented once.
// bad$fooBar->baz()->qux($param);
// good$fooBar
->baz()
->qux($param);
  • When you pass an array as the only argument, the array brackets should be on the same lines as the method parenthesis.
// badfoo(
[
'foo' => 'bar',
]
);
// goodfoo([
'foo' => 'bar',
]);

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 of else if so that all control keywords look like single words.

Example

// badif(EXPRESSION){
// Do something...
}
// badif (EXPRESSION) {
// Do something...
}
// badif (EXPRESSION)
{
// Do something...
}
// badif (EXPRESSION) {
// Do something...
}
else {
// Do something...
}
// goodif∙(EXPRESSION)∙{
∙∙∙∙// Do something...
}∙elseif∙(OTHER_EXPRESSION)∙{
∙∙∙∙// Do something...
}∙else∙{
∙∙∙∙// Do something...
}

Ternary (?:)

  • You should not use nesting ternary.

Example

// bad$foo = EXPRESSION ? 'bar' : OTHER_EXPRESSION ? 'baz' : 'qux';
// good$foo∙=∙EXPRESSION∙?∙'bar'∙:∙'baz';
// good$foo∙=∙EXPRESSION
∙∙∙∙?∙'bar'
∙∙∙∙:∙'baz';

switch and case

  • The case statement must be indented once from switch, and the break keyword (or other terminating keyword) must be indented at the same level as the case body.

  • There must be a comment such as // no break when fall-through is intentional in a non-empty case body.

Example

// badswitch(EXPRESSION)
{
case0:
// Do something...break;
}
// goodswitch∙(EXPRESSION)∙{
∙∙∙∙case∙0:
∙∙∙∙∙∙∙∙// Do something...
∙∙∙∙∙∙∙∙break;
∙∙∙∙case∙1:
∙∙∙∙∙∙∙∙// Do something with no break...
∙∙∙∙∙∙∙∙// no break
∙∙∙∙case∙2:
∙∙∙∙case∙3:
∙∙∙∙case∙4:
∙∙∙∙∙∙∙∙// Do something with return instead of break...
∙∙∙∙∙∙∙∙return;
∙∙∙∙default:
∙∙∙∙∙∙∙∙// Do something in default case...
∙∙∙∙∙∙∙∙break;
}

while and do while

Example

// badwhile(EXPRESSION)
{
// Do something...
}
// baddo
{
// Do something...
} while(EXPRESSION);
// goodwhile∙(EXPRESSION)∙{
∙∙∙∙// Do something...
}
// good
do∙{
∙∙∙∙// Do something...
}∙while∙(EXPRESSION);

for

Example

// badfor( $i=0;$i<10;$i++ )
{
// Do something...
}
// goodfor∙($i∙=∙0;∙$i∙<∙10;∙$i++)∙{
∙∙∙∙// Do something...
}

foreach

Example

// badforeach( $fooas$key=>$value )
{
// Do something...
}
// goodforeach∙($foo∙as∙$key∙=>∙$value)∙{
∙∙∙∙// Do something...
}

try and catch

Example

// badtry
{
// Do something...
}
catch(FooException$e)
{
// Do something...
}
// good
try∙{
∙∙∙∙// Do something...
}∙catch∙(FooException∙$exception)∙{
∙∙∙∙// Do something...
}∙catch∙(BarException∙$exception)∙{
∙∙∙∙// Do something...
}∙finally∙{
∙∙∙∙// Do something...
}
  • 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)

// good$closureWithArguments∙=∙function∙($foo,∙$bar)∙{
∙∙∙∙// Do something...
};
// good$closureWithArgumentsAndVariables∙=∙function∙($foo,∙$bar)∙use∙($baz,∙$qux)∙{
∙∙∙∙// Do something...
};
// good$longArgumentsNoVariables∙=∙function∙(
∙∙∙∙$longArgumentFoo,
∙∙∙∙$longArgumentBar,
∙∙∙∙$longArgumentBaz
)∙{
∙∙∙∙// Do something...
};
// good$noArgumentsLongVariables∙=∙function∙()∙use∙(
∙∙∙∙$longVariableFoo,
∙∙∙∙$longVariablBar,
∙∙∙∙$longVariableBaz
)∙{
∙∙∙∙// Do something...
};
// good$longArgumentsLongVariables∙=∙function∙(
∙∙∙∙$longArgumentFoo,
∙∙∙∙$longArgumentBar,
∙∙∙∙$longArgumentBaz
)∙use∙(
∙∙∙∙$longVariableFoo,
∙∙∙∙$longVariableBar,
∙∙∙∙$longVariableBaz
)∙{
∙∙∙∙// Do something...
};
// good$longArgumentsShortVariables∙=∙function∙(
∙∙∙∙$longArgumentFoo,
∙∙∙∙$longArgumentBar,
∙∙∙∙$longArgumentBaz
)∙use∙($variableFoo)∙{
∙∙∙∙// Do something...
};
// good$shortArgumentsLongVariables∙=∙function∙($argumentFoo)∙use∙(
∙∙∙∙$longVariableFoo,
∙∙∙∙$longVariableBar,
∙∙∙∙$longVariableBaz
)∙{
∙∙∙∙// Do something...
};

Example (usage)

$foo->bar(
∙∙∙∙$argumentFoo,
∙∙∙∙function∙($argumentBar)∙use∙($variableFoo)∙{
∙∙∙∙∙∙∙∙// Do something...
∙∙∙∙},
∙∙∙∙$argumentBaz
);

Date

  • You must use new \DateTime('2014-01-01 00:00:00') instead of date('2014-01-01 00:00:00').

  • You must use new \DateTime('Sunday') instead of strtotime('Sunday').

Readibility

  • When possible, avoid nestings of more than 2 levels and prefer "return early" structures.
// bad$response = [];
if ($foo) {
foreach ($foo->getBars() as$bar) {
if ($bar->hasBaz()) {
// 3 nested levels
}
}
}
return$response;
// good$response = [];
if (!$foo) {
return$response;
}
foreach ($foo->getBars() as$bar) {
if ($bar->hasBaz()) {
// only 2 nested levels
}
}
return$response;

About

PHP conventions at iAdvize

Resources

Stars

16 stars

Watchers

11 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
This repository was archived by the owner on May 13, 2025. It is now read-only.

Repository files navigation

iAdvize PHP Style Guide

  1. IDE Integration
  2. Files
  3. Lines
  4. Keywords
  5. Comments
  6. Naming
  7. Variables
  8. Constants
  9. Type casting
  10. Namespaces and use declarations
  11. String
  12. Arrays
  13. Classes, Properties, and Methods
  14. Interfaces, Traits
  15. Function and Method Calls
  16. Control Structures
  17. Closures
  18. Best practices

Configure your PHPStorm

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

  • 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 <?php ?> tags for non-views scripts.

<?phpecho'test';
  • Use the short-echo <?= ?> tags for view scripts.
<title><?=$title;∙?></title>
  • 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.

  • Blank lines may be added to improve readability and to indicate related blocks of code.
// nahfunctionfoo()
{
$foo = 'test';
$foo = strtoupper($foo);
return$foo;
}
// goodfunctionbar()
{
$foo = 'test';
$foo = strtoupper($foo);
return$foo;
}
  • There must not be more than one statement per line.
// bad$foo = substr(strtoupper('test'), 0, 1);
// good$foo = 'test';
$foo = strtoupper($foo);
$foo = substr($foo, 0, 1);
  • All PHP keywords must be in lower case (Eg: true, false, null, etc.)
  • Namespaces names must be delcared in UpperCamelCase.
// badnamespaceVendor\fooBar;
// badnamespaceVendor\foo_bar;
// goodnamespaceVendor\FooBar;
  • 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:

// baduseFoo\Bar,
Qux\Quux,
Foo\Baz;
// baduseFoo\Bar;
useQux\Quux;
useFoo\Baz;
// gooduseFoo\Bar;
useFoo\Baz;
useQux\Quux;
useQux\Corge\Grault;
  • use alias declaration should be composed with sub-namespaces names.
// baduseFoo\BarasBaz;
// baduseBaz\Qux\QuuxasBQQ;
// gooduseFoo\BarasFooBar;
// gooduseBaz\Qux\QuuxasBazQuxQuux;

In-line code comments

  • Comments should be on a separate line immediately before the code line or block they reference.
// bad$foo = 'bar'; // Bar in foo// good// Foo assignment for example$foo = 'bar';
// good// Foo assignment// for example$foo = 'bar';

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.
/** * Foo * */class Foo
{
/** * The description of bar * * @param string $baz The baz * * @return string The return of bar */publicfunctionbar($baz)
{
// Returned valuereturn'Do something...';
}
}
  • You must add PHPDoc blocks for all variable references.
/** @var Bar\Baz $foo Baz object */
$foo = $bar->baz();
/** * Foo * */class Foo
{
/** @var string $bar It's bar! */public$bar = '';
}
  • 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.
// BadnamespaceVendor\Bar\Baz;
/** * Foo * */class Foo
{
/** @var \Other\MyClass $myClass */protected$myClass;
/** * @return \Other\MyClass */publicfunctiongetMyClass()
{
return$this->myClass;
}
}
// GoodnamespaceVendor\Bar\Baz;
useOther\MyClass;
/** * Foo * */class Foo
{
/** @var MyClass $myClass */protected$myClass;
/** * @return MyClass */publicfunctiongetMyClass()
{
return$this->myClass;
}
}
  • @todo and @fixme must be used in PHPDoc blocks like annotations.
/** @todo Think to check value */$foo = 'bar';
/** @fixme Change qux to quux */$baz = 'qux';

Qualify objects you use

  • you should add @var tag when you get object from abstract method
// bad$logger = $this->getServiceLocator()->get('logger');
// bad$this->getServiceLocator()->get('AwesomeFactory')->createAwesomeness();
// good/** @var LoggerInterface $logger */$logger = $this->getServiceLocator()->get('logger');
// good/** @var AwesomeFactory $awesomeFactory */$awesomeFactory = $this->getServiceLocator()->get('AwesomeFactory');
$awesomeFactory->createAwesomeness()
  • you shouldn't add @var tag when you get object from explicit method
// bad/** * Class AwesomeFactory */class AwesomeFactory
{
/** * @return Awesome */publicfunctioncreateAwesomeness()
{
returnAwesome();
}
}
$awesomeFactory = newAwesomeFactory();
/** @var Awesome $awesome */$awesome = $awesomeFactory->createAwesomeness();
// good/** * Class AwesomeFactory */class AwesomeFactory
{
/** * @return Awesome */publicfunctioncreateAwesomeness()
{
returnAwesome();
}
}
$awesomeFactory = newAwesomeFactory();
$awesome = $awesomeFactory->createAwesomeness();
  • Clarity over brevity in variable, method and class names
// bad$o = newObject();
// badclass A
{
}
// badpublicfunctiondoIt()
{
}
// good$object = newObject();
// goodclass Substracter
{
}
// goodpublicfunctionassociateChannelToOperator()
{
}
  • Boolean variable names should be either an adjective or a past participle form. Associated getter method should begin with is or has.
// bad$enablePlugin = true;
// badpublicfunctiongetEnablePlugin() {}
// badpublicfunctiongetPluginEnabled() {}
// good$pluginEnabled = true;
// good$visible = true;
// goodpublicfunctionisPluginEnabled() {}
// goodpublicfunctionisVisible() {}
  • DateTime variable names should be a past participle form ending with At.
// bad$dateUpdate = new \DateTime;
// bad$endDate = new \DateTime;
// good$updatedAt = new \DateTime;
// good$lastLoggedAt = new \DateTime;

User variables

  • Variables should be in lowerCamelCase.
// bad$_foo='';
// bad$foo_bar = '';
// bad$fooBar='';
// good$fooBar∙=∙'';
  • You must set off operators with spaces.
// bad$foo = (5+6)/5;
// good$foo∙=(5∙+∙6)∙/∙5;
  • You must conserve a great alignment.
// bad$foo = 'Ba';
$foo .= 'r';
$quux = 'Qu';
$quux .= 'x';
// good$foo = 'Ba';
$foo .= 'r';
$quux = 'Qu';
$quux .= 'x';
// bad$fooBarBazQux->bar()->
baz()->qux();
// good$fooBarBazQux∙∙∙∙->bar()
∙∙∙∙->baz()
∙∙∙∙->qux();

Global variables

  • You must used $_POST, $_GET and $_COOKIE instead of $_REQUEST. If you use a framework, use Request component.
  • 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

$foo = 'Bar'
.'Baz'
.'Qux';
  • A string must not concatenate with functions or methods
// bad$foo = ucfisrt('bar') . ' baz';
// good$foo = ucfirst('bar');
$foo = $foo . ' baz';
// very good$foo = ucfirst('bar');
$foo .= ' baz';

Class constants

  • Constants must be in UPPER_SNAKE_CASE.
// badconstMAXSIZE=5;
// badconstmax_size∙=∙4;
// goodconstMAX_SIZE∙=∙5;
  • You must set assignment operator with spaces.
// badconstMAX_SIZE=5;
// goodconstMAX_SIZE∙=∙5;
  • You must conserve a great alignment.
// badconstFOO∙∙='Ba';
constFOO∙∙='r';
constQUUX∙='Qu';
constQUUX∙='x';
// goodconstFOO∙∙='Ba';
constFOO∙∙='r';
constQUUX∙='Qu';
constQUUX∙='x';
  • You must use (int) $foo instead of intval($foo).

  • You must use (bool) $foo instead of boolval($foo).

  • You must use (float) $foo instead of floatval($foo).

  • You must use (string) $foo instead of strval($foo).

// bad$foo∙=(string)$bar;
// good$foo∙=(string)∙$bar;
  • You must use [] notation instead of array().

  • Arrays with few data must be declared like this:

$foo∙=∙['Bar',∙'Baz',∙'Qux'];
  • Arrays with lots of data must be declared like this:
$foo = [
∙∙∙∙'bar'∙∙=>∙'abc',
∙∙∙∙'baz'∙∙=>∙123,
∙∙∙∙'qux'∙∙=>∙true,
∙∙∙∙'quux'∙=>∙[
∙∙∙∙∙∙∙∙'corge'∙∙=>∙[],
∙∙∙∙∙∙∙∙'grault'∙=>∙123.456,
∙∙∙∙],
];
  • For the arrays with lots of data, lines must be terminated by a comma. (Easy to copy/paste)

Classes

  • The extends and implements 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.

<?phpnamespaceVendor\Foo;
class Foo extends Bar implements Baz, Qux, Quux
{
// Do something...
}
  • 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.
<?phpnamespaceVendor\Foo;
class Foo extends Bar implements
∙∙∙∙Baz,
∙∙∙∙Qux,
∙∙∙∙Quux
{
// Do something...
}

Properties

  • Visibility must be declared on all properties.
// bad/** @var string Property description */$foo = '';
// good/** @var string Property description */public$foo = '';
  • There must not be more than one property declared per statement.
// badpublic$foo = '',
$bar = '';
// good/** @var string Property description */public$foo = '';
/** @var string Property description */protected$bar = '';
  • Property names must not be prefixed with a single underscore to indicate protected or private visibility.
// bad/** @var string Property description */protected$_bar = '';
/** @var string Property description */private$_baz = '';
// good/** @var string Property description */protected$bar = '';
/** @var string Property description */private$baz = '';
  • When present, the static declaration must come after the visibility declaration.
// bad/** @var string $foo Property description */staticpublic$foo = '';
// good/** @var string $foo Property description */publicstatic$foo = '';

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.

// badprotectedfunction_foo()
{
// Do something...
}
// goodprotectedfunctionfoo()
{
// Do something...
}
  • Method names must not be declared with a space after the method name.
// badpublicfunctionfoo∙()
{
// Do something...
}
// goodpublicfunctionfoo()
{
// Do something...
}
  • There must not be a space after the opening parenthesis, and there must not be a space before the closing parenthesis.
// badpublicfunction foo()∙{∙
// Do something...
∙}
// goodpublicfunctionfoo()
{
// Do something...
}
  • The opening brace must go on its own line, and the closing brace must go on the next line following the body.
// badpublicfunction foo()∙{
// Do something...}// goodpublicfunctionfoo()
{
// Do something...
}
  • In the argument list, there must not be a space before each comma, and there must be one space after each comma.
// badpublicfunctionfoo($bar∙,&$baz∙,$qux = [])
{
// Do something...
}
// badpublicfunctionfoo($bar, &$baz, $qux = []∙)
{
// Do something...
}
// goodpublicfunction foo($bar,&$baz,$qux∙=∙[])
{
// Do something...
}
  • 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.

// goodpublicfunctionfoo(
∙∙∙∙$bar,
∙∙∙∙&$baz,
∙∙∙∙$qux = []
) {
// Do something...
}
  • When present, the abstract and final declarations must precede the visibility declaration.

  • When present, the static declaration must come after the visibility declaration.

// badprotectedabstractfunctionfoo();
staticpublicfinalfunctionbar()
{
// Do something...
}
// goodabstractprotectedfunctionfoo();
finalpublicstaticfunctionbar()
{
// Do something...
}

Interfaces

  • The interface name must be suffixed with Interface.
<?phpnamespaceVendor\Foo;
/** * Interface Foo * */interface FooInterface
{
/** * Set Foo * * @param string $foo */publicfunctionsetFoo($foo);
}

Traits

  • The trait name must be suffixed with Trait.
<?phpnamespaceVendor\Foo;
/** * Trait Foo * */trait FooTrait
{
/** @var \Vendor\Bar */protected$bar;
/** * Set Bar * * @param string $bar */publicfunctionsetBar($bar)
{
$this->bar = $bar;
}
}
  • There must not be a space between the method or function name and the opening parenthesis.
// badfoo∙();
$bar->baz∙();
// goodfoo();
$bar->baz();
  • 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.
// badfoo(∙$qux∙);
$bar->baz(∙$qux∙);
// goodfoo($qux);
$bar->baz($qux);
  • There must not be a space before each comma and there must be one space after each comma.
// badfoo($bar∙,∙$baz∙,∙$qux);
// goodfoo($bar,∙$baz,∙$qux);
  • 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.
// badfoo($longFoo,
$longBar,
$longBaz
);
// badfoo($longFoo,
$longBar,
$longBaz);
// goodfoo(
∙∙∙∙$longFoo,
∙∙∙∙$longBar,
∙∙∙∙$longBaz
);
  • Chained method calls must be wrapped before the first call and indented once.
// bad$fooBar->baz()->qux($param);
// good$fooBar
->baz()
->qux($param);
  • When you pass an array as the only argument, the array brackets should be on the same lines as the method parenthesis.
// badfoo(
[
'foo' => 'bar',
]
);
// goodfoo([
'foo' => 'bar',
]);

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 of else if so that all control keywords look like single words.

Example

// badif(EXPRESSION){
// Do something...
}
// badif (EXPRESSION) {
// Do something...
}
// badif (EXPRESSION)
{
// Do something...
}
// badif (EXPRESSION) {
// Do something...
}
else {
// Do something...
}
// goodif∙(EXPRESSION)∙{
∙∙∙∙// Do something...
}∙elseif∙(OTHER_EXPRESSION)∙{
∙∙∙∙// Do something...
}∙else∙{
∙∙∙∙// Do something...
}

Ternary (?:)

  • You should not use nesting ternary.

Example

// bad$foo = EXPRESSION ? 'bar' : OTHER_EXPRESSION ? 'baz' : 'qux';
// good$foo∙=∙EXPRESSION∙?∙'bar'∙:∙'baz';
// good$foo∙=∙EXPRESSION
∙∙∙∙?∙'bar'
∙∙∙∙:∙'baz';

switch and case

  • The case statement must be indented once from switch, and the break keyword (or other terminating keyword) must be indented at the same level as the case body.

  • There must be a comment such as // no break when fall-through is intentional in a non-empty case body.

Example

// badswitch(EXPRESSION)
{
case0:
// Do something...break;
}
// goodswitch∙(EXPRESSION)∙{
∙∙∙∙case∙0:
∙∙∙∙∙∙∙∙// Do something...
∙∙∙∙∙∙∙∙break;
∙∙∙∙case∙1:
∙∙∙∙∙∙∙∙// Do something with no break...
∙∙∙∙∙∙∙∙// no break
∙∙∙∙case∙2:
∙∙∙∙case∙3:
∙∙∙∙case∙4:
∙∙∙∙∙∙∙∙// Do something with return instead of break...
∙∙∙∙∙∙∙∙return;
∙∙∙∙default:
∙∙∙∙∙∙∙∙// Do something in default case...
∙∙∙∙∙∙∙∙break;
}

while and do while

Example

// badwhile(EXPRESSION)
{
// Do something...
}
// baddo
{
// Do something...
} while(EXPRESSION);
// goodwhile∙(EXPRESSION)∙{
∙∙∙∙// Do something...
}
// good
do∙{
∙∙∙∙// Do something...
}∙while∙(EXPRESSION);

for

Example

// badfor( $i=0;$i<10;$i++ )
{
// Do something...
}
// goodfor∙($i∙=∙0;∙$i∙<∙10;∙$i++)∙{
∙∙∙∙// Do something...
}

foreach

Example

// badforeach( $fooas$key=>$value )
{
// Do something...
}
// goodforeach∙($foo∙as∙$key∙=>∙$value)∙{
∙∙∙∙// Do something...
}

try and catch

Example

// badtry
{
// Do something...
}
catch(FooException$e)
{
// Do something...
}
// good
try∙{
∙∙∙∙// Do something...
}∙catch∙(FooException∙$exception)∙{
∙∙∙∙// Do something...
}∙catch∙(BarException∙$exception)∙{
∙∙∙∙// Do something...
}∙finally∙{
∙∙∙∙// Do something...
}
  • 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)

// good$closureWithArguments∙=∙function∙($foo,∙$bar)∙{
∙∙∙∙// Do something...
};
// good$closureWithArgumentsAndVariables∙=∙function∙($foo,∙$bar)∙use∙($baz,∙$qux)∙{
∙∙∙∙// Do something...
};
// good$longArgumentsNoVariables∙=∙function∙(
∙∙∙∙$longArgumentFoo,
∙∙∙∙$longArgumentBar,
∙∙∙∙$longArgumentBaz
)∙{
∙∙∙∙// Do something...
};
// good$noArgumentsLongVariables∙=∙function∙()∙use∙(
∙∙∙∙$longVariableFoo,
∙∙∙∙$longVariablBar,
∙∙∙∙$longVariableBaz
)∙{
∙∙∙∙// Do something...
};
// good$longArgumentsLongVariables∙=∙function∙(
∙∙∙∙$longArgumentFoo,
∙∙∙∙$longArgumentBar,
∙∙∙∙$longArgumentBaz
)∙use∙(
∙∙∙∙$longVariableFoo,
∙∙∙∙$longVariableBar,
∙∙∙∙$longVariableBaz
)∙{
∙∙∙∙// Do something...
};
// good$longArgumentsShortVariables∙=∙function∙(
∙∙∙∙$longArgumentFoo,
∙∙∙∙$longArgumentBar,
∙∙∙∙$longArgumentBaz
)∙use∙($variableFoo)∙{
∙∙∙∙// Do something...
};
// good$shortArgumentsLongVariables∙=∙function∙($argumentFoo)∙use∙(
∙∙∙∙$longVariableFoo,
∙∙∙∙$longVariableBar,
∙∙∙∙$longVariableBaz
)∙{
∙∙∙∙// Do something...
};

Example (usage)

$foo->bar(
∙∙∙∙$argumentFoo,
∙∙∙∙function∙($argumentBar)∙use∙($variableFoo)∙{
∙∙∙∙∙∙∙∙// Do something...
∙∙∙∙},
∙∙∙∙$argumentBaz
);

Date

  • You must use new \DateTime('2014-01-01 00:00:00') instead of date('2014-01-01 00:00:00').

  • You must use new \DateTime('Sunday') instead of strtotime('Sunday').

Readibility

  • When possible, avoid nestings of more than 2 levels and prefer "return early" structures.
// bad$response = [];
if ($foo) {
foreach ($foo->getBars() as$bar) {
if ($bar->hasBaz()) {
// 3 nested levels
}
}
}
return$response;
// good$response = [];
if (!$foo) {
return$response;
}
foreach ($foo->getBars() as$bar) {
if ($bar->hasBaz()) {
// only 2 nested levels
}
}
return$response;

About

PHP conventions at iAdvize

Resources

Stars

16 stars

Watchers

11 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content
This repository was archived by the owner on May 13, 2025. It is now read-only.

Repository files navigation

iAdvize PHP Style Guide

  1. IDE Integration
  2. Files
  3. Lines
  4. Keywords
  5. Comments
  6. Naming
  7. Variables
  8. Constants
  9. Type casting
  10. Namespaces and use declarations
  11. String
  12. Arrays
  13. Classes, Properties, and Methods
  14. Interfaces, Traits
  15. Function and Method Calls
  16. Control Structures
  17. Closures
  18. Best practices

Configure your PHPStorm

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

  • 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 <?php ?> tags for non-views scripts.

<?phpecho'test';
  • Use the short-echo <?= ?> tags for view scripts.
<title><?=$title;∙?></title>
  • 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.

  • Blank lines may be added to improve readability and to indicate related blocks of code.
// nahfunctionfoo()
{
$foo = 'test';
$foo = strtoupper($foo);
return$foo;
}
// goodfunctionbar()
{
$foo = 'test';
$foo = strtoupper($foo);
return$foo;
}
  • There must not be more than one statement per line.
// bad$foo = substr(strtoupper('test'), 0, 1);
// good$foo = 'test';
$foo = strtoupper($foo);
$foo = substr($foo, 0, 1);
  • All PHP keywords must be in lower case (Eg: true, false, null, etc.)
  • Namespaces names must be delcared in UpperCamelCase.
// badnamespaceVendor\fooBar;
// badnamespaceVendor\foo_bar;
// goodnamespaceVendor\FooBar;
  • 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:

// baduseFoo\Bar,
Qux\Quux,
Foo\Baz;
// baduseFoo\Bar;
useQux\Quux;
useFoo\Baz;
// gooduseFoo\Bar;
useFoo\Baz;
useQux\Quux;
useQux\Corge\Grault;
  • use alias declaration should be composed with sub-namespaces names.
// baduseFoo\BarasBaz;
// baduseBaz\Qux\QuuxasBQQ;
// gooduseFoo\BarasFooBar;
// gooduseBaz\Qux\QuuxasBazQuxQuux;

In-line code comments

  • Comments should be on a separate line immediately before the code line or block they reference.
// bad$foo = 'bar'; // Bar in foo// good// Foo assignment for example$foo = 'bar';
// good// Foo assignment// for example$foo = 'bar';

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.
/** * Foo * */class Foo
{
/** * The description of bar * * @param string $baz The baz * * @return string The return of bar */publicfunctionbar($baz)
{
// Returned valuereturn'Do something...';
}
}
  • You must add PHPDoc blocks for all variable references.
/** @var Bar\Baz $foo Baz object */
$foo = $bar->baz();
/** * Foo * */class Foo
{
/** @var string $bar It's bar! */public$bar = '';
}
  • 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.
// BadnamespaceVendor\Bar\Baz;
/** * Foo * */class Foo
{
/** @var \Other\MyClass $myClass */protected$myClass;
/** * @return \Other\MyClass */publicfunctiongetMyClass()
{
return$this->myClass;
}
}
// GoodnamespaceVendor\Bar\Baz;
useOther\MyClass;
/** * Foo * */class Foo
{
/** @var MyClass $myClass */protected$myClass;
/** * @return MyClass */publicfunctiongetMyClass()
{
return$this->myClass;
}
}
  • @todo and @fixme must be used in PHPDoc blocks like annotations.
/** @todo Think to check value */$foo = 'bar';
/** @fixme Change qux to quux */$baz = 'qux';

Qualify objects you use

  • you should add @var tag when you get object from abstract method
// bad$logger = $this->getServiceLocator()->get('logger');
// bad$this->getServiceLocator()->get('AwesomeFactory')->createAwesomeness();
// good/** @var LoggerInterface $logger */$logger = $this->getServiceLocator()->get('logger');
// good/** @var AwesomeFactory $awesomeFactory */$awesomeFactory = $this->getServiceLocator()->get('AwesomeFactory');
$awesomeFactory->createAwesomeness()
  • you shouldn't add @var tag when you get object from explicit method
// bad/** * Class AwesomeFactory */class AwesomeFactory
{
/** * @return Awesome */publicfunctioncreateAwesomeness()
{
returnAwesome();
}
}
$awesomeFactory = newAwesomeFactory();
/** @var Awesome $awesome */$awesome = $awesomeFactory->createAwesomeness();
// good/** * Class AwesomeFactory */class AwesomeFactory
{
/** * @return Awesome */publicfunctioncreateAwesomeness()
{
returnAwesome();
}
}
$awesomeFactory = newAwesomeFactory();
$awesome = $awesomeFactory->createAwesomeness();
  • Clarity over brevity in variable, method and class names
// bad$o = newObject();
// badclass A
{
}
// badpublicfunctiondoIt()
{
}
// good$object = newObject();
// goodclass Substracter
{
}
// goodpublicfunctionassociateChannelToOperator()
{
}
  • Boolean variable names should be either an adjective or a past participle form. Associated getter method should begin with is or has.
// bad$enablePlugin = true;
// badpublicfunctiongetEnablePlugin() {}
// badpublicfunctiongetPluginEnabled() {}
// good$pluginEnabled = true;
// good$visible = true;
// goodpublicfunctionisPluginEnabled() {}
// goodpublicfunctionisVisible() {}
  • DateTime variable names should be a past participle form ending with At.
// bad$dateUpdate = new \DateTime;
// bad$endDate = new \DateTime;
// good$updatedAt = new \DateTime;
// good$lastLoggedAt = new \DateTime;

User variables

  • Variables should be in lowerCamelCase.
// bad$_foo='';
// bad$foo_bar = '';
// bad$fooBar='';
// good$fooBar∙=∙'';
  • You must set off operators with spaces.
// bad$foo = (5+6)/5;
// good$foo∙=(5∙+∙6)∙/∙5;
  • You must conserve a great alignment.
// bad$foo = 'Ba';
$foo .= 'r';
$quux = 'Qu';
$quux .= 'x';
// good$foo = 'Ba';
$foo .= 'r';
$quux = 'Qu';
$quux .= 'x';
// bad$fooBarBazQux->bar()->
baz()->qux();
// good$fooBarBazQux∙∙∙∙->bar()
∙∙∙∙->baz()
∙∙∙∙->qux();

Global variables

  • You must used $_POST, $_GET and $_COOKIE instead of $_REQUEST. If you use a framework, use Request component.
  • 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

$foo = 'Bar'
.'Baz'
.'Qux';
  • A string must not concatenate with functions or methods
// bad$foo = ucfisrt('bar') . ' baz';
// good$foo = ucfirst('bar');
$foo = $foo . ' baz';
// very good$foo = ucfirst('bar');
$foo .= ' baz';

Class constants

  • Constants must be in UPPER_SNAKE_CASE.
// badconstMAXSIZE=5;
// badconstmax_size∙=∙4;
// goodconstMAX_SIZE∙=∙5;
  • You must set assignment operator with spaces.
// badconstMAX_SIZE=5;
// goodconstMAX_SIZE∙=∙5;
  • You must conserve a great alignment.
// badconstFOO∙∙='Ba';
constFOO∙∙='r';
constQUUX∙='Qu';
constQUUX∙='x';
// goodconstFOO∙∙='Ba';
constFOO∙∙='r';
constQUUX∙='Qu';
constQUUX∙='x';
  • You must use (int) $foo instead of intval($foo).

  • You must use (bool) $foo instead of boolval($foo).

  • You must use (float) $foo instead of floatval($foo).

  • You must use (string) $foo instead of strval($foo).

// bad$foo∙=(string)$bar;
// good$foo∙=(string)∙$bar;
  • You must use [] notation instead of array().

  • Arrays with few data must be declared like this:

$foo∙=∙['Bar',∙'Baz',∙'Qux'];
  • Arrays with lots of data must be declared like this:
$foo = [
∙∙∙∙'bar'∙∙=>∙'abc',
∙∙∙∙'baz'∙∙=>∙123,
∙∙∙∙'qux'∙∙=>∙true,
∙∙∙∙'quux'∙=>∙[
∙∙∙∙∙∙∙∙'corge'∙∙=>∙[],
∙∙∙∙∙∙∙∙'grault'∙=>∙123.456,
∙∙∙∙],
];
  • For the arrays with lots of data, lines must be terminated by a comma. (Easy to copy/paste)

Classes

  • The extends and implements 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.

<?phpnamespaceVendor\Foo;
class Foo extends Bar implements Baz, Qux, Quux
{
// Do something...
}
  • 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.
<?phpnamespaceVendor\Foo;
class Foo extends Bar implements
∙∙∙∙Baz,
∙∙∙∙Qux,
∙∙∙∙Quux
{
// Do something...
}

Properties

  • Visibility must be declared on all properties.
// bad/** @var string Property description */$foo = '';
// good/** @var string Property description */public$foo = '';
  • There must not be more than one property declared per statement.
// badpublic$foo = '',
$bar = '';
// good/** @var string Property description */public$foo = '';
/** @var string Property description */protected$bar = '';
  • Property names must not be prefixed with a single underscore to indicate protected or private visibility.
// bad/** @var string Property description */protected$_bar = '';
/** @var string Property description */private$_baz = '';
// good/** @var string Property description */protected$bar = '';
/** @var string Property description */private$baz = '';
  • When present, the static declaration must come after the visibility declaration.
// bad/** @var string $foo Property description */staticpublic$foo = '';
// good/** @var string $foo Property description */publicstatic$foo = '';

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.

// badprotectedfunction_foo()
{
// Do something...
}
// goodprotectedfunctionfoo()
{
// Do something...
}
  • Method names must not be declared with a space after the method name.
// badpublicfunctionfoo∙()
{
// Do something...
}
// goodpublicfunctionfoo()
{
// Do something...
}
  • There must not be a space after the opening parenthesis, and there must not be a space before the closing parenthesis.
// badpublicfunction foo()∙{∙
// Do something...
∙}
// goodpublicfunctionfoo()
{
// Do something...
}
  • The opening brace must go on its own line, and the closing brace must go on the next line following the body.
// badpublicfunction foo()∙{
// Do something...}// goodpublicfunctionfoo()
{
// Do something...
}
  • In the argument list, there must not be a space before each comma, and there must be one space after each comma.
// badpublicfunctionfoo($bar∙,&$baz∙,$qux = [])
{
// Do something...
}
// badpublicfunctionfoo($bar, &$baz, $qux = []∙)
{
// Do something...
}
// goodpublicfunction foo($bar,&$baz,$qux∙=∙[])
{
// Do something...
}
  • 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.

// goodpublicfunctionfoo(
∙∙∙∙$bar,
∙∙∙∙&$baz,
∙∙∙∙$qux = []
) {
// Do something...
}
  • When present, the abstract and final declarations must precede the visibility declaration.

  • When present, the static declaration must come after the visibility declaration.

// badprotectedabstractfunctionfoo();
staticpublicfinalfunctionbar()
{
// Do something...
}
// goodabstractprotectedfunctionfoo();
finalpublicstaticfunctionbar()
{
// Do something...
}

Interfaces

  • The interface name must be suffixed with Interface.
<?phpnamespaceVendor\Foo;
/** * Interface Foo * */interface FooInterface
{
/** * Set Foo * * @param string $foo */publicfunctionsetFoo($foo);
}

Traits

  • The trait name must be suffixed with Trait.
<?phpnamespaceVendor\Foo;
/** * Trait Foo * */trait FooTrait
{
/** @var \Vendor\Bar */protected$bar;
/** * Set Bar * * @param string $bar */publicfunctionsetBar($bar)
{
$this->bar = $bar;
}
}
  • There must not be a space between the method or function name and the opening parenthesis.
// badfoo∙();
$bar->baz∙();
// goodfoo();
$bar->baz();
  • 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.
// badfoo(∙$qux∙);
$bar->baz(∙$qux∙);
// goodfoo($qux);
$bar->baz($qux);
  • There must not be a space before each comma and there must be one space after each comma.
// badfoo($bar∙,∙$baz∙,∙$qux);
// goodfoo($bar,∙$baz,∙$qux);
  • 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.
// badfoo($longFoo,
$longBar,
$longBaz
);
// badfoo($longFoo,
$longBar,
$longBaz);
// goodfoo(
∙∙∙∙$longFoo,
∙∙∙∙$longBar,
∙∙∙∙$longBaz
);
  • Chained method calls must be wrapped before the first call and indented once.
// bad$fooBar->baz()->qux($param);
// good$fooBar
->baz()
->qux($param);
  • When you pass an array as the only argument, the array brackets should be on the same lines as the method parenthesis.
// badfoo(
[
'foo' => 'bar',
]
);
// goodfoo([
'foo' => 'bar',
]);

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 of else if so that all control keywords look like single words.

Example

// badif(EXPRESSION){
// Do something...
}
// badif (EXPRESSION) {
// Do something...
}
// badif (EXPRESSION)
{
// Do something...
}
// badif (EXPRESSION) {
// Do something...
}
else {
// Do something...
}
// goodif∙(EXPRESSION)∙{
∙∙∙∙// Do something...
}∙elseif∙(OTHER_EXPRESSION)∙{
∙∙∙∙// Do something...
}∙else∙{
∙∙∙∙// Do something...
}

Ternary (?:)

  • You should not use nesting ternary.

Example

// bad$foo = EXPRESSION ? 'bar' : OTHER_EXPRESSION ? 'baz' : 'qux';
// good$foo∙=∙EXPRESSION∙?∙'bar'∙:∙'baz';
// good$foo∙=∙EXPRESSION
∙∙∙∙?∙'bar'
∙∙∙∙:∙'baz';

switch and case

  • The case statement must be indented once from switch, and the break keyword (or other terminating keyword) must be indented at the same level as the case body.

  • There must be a comment such as // no break when fall-through is intentional in a non-empty case body.

Example

// badswitch(EXPRESSION)
{
case0:
// Do something...break;
}
// goodswitch∙(EXPRESSION)∙{
∙∙∙∙case∙0:
∙∙∙∙∙∙∙∙// Do something...
∙∙∙∙∙∙∙∙break;
∙∙∙∙case∙1:
∙∙∙∙∙∙∙∙// Do something with no break...
∙∙∙∙∙∙∙∙// no break
∙∙∙∙case∙2:
∙∙∙∙case∙3:
∙∙∙∙case∙4:
∙∙∙∙∙∙∙∙// Do something with return instead of break...
∙∙∙∙∙∙∙∙return;
∙∙∙∙default:
∙∙∙∙∙∙∙∙// Do something in default case...
∙∙∙∙∙∙∙∙break;
}

while and do while

Example

// badwhile(EXPRESSION)
{
// Do something...
}
// baddo
{
// Do something...
} while(EXPRESSION);
// goodwhile∙(EXPRESSION)∙{
∙∙∙∙// Do something...
}
// good
do∙{
∙∙∙∙// Do something...
}∙while∙(EXPRESSION);

for

Example

// badfor( $i=0;$i<10;$i++ )
{
// Do something...
}
// goodfor∙($i∙=∙0;∙$i∙<∙10;∙$i++)∙{
∙∙∙∙// Do something...
}

foreach

Example

// badforeach( $fooas$key=>$value )
{
// Do something...
}
// goodforeach∙($foo∙as∙$key∙=>∙$value)∙{
∙∙∙∙// Do something...
}

try and catch

Example

// badtry
{
// Do something...
}
catch(FooException$e)
{
// Do something...
}
// good
try∙{
∙∙∙∙// Do something...
}∙catch∙(FooException∙$exception)∙{
∙∙∙∙// Do something...
}∙catch∙(BarException∙$exception)∙{
∙∙∙∙// Do something...
}∙finally∙{
∙∙∙∙// Do something...
}
  • 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)

// good$closureWithArguments∙=∙function∙($foo,∙$bar)∙{
∙∙∙∙// Do something...
};
// good$closureWithArgumentsAndVariables∙=∙function∙($foo,∙$bar)∙use∙($baz,∙$qux)∙{
∙∙∙∙// Do something...
};
// good$longArgumentsNoVariables∙=∙function∙(
∙∙∙∙$longArgumentFoo,
∙∙∙∙$longArgumentBar,
∙∙∙∙$longArgumentBaz
)∙{
∙∙∙∙// Do something...
};
// good$noArgumentsLongVariables∙=∙function∙()∙use∙(
∙∙∙∙$longVariableFoo,
∙∙∙∙$longVariablBar,
∙∙∙∙$longVariableBaz
)∙{
∙∙∙∙// Do something...
};
// good$longArgumentsLongVariables∙=∙function∙(
∙∙∙∙$longArgumentFoo,
∙∙∙∙$longArgumentBar,
∙∙∙∙$longArgumentBaz
)∙use∙(
∙∙∙∙$longVariableFoo,
∙∙∙∙$longVariableBar,
∙∙∙∙$longVariableBaz
)∙{
∙∙∙∙// Do something...
};
// good$longArgumentsShortVariables∙=∙function∙(
∙∙∙∙$longArgumentFoo,
∙∙∙∙$longArgumentBar,
∙∙∙∙$longArgumentBaz
)∙use∙($variableFoo)∙{
∙∙∙∙// Do something...
};
// good$shortArgumentsLongVariables∙=∙function∙($argumentFoo)∙use∙(
∙∙∙∙$longVariableFoo,
∙∙∙∙$longVariableBar,
∙∙∙∙$longVariableBaz
)∙{
∙∙∙∙// Do something...
};

Example (usage)

$foo->bar(
∙∙∙∙$argumentFoo,
∙∙∙∙function∙($argumentBar)∙use∙($variableFoo)∙{
∙∙∙∙∙∙∙∙// Do something...
∙∙∙∙},
∙∙∙∙$argumentBaz
);

Date

  • You must use new \DateTime('2014-01-01 00:00:00') instead of date('2014-01-01 00:00:00').

  • You must use new \DateTime('Sunday') instead of strtotime('Sunday').

Readibility

  • When possible, avoid nestings of more than 2 levels and prefer "return early" structures.
// bad$response = [];
if ($foo) {
foreach ($foo->getBars() as$bar) {
if ($bar->hasBaz()) {
// 3 nested levels
}
}
}
return$response;
// good$response = [];
if (!$foo) {
return$response;
}
foreach ($foo->getBars() as$bar) {
if ($bar->hasBaz()) {
// only 2 nested levels
}
}
return$response;

About

PHP conventions at iAdvize

Resources

Stars

16 stars

Watchers

11 watching

Forks

Releases

Packages

Contributors

Languages