{"id":22093208,"url":"https://github.com/iadvize/php-convention","last_synced_at":"2025-07-24T20:32:40.665Z","repository":{"id":12720555,"uuid":"15393241","full_name":"iadvize/php-convention","owner":"iadvize","description":"PHP conventions at iAdvize","archived":false,"fork":false,"pushed_at":"2024-05-23T14:43:25.000Z","size":101,"stargazers_count":15,"open_issues_count":0,"forks_count":5,"subscribers_count":55,"default_branch":"master","last_synced_at":"2024-11-07T21:08:46.279Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/iadvize.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":"CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2013-12-23T10:42:42.000Z","updated_at":"2024-05-23T14:42:20.000Z","dependencies_parsed_at":"2023-11-06T09:37:56.085Z","dependency_job_id":"9d58f1b4-9e79-4441-86a3-2906c1b4f840","html_url":"https://github.com/iadvize/php-convention","commit_stats":{"total_commits":95,"total_committers":17,"mean_commits":5.588235294117647,"dds":0.7894736842105263,"last_synced_commit":"7f467022e403bd874dde67fa10934e75da737967"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iadvize%2Fphp-convention","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iadvize%2Fphp-convention/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iadvize%2Fphp-convention/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iadvize%2Fphp-convention/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/iadvize","download_url":"https://codeload.github.com/iadvize/php-convention/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":227476011,"owners_count":17779417,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-01T03:13:16.865Z","updated_at":"2025-07-24T20:32:40.647Z","avatar_url":"https://github.com/iadvize.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# DEPRECATED, move to https://docs.iadvize.io/RFCs/php/?h=php\n\n# iAdvize PHP Style Guide\n\n## \u003ca name='TOC'\u003eTable of Contents\u003c/a\u003e\n\n  1. [IDE Integration](#installation)\n  1. [Files](#files)\n  1. [Lines](#lines)\n  1. [Keywords](#keywords)\n  1. [Comments](#comments)\n  1. [Naming](#naming)\n  1. [Variables](#variables)\n  1. [Constants](#constants)\n  1. [Type casting](#type-casting)\n  1. [Namespaces and use declarations](#namespaces-use)\n  1. [String](#strings)\n  1. [Arrays](#arrays)\n  1. [Classes, Properties, and Methods](#classes)\n  1. [Interfaces, Traits](#interfaces)\n  1. [Function and Method Calls](#function-method-calls)\n  1. [Control Structures](#control-structures)\n  1. [Closures](#closures)\n  1. [Best practices](#best-practices)\n\n## \u003ca name='installation'\u003eIDE integration\u003c/a\u003e\n\n[![Configure your PHPStorm](https://infinit.io/_/rF5xJXT.png)](https://www.youtube.com/watch?v=7XO5crQkR4Y)\n\n### Troubleshooting\n\nIf you have this `phpcs: PHP Fatal error:  Uncaught exception 'PHP_CodeSniffer_Exception' with message 'Referenced sniff \"Symfony2\" does not exist'`\n\nLaunch this: `./vendor/bin/phpcs --config-set installed_paths $PWD/vendor/escapestudios/symfony2-coding-standard,$PWD/vendor/iadvize/php-convention/phpcs`\n\n## \u003ca name='files'\u003eFiles\u003c/a\u003e\n\n  - Use only `UTF-8 without BOM`.\n\n  - Use only the Unix LF (linefeed) line ending.\n\n  - All PHP files must end with a single blank line.\n\n  - Use the long `\u003c?php ?\u003e` tags for non-views scripts.\n\n```php\n\u003c?php\n\necho 'test';\n```\n\n  - Use the short-echo `\u003c?= ?\u003e` tags for view scripts.\n\n```php\n\u003ctitle\u003e\u003c?=∙$title;∙?\u003e\u003c/title\u003e\n```\n\n  - The closing `?\u003e` tag must be omitted from files containing only PHP.\n\n  - Limit on line length limit must be `200 characters`.\n\n  - File must contain only one statement of namespace.\n\n  - Code must use `4 spaces` for indenting, not tabs.\n\n## \u003ca name='lines'\u003eLines\u003c/a\u003e\n\n  - Blank lines may be added to improve readability and to indicate related blocks of code.\n\n```php\n// nah\nfunction foo()\n{\n    $foo = 'test';\n    $foo = strtoupper($foo);\n    return $foo;\n}\n\n// good\nfunction bar()\n{\n    $foo = 'test';\n    $foo = strtoupper($foo);\n\n    return $foo;\n}\n\n```\n\n  - There must not be more than one statement per line.\n\n```php\n// bad\n$foo = substr(strtoupper('test'), 0, 1);\n\n// good\n$foo = 'test';\n$foo = strtoupper($foo);\n$foo = substr($foo, 0, 1);\n```\n\n## \u003ca name='keywords'\u003eKeywords\u003c/a\u003e\n\n  - All PHP keywords must be in lower case (Eg: `true`, `false`, `null`, etc.)\n\n## \u003ca name='namespaces-use'\u003eNamespaces and use declarations\u003c/a\u003e\n\n  - Namespaces names must be delcared in `UpperCamelCase`.\n\n```php\n// bad\nnamespace Vendor\\fooBar;\n\n// bad\nnamespace Vendor\\foo_bar;\n\n// good\nnamespace Vendor\\FooBar;\n```\n\n  - Namespaces declaration never begin by a backslash `Vendor\\Space\\Space`.\n\n  - There must be one blank line before and after the `namepsace` declaration.\n\n  - There must be one blank line after the block of `use` declaration.\n\n  - `use` declaration must not separated by comma.\n\n  - `use` block declarations must be grouped by package:\n\n```php\n// bad\nuse Foo\\Bar,\n    Qux\\Quux,\n    Foo\\Baz;\n\n// bad\nuse Foo\\Bar;\nuse Qux\\Quux;\nuse Foo\\Baz;\n\n// good\nuse Foo\\Bar;\nuse Foo\\Baz;\nuse Qux\\Quux;\nuse Qux\\Corge\\Grault;\n```\n\n  - `use` alias declaration should be composed with sub-namespaces names.\n\n```php\n// bad\nuse Foo\\Bar as Baz;\n\n// bad\nuse Baz\\Qux\\Quux as BQQ;\n\n// good\nuse Foo\\Bar as FooBar;\n\n// good\nuse Baz\\Qux\\Quux as BazQuxQuux;\n```\n\n## \u003ca name='comments'\u003eComments\u003c/a\u003e\n\n### In-line code comments\n\n  - Comments should be on a separate line immediately before the code line or block they reference.\n\n```php\n// bad\n$foo = 'bar'; // Bar in foo\n\n// good\n// Foo assignment for example\n$foo = 'bar';\n\n// good\n// Foo assignment\n// for example\n$foo = 'bar';\n```\n\n### Block code comments\n\n  - 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.\n\n```php\n/**\n * Foo\n *\n */\nclass Foo\n{\n    /**\n     * The description of bar\n     *\n     * @param string $baz The baz\n     *\n     * @return string The return of bar\n     */\n    public function bar($baz)\n    {\n        // Returned value\n        return 'Do something...';\n    }\n}\n```\n\n  - You must add PHPDoc blocks for all variable references.\n\n```\n/** @var Bar\\Baz $foo Baz object */\n$foo = $bar-\u003ebaz();\n```\n\n```php\n/**\n * Foo\n *\n */\nclass Foo\n{\n    /** @var string $bar It's bar! */\n    public $bar = '';\n}\n```\n  - 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.\n\n```php\n// Bad\nnamespace Vendor\\Bar\\Baz;\n\n/**\n * Foo\n *\n */\nclass Foo\n{\n    /** @var \\Other\\MyClass $myClass */\n    protected $myClass;\n\n    /**\n     * @return \\Other\\MyClass\n     */\n    public function getMyClass()\n    {\n      return $this-\u003emyClass;\n    }\n}\n```\n\n```php\n// Good\nnamespace Vendor\\Bar\\Baz;\n\nuse Other\\MyClass;\n\n/**\n * Foo\n *\n */\nclass Foo\n{\n    /** @var MyClass $myClass */\n    protected $myClass;\n\n    /**\n     * @return MyClass\n     */\n    public function getMyClass()\n    {\n      return $this-\u003emyClass;\n    }\n}\n```\n\n  - `@todo` and `@fixme` must be used in PHPDoc blocks like annotations.\n\n```php\n/** @todo Think to check value */\n$foo = 'bar';\n\n/** @fixme Change qux to quux */\n$baz = 'qux';\n```\n\n\n### Qualify objects you use\n   - you should add `@var` tag when you get object from abstract method\n\n```php\n// bad\n$logger = $this-\u003egetServiceLocator()-\u003eget('logger');\n\n// bad\n$this-\u003egetServiceLocator()-\u003eget('AwesomeFactory')-\u003ecreateAwesomeness();\n\n// good\n/** @var LoggerInterface $logger */\n$logger = $this-\u003egetServiceLocator()-\u003eget('logger');\n\n// good\n/** @var AwesomeFactory $awesomeFactory */\n$awesomeFactory = $this-\u003egetServiceLocator()-\u003eget('AwesomeFactory');\n$awesomeFactory-\u003ecreateAwesomeness()\n\n```\n   - you shouldn't add `@var` tag when you get object from explicit method\n\n```php\n// bad\n/**\n * Class AwesomeFactory\n */\nclass AwesomeFactory\n{\n    /**\n     * @return Awesome\n     */\n     public function createAwesomeness()\n     {\n         return Awesome();\n     }\n}\n$awesomeFactory = new AwesomeFactory();\n/** @var Awesome $awesome */\n$awesome = $awesomeFactory-\u003ecreateAwesomeness();\n\n// good\n/**\n * Class AwesomeFactory\n */\nclass AwesomeFactory\n{\n    /**\n     * @return Awesome\n     */\n     public function createAwesomeness()\n     {\n         return Awesome();\n     }\n}\n$awesomeFactory = new AwesomeFactory();\n$awesome        = $awesomeFactory-\u003ecreateAwesomeness();\n```\n\n## \u003ca name='naming'\u003eNaming\u003c/a\u003e\n\n  - Clarity over brevity in variable, method and class names\n\n```php\n// bad\n$o = new Object();\n\n// bad\nclass A\n{\n}\n\n// bad\npublic function doIt()\n{\n}\n\n// good\n$object = new Object();\n\n// good\nclass Substracter\n{\n}\n\n// good\npublic function associateChannelToOperator()\n{\n}\n```\n\n  - `Boolean` variable names should be either an adjective or a past participle form.\n    Associated getter method should begin with `is` or `has`.\n\n```php\n// bad\n$enablePlugin = true;\n\n// bad\npublic function getEnablePlugin() {}\n\n// bad\npublic function getPluginEnabled() {}\n\n// good\n$pluginEnabled = true;\n\n// good\n$visible = true;\n\n// good\npublic function isPluginEnabled() {}\n\n// good\npublic function isVisible() {}\n```\n\n  - `DateTime` variable names should be a past participle form ending with `At`.\n\n```php\n// bad\n$dateUpdate = new \\DateTime;\n\n// bad\n$endDate = new \\DateTime;\n\n// good\n$updatedAt = new \\DateTime;\n\n// good\n$lastLoggedAt = new \\DateTime;\n```\n\n## \u003ca name='variables'\u003eVariables\u003c/a\u003e\n\n### User variables\n\n  - Variables should be in `lowerCamelCase`.\n\n```php\n// bad\n$_foo='';\n\n// bad\n$foo_bar = '';\n\n// bad\n$fooBar='';\n\n// good\n$fooBar∙=∙'';\n```\n\n  - You must set off operators with spaces.\n\n```php\n// bad\n$foo = (5+6)/5;\n\n// good\n$foo∙=∙(5∙+∙6)∙/∙5;\n```\n\n  - You must conserve a great alignment.\n\n```php\n// bad\n$foo = 'Ba';\n$foo .= 'r';\n$quux = 'Qu';\n$quux .= 'x';\n\n// good\n$foo   = 'Ba';\n$foo  .= 'r';\n$quux  = 'Qu';\n$quux .= 'x';\n```\n\n```php\n// bad\n$fooBarBazQux-\u003ebar()-\u003e\n    baz()-\u003equx();\n\n// good\n$fooBarBazQux\n∙∙∙∙-\u003ebar()\n∙∙∙∙-\u003ebaz()\n∙∙∙∙-\u003equx();\n```\n\n### Global variables\n\n  - You must used `$_POST`, `$_GET` and `$_COOKIE` instead of `$_REQUEST`. If you use a framework, use `Request` component.\n\n## \u003ca name='strings'\u003eStrings\u003c/a\u003e\n\n  - A string must be enclosed in single quotes `'hello'`.\n\n  - A concatenated string must use single quotes `'foo' . $bar`\n\n  - A concatenated string must use spaces around points `'foo' . $bar`\n\n  - A string declaration in multiline must be aligned\n\n```php\n$foo = 'Bar'\n      .'Baz'\n      .'Qux';\n```\n\n  - A string must not concatenate with functions or methods\n\n```php\n// bad\n$foo = ucfisrt('bar') . ' baz';\n\n// good\n$foo = ucfirst('bar');\n$foo = $foo . ' baz';\n\n// very good\n$foo  = ucfirst('bar');\n$foo .= ' baz';\n```\n\n## \u003ca name='constants'\u003eConstants\u003c/a\u003e\n\n### Class constants\n\n  - Constants must be in `UPPER_SNAKE_CASE`.\n\n```php\n// bad\nconst MAXSIZE=5;\n\n// bad\nconst max_size∙=∙4;\n\n// good\nconst MAX_SIZE∙=∙5;\n```\n  - You must set assignment operator with spaces.\n  \n```php\n// bad\nconst MAX_SIZE=5;\n\n// good\nconst MAX_SIZE∙=∙5;\n```\n\n  - You must conserve a great alignment.\n\n```php\n// bad\nconst FOO∙∙=∙'Ba';\nconst FOO∙∙=∙'r';\nconst QUUX∙=∙'Qu';\nconst QUUX∙=∙'x';\n\n// good\nconst FOO∙∙=∙'Ba';\nconst FOO∙∙=∙'r';\nconst QUUX∙=∙'Qu';\nconst QUUX∙=∙'x';\n```\n\n## \u003ca name='type-casting'\u003eType casting\u003c/a\u003e\n\n  - You must use `(int) $foo` instead of `intval($foo)`.\n\n  - You must use `(bool) $foo` instead of `boolval($foo)`.\n\n  - You must use `(float) $foo` instead of `floatval($foo)`.\n\n  - You must use `(string) $foo` instead of `strval($foo)`.\n\n```php\n// bad\n$foo∙=∙(string)$bar;\n\n// good\n$foo∙=∙(string)∙$bar;\n```\n\n## \u003ca name='arrays'\u003eArrays\u003c/a\u003e\n\n  - You must use `[]` notation instead of `array()`.\n\n  - Arrays with few data must be declared like this:\n\n```php\n$foo∙=∙['Bar',∙'Baz',∙'Qux'];\n```\n\n  - Arrays with lots of data must be declared like this:\n\n```php\n$foo = [\n∙∙∙∙'bar'∙∙=\u003e∙'abc',\n∙∙∙∙'baz'∙∙=\u003e∙123,\n∙∙∙∙'qux'∙∙=\u003e∙true,\n∙∙∙∙'quux'∙=\u003e∙[\n∙∙∙∙∙∙∙∙'corge'∙∙=\u003e∙[],\n∙∙∙∙∙∙∙∙'grault'∙=\u003e∙123.456,\n∙∙∙∙],\n];\n```\n\n  - For the arrays with lots of data, lines must be terminated by a comma. (Easy to copy/paste)\n\n## \u003ca name='classes'\u003eClasses, Properties, and Methods\u003c/a\u003e\n\n### Classes\n\n  - The `extends` and `implements` keywords should be declared on the same line as the class name.\n\n  - 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.\n\n```php\n\u003c?php\n\nnamespace Vendor\\Foo;\n\nclass Foo extends Bar implements Baz, Qux, Quux\n{\n    // Do something...\n}\n```\n\n  - 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.\n\n```php\n\u003c?php\n\nnamespace Vendor\\Foo;\n\nclass Foo extends Bar implements\n∙∙∙∙Baz,\n∙∙∙∙Qux,\n∙∙∙∙Quux\n{\n    // Do something...\n}\n```\n\n### Properties\n  - Visibility must be declared on all properties.\n\n```php\n// bad\n/** @var string Property description */\n$foo = '';\n\n// good\n/** @var string Property description */\npublic $foo = '';\n```\n\n  - There must not be more than one property declared per statement.\n\n```php\n// bad\npublic $foo = '',\n       $bar = '';\n\n// good\n/** @var string Property description */\npublic $foo = '';\n\n/** @var string Property description */\nprotected $bar = '';\n```\n\n  - Property names must not be prefixed with a single underscore to indicate `protected` or `private` visibility.\n\n```php\n// bad\n/** @var string Property description */\nprotected $_bar = '';\n\n/** @var string Property description */\nprivate $_baz = '';\n\n// good\n/** @var string Property description */\nprotected $bar = '';\n\n/** @var string Property description */\nprivate $baz = '';\n```\n\n  - When present, the `static` declaration must come after the visibility declaration.\n\n```php\n// bad\n/** @var string $foo Property description */\nstatic public $foo = '';\n\n// good\n/** @var string $foo Property description */\npublic static $foo = '';\n```\n\n### Methods\n  - Visibility must be declared on all methods. (Eg: `public|protected|private foo()`)\n\n  - Method names should not be prefixed with a single underscore to indicate protected or private visibility.\n\n```php\n// bad\nprotected function _foo()\n{\n    // Do something...\n}\n\n// good\nprotected function foo()\n{\n    // Do something...\n}\n```\n\n  - Method names must not be declared with a space after the method name.\n\n```php\n// bad\npublic function foo∙()\n{\n    // Do something...\n}\n\n// good\npublic function foo()\n{\n    // Do something...\n}\n```\n\n  - There must not be a space after the opening parenthesis, and there must not be a space before the closing parenthesis.\n\n```php\n// bad\npublic function foo()∙{∙\n    // Do something...\n∙}\n\n// good\npublic function foo()\n{\n    // Do something...\n}\n```\n\n  - The opening brace must go on its own line, and the closing brace must go on the next line following the body.\n\n```php\n// bad\npublic function foo()∙{\n    // Do something...}\n\n// good\npublic function foo()\n{\n    // Do something...\n}\n```\n  - In the argument list, there must not be a space before each comma, and there must be one space after each comma.\n\n```php\n// bad\npublic function foo($bar∙,∙\u0026$baz∙,∙$qux = [])\n{\n    // Do something...\n}\n\n// bad\npublic function foo(∙$bar, \u0026$baz, $qux = []∙)\n{\n    // Do something...\n}\n\n// good\npublic function foo($bar,∙\u0026$baz,∙$qux∙=∙[])\n{\n    // Do something...\n}\n```\n  - Argument lists may be split across multiple lines, where each subsequent line is indented once.\n\n  - When doing so, the first item in the list must be on the next line, and there must be only one argument per line.\n\n   - 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.\n\n```php\n// good\npublic function foo(\n∙∙∙∙$bar,\n∙∙∙∙\u0026$baz,\n∙∙∙∙$qux = []\n) {\n    // Do something...\n}\n```\n\n  - When present, the `abstract` and `final` declarations must precede the visibility declaration.\n\n  - When present, the `static` declaration must come after the visibility declaration.\n\n```php\n// bad\nprotected abstract function foo();\n\nstatic public final function bar()\n{\n    // Do something...\n}\n\n// good\nabstract protected function foo();\n\nfinal public static function bar()\n{\n    // Do something...\n}\n```\n## \u003ca name='interfaces'\u003eInterfaces, Traits\u003c/a\u003e\n\n### Interfaces\n  - The interface name must be suffixed with `Interface`.\n\n\n```php\n\u003c?php\n\nnamespace Vendor\\Foo;\n\n/**\n * Interface Foo\n *\n */\ninterface FooInterface\n{\n    /**\n     * Set Foo\n     *\n     * @param string $foo\n     */\n    public function setFoo($foo);\n}\n```\n\n### Traits\n  - The trait name must be suffixed with `Trait`.\n\n\n```php\n\u003c?php\n\nnamespace Vendor\\Foo;\n\n/**\n * Trait Foo\n *\n */\ntrait FooTrait\n{\n    /** @var \\Vendor\\Bar */\n    protected $bar;\n\n    /**\n     * Set Bar\n     *\n     * @param string $bar\n     */\n    public function setBar($bar)\n    {\n        $this-\u003ebar = $bar;\n    }\n}\n```\n\n## \u003ca name='function-method-calls'\u003eFunction and Method Calls\u003c/a\u003e\n\n  - There must not be a space between the method or function name and the opening parenthesis.\n\n```php\n// bad\nfoo∙();\n$bar-\u003ebaz∙();\n\n// good\nfoo();\n$bar-\u003ebaz();\n```\n\n  - 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.\n\n```php\n// bad\nfoo(∙$qux∙);\n$bar-\u003ebaz(∙$qux∙);\n\n// good\nfoo($qux);\n$bar-\u003ebaz($qux);\n```\n\n  - There must not be a space before each comma and there must be one space after each comma.\n\n```php\n// bad\nfoo($bar∙,∙$baz∙,∙$qux);\n\n// good\nfoo($bar,∙$baz,∙$qux);\n```\n\n  - 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.\n\n```php\n// bad\nfoo($longFoo,\n    $longBar,\n    $longBaz\n);\n\n// bad\nfoo($longFoo,\n    $longBar,\n    $longBaz);\n\n// good\nfoo(\n∙∙∙∙$longFoo,\n∙∙∙∙$longBar,\n∙∙∙∙$longBaz\n);\n```\n\n  - Chained method calls must be wrapped before the first call and indented once.\n\n```php\n// bad\n$fooBar-\u003ebaz()-\u003equx($param);\n\n// good\n$fooBar\n    -\u003ebaz()\n    -\u003equx($param);\n```\n\n  - When you pass an array as the only argument, the array brackets should be on the same lines as the method parenthesis.\n\n```php\n// bad\nfoo(\n    [\n        'foo' =\u003e 'bar',\n    ]\n);\n\n// good\nfoo([\n    'foo' =\u003e 'bar',\n]);\n```\n\n## \u003ca name='control-structures'\u003eControl structures\u003c/a\u003e\n\n### General\n\n  - There must be one space after the control structure keyword\n\n  - There must not be a space after the opening parenthesis\n\n  - There must not be a space before the closing parenthesis\n\n  - There must be one space between the closing parenthesis and the opening brace\n\n  - The structure body must be indented once\n\n  - The closing brace must be on the next line after the body\n\n  - 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.\n\n### `if`, `elseif`, `else`\n\n- The keyword `elseif` should be used instead of `else if` so that all control keywords look like single words.\n\n#### Example\n\n```php\n// bad\nif(EXPRESSION){\n   // Do something...\n}\n\n// bad\nif (EXPRESSION) {\n// Do something...\n}\n\n// bad\nif (EXPRESSION)\n{\n    // Do something...\n}\n\n// bad\nif (EXPRESSION) {\n    // Do something...\n}\nelse {\n    // Do something...\n}\n\n// good\nif∙(EXPRESSION)∙{\n∙∙∙∙// Do something...\n}∙elseif∙(OTHER_EXPRESSION)∙{\n∙∙∙∙// Do something...\n}∙else∙{\n∙∙∙∙// Do something...\n}\n```\n\n### Ternary (`?:`)\n\n  - You should not use nesting ternary.\n\n#### Example\n\n```php\n// bad\n$foo = EXPRESSION ? 'bar' : OTHER_EXPRESSION ? 'baz' : 'qux';\n\n// good\n$foo∙=∙EXPRESSION∙?∙'bar'∙:∙'baz';\n\n// good\n$foo∙=∙EXPRESSION\n∙∙∙∙?∙'bar'\n∙∙∙∙:∙'baz';\n```\n\n### `switch` and `case`\n\n  - 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.\n\n  - There must be a comment such as `// no break` when fall-through is intentional in a non-empty case body.\n\n#### Example\n\n```php\n// bad\nswitch(EXPRESSION)\n{\n    case 0:\n        // Do something...\n    break;\n}\n\n// good\nswitch∙(EXPRESSION)∙{\n∙∙∙∙case∙0:\n∙∙∙∙∙∙∙∙// Do something...\n∙∙∙∙∙∙∙∙break;\n∙∙∙∙case∙1:\n∙∙∙∙∙∙∙∙// Do something with no break...\n∙∙∙∙∙∙∙∙// no break\n∙∙∙∙case∙2:\n∙∙∙∙case∙3:\n∙∙∙∙case∙4:\n∙∙∙∙∙∙∙∙// Do something with return instead of break...\n∙∙∙∙∙∙∙∙return;\n∙∙∙∙default:\n∙∙∙∙∙∙∙∙// Do something in default case...\n∙∙∙∙∙∙∙∙break;\n}\n```\n\n### `while` and `do while`\n\n#### Example\n\n```php\n// bad\nwhile(EXPRESSION)\n{\n    // Do something...\n}\n\n// bad\ndo\n{\n    // Do something...\n} while(EXPRESSION);\n\n// good\nwhile∙(EXPRESSION)∙{\n∙∙∙∙// Do something...\n}\n\n// good\ndo∙{\n∙∙∙∙// Do something...\n}∙while∙(EXPRESSION);\n```\n\n### `for`\n\n#### Example\n\n```php\n// bad\nfor( $i=0;$i\u003c10;$i++ )\n{\n    // Do something...\n}\n\n// good\nfor∙($i∙=∙0;∙$i∙\u003c∙10;∙$i++)∙{\n∙∙∙∙// Do something...\n}\n```\n\n### `foreach`\n\n#### Example\n\n```php\n// bad\nforeach( $foo as $key=\u003e$value )\n{\n    // Do something...\n}\n\n// good\nforeach∙($foo∙as∙$key∙=\u003e∙$value)∙{\n∙∙∙∙// Do something...\n}\n```\n### `try` and `catch`\n\n#### Example\n\n```php\n// bad\ntry\n{\n    // Do something...\n}\ncatch(FooException $e)\n{\n    // Do something...\n}\n\n// good\ntry∙{\n∙∙∙∙// Do something...\n}∙catch∙(FooException∙$exception)∙{\n∙∙∙∙// Do something...\n}∙catch∙(BarException∙$exception)∙{\n∙∙∙∙// Do something...\n}∙finally∙{\n∙∙∙∙// Do something...\n}\n```\n\n## \u003ca name='closures'\u003eClosures\u003c/a\u003e\n\n  - Closures must be declared with a space after the function keyword, and a space before and after the use keyword.\n\n  - The opening brace must go on the same line, and the closing brace MUST go on the next line following the body.\n\n  - 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.\n\n  - 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.\n\n  - Closure arguments with default values must go at the end of the argument list.\n\n  - Argument lists and variable lists may be split across multiple lines, where each subsequent line is indented once.\n\n  - 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.\n\n  - 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.\n\n### Example (declaration)\n\n```php\n// good\n$closureWithArguments∙=∙function∙($foo,∙$bar)∙{\n∙∙∙∙// Do something...\n};\n\n// good\n$closureWithArgumentsAndVariables∙=∙function∙($foo,∙$bar)∙use∙($baz,∙$qux)∙{\n∙∙∙∙// Do something...\n};\n\n// good\n$longArgumentsNoVariables∙=∙function∙(\n∙∙∙∙$longArgumentFoo,\n∙∙∙∙$longArgumentBar,\n∙∙∙∙$longArgumentBaz\n)∙{\n∙∙∙∙// Do something...\n};\n\n// good\n$noArgumentsLongVariables∙=∙function∙()∙use∙(\n∙∙∙∙$longVariableFoo,\n∙∙∙∙$longVariablBar,\n∙∙∙∙$longVariableBaz\n)∙{\n∙∙∙∙// Do something...\n};\n\n// good\n$longArgumentsLongVariables∙=∙function∙(\n∙∙∙∙$longArgumentFoo,\n∙∙∙∙$longArgumentBar,\n∙∙∙∙$longArgumentBaz\n)∙use∙(\n∙∙∙∙$longVariableFoo,\n∙∙∙∙$longVariableBar,\n∙∙∙∙$longVariableBaz\n)∙{\n∙∙∙∙// Do something...\n};\n\n// good\n$longArgumentsShortVariables∙=∙function∙(\n∙∙∙∙$longArgumentFoo,\n∙∙∙∙$longArgumentBar,\n∙∙∙∙$longArgumentBaz\n)∙use∙($variableFoo)∙{\n∙∙∙∙// Do something...\n};\n\n// good\n$shortArgumentsLongVariables∙=∙function∙($argumentFoo)∙use∙(\n∙∙∙∙$longVariableFoo,\n∙∙∙∙$longVariableBar,\n∙∙∙∙$longVariableBaz\n)∙{\n∙∙∙∙// Do something...\n};\n```\n\n### Example (usage)\n\n```php\n$foo-\u003ebar(\n∙∙∙∙$argumentFoo,\n∙∙∙∙function∙($argumentBar)∙use∙($variableFoo)∙{\n∙∙∙∙∙∙∙∙// Do something...\n∙∙∙∙},\n∙∙∙∙$argumentBaz\n);\n```\n\n## \u003ca name='best-practices'\u003eBest practices\u003c/a\u003e\n\n### Date\n\n  - You must use `new \\DateTime('2014-01-01 00:00:00')` instead of `date('2014-01-01 00:00:00')`.\n\n  - You must use `new \\DateTime('Sunday')` instead of `strtotime('Sunday')`.\n\n### Readibility\n\n  - When possible, avoid nestings of more than 2 levels and prefer \"return early\" structures.\n\n\n```php\n// bad\n$response = [];\nif ($foo) {\n    foreach ($foo-\u003egetBars() as $bar) {\n        if ($bar-\u003ehasBaz()) {\n            // 3 nested levels\n        }\n    }\n}\nreturn $response;\n\n\n// good\n$response = [];\nif (!$foo) {\n    return $response;\n}\n\nforeach ($foo-\u003egetBars() as $bar) {\n    if ($bar-\u003ehasBaz()) {\n        // only 2 nested levels\n    }\n}\nreturn $response;\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiadvize%2Fphp-convention","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fiadvize%2Fphp-convention","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiadvize%2Fphp-convention/lists"}