{"id":19878891,"url":"https://github.com/gtap-dev/php","last_synced_at":"2026-03-02T23:39:31.326Z","repository":{"id":47185454,"uuid":"287518801","full_name":"gtap-dev/php","owner":"gtap-dev","description":"PHP code guidelines","archived":false,"fork":false,"pushed_at":"2021-09-09T11:08:53.000Z","size":14,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-01-11T17:49:17.719Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"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/gtap-dev.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":null,"security":null,"support":null}},"created_at":"2020-08-14T11:37:22.000Z","updated_at":"2021-09-09T11:08:55.000Z","dependencies_parsed_at":"2022-08-02T18:16:12.935Z","dependency_job_id":null,"html_url":"https://github.com/gtap-dev/php","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gtap-dev%2Fphp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gtap-dev%2Fphp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gtap-dev%2Fphp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gtap-dev%2Fphp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gtap-dev","download_url":"https://codeload.github.com/gtap-dev/php/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241309093,"owners_count":19941722,"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-11-12T17:06:59.880Z","updated_at":"2026-03-02T23:39:26.295Z","avatar_url":"https://github.com/gtap-dev.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# PHP Coding Style Guide\r\n\r\nA mostly reasonable approach to PHP\r\n\r\nThis is a superset of the [PSR-12].\r\n\r\n**We suggest you familiarize yourself with the [clean-code] principles first.** \r\n\r\n## 1. Overview\r\n\r\n### 1.1. Previous language versions\r\n\r\nThroughout this document, any instructions may be ignored if they do not exist in versions\r\nof PHP supported by your project.\r\n\r\n## 2. Basic Coding Standard\r\n\r\n## 2.1 Overview\r\n\r\n- Files must use only `\u003c?php` tags.\r\n\r\n- Files must use only UTF-8 without BOM for PHP code.\r\n\r\n- Files should *either* declare symbols (classes, functions, constants, etc.)\r\n  *or* cause side-effects (e.g. generate output, change .ini settings, etc.)\r\n  but should not do both.\r\n\r\n- Class names must be declared in `PascalCase`.\r\n\r\n- Class constants must be declared in all upper case with underscore separators.\r\n\r\n- Class property names should be declared in `camelCase`.\r\n\r\n- Method names must be declared in `camelCase`.\r\n\r\n## 2.2 Autoloader\r\n\r\n- The term \"class\" refers to classes, interfaces, traits, and other similar\r\n   structures.\r\n\r\n- A fully qualified class name has the following form:\r\n\r\n        \\\u003cNamespaceName\u003e(\\\u003cSubNamespaceNames\u003e)*\\\u003cClassName\u003e\r\n\r\n- The fully qualified class name must have a top-level namespace name,\r\n   also known as a \"vendor namespace\".\r\n\r\n- The fully qualified class name may have one or more sub-namespace\r\n   names.\r\n\r\n- The fully qualified class name must have a terminating class name.\r\n\r\n- Underscores have no special meaning in any portion of the fully\r\n   qualified class name.\r\n\r\n- Alphabetic characters in the fully qualified class name may be any\r\n   combination of lower case and upper case.\r\n\r\n- All class names must be referenced in a case-sensitive fashion.\r\n\r\n- When loading a file that corresponds to a fully qualified class name ...\r\n\r\n- A contiguous series of one or more leading namespace and sub-namespace\r\n    names, not including the leading namespace separator, in the fully\r\n    qualified class name (a \"namespace prefix\") corresponds to at least one\r\n    \"base directory\".\r\n\r\n- The contiguous sub-namespace names after the \"namespace prefix\"\r\n    correspond to a subdirectory within a \"base directory\", in which the\r\n    namespace separators represent directory separators. The subdirectory\r\n    name must match the case of the sub-namespace names.\r\n\r\n- The terminating class name corresponds to a file name ending in `.php`.\r\n    The file name must match the case of the terminating class name.\r\n\r\n- Autoloader implementations must not throw exceptions, must not raise errors\r\n    of any level, and should not return a value.\r\n \r\n## 2.3 Files\r\n\r\nAll PHP files must use the Unix LF (linefeed) line ending only.\r\n\r\nAll PHP files must end with a non-blank line, terminated with a single LF.\r\n\r\nThe closing `?\u003e` tag must be omitted from files containing only PHP.\r\n\r\nPHP code must use the long `\u003c?php ?\u003e` tags, it must not use the other tag variations.\r\n\r\nPHP code must use only UTF-8 without BOM.\r\n\r\n### 2.3.1 Lines\r\n\r\nThere must not be a hard limit on line length.\r\n\r\nThe soft limit on line length must be 120 characters.\r\n\r\nThere must not be trailing whitespace at the end of lines.\r\n\r\nBlank lines may be added to improve readability and to indicate related\r\nblocks of code except where explicitly forbidden.\r\n\r\nThere must not be more than one statement (statement ends with a semicolon) per line.\r\n\r\n### 2.3.2 Indenting\r\n\r\nCode must use an indent of 4 spaces for each indent level, and must not use\r\ntabs for indenting.\r\n\r\n### 2.3.3 Side Effects\r\n\r\nA file should declare new symbols (classes, functions, constants,\r\netc.) and cause no other side effects, or it should execute logic with side\r\neffects, but should not do both.\r\n\r\nThe phrase \"side effects\" means execution of logic not directly related to\r\ndeclaring classes, functions, constants, etc., *merely from including the\r\nfile*.\r\n\r\n\"Side effects\" include but are not limited to: generating output, explicit\r\nuse of `require` or `include`, connecting to external services, modifying ini\r\nsettings, emitting errors or exceptions, modifying global or static variables,\r\nreading from or writing to a file, and so on.\r\n\r\nThe following is an example of a file with both declarations and side effects;\r\ni.e, an example of what to avoid:\r\n\r\n~~~php\r\n\u003c?php\r\n// side effect: change ini settings\r\nini_set('error_reporting', E_ALL);\r\n\r\n// side effect: loads a file\r\ninclude 'file.php';\r\n\r\n// side effect: generates output\r\necho '\u003chtml\u003e';\r\n\r\n// declaration\r\nfunction foo() {\r\n    // function body\r\n}\r\n~~~\r\n\r\nThe following example is of a file that contains declarations without side\r\neffects; i.e., an example of what to emulate:\r\n\r\n~~~php\r\n\u003c?php\r\n// declaration\r\nfunction foo() {\r\n    // function body\r\n}\r\n\r\n// conditional declaration is *not* a side effect\r\nif (!function_exists('bar')) {\r\n    function bar() {\r\n        // function body\r\n    }\r\n}\r\n~~~\r\n\r\n## 2.4. Namespace and Class Names\r\n\r\nThis means each class should be in a file by itself, and is in a namespace of at\r\nleast one level: a top-level vendor name.\r\nExceptions can be made for small utility classes which are only used in the same file.\r\n\r\nClass names must be declared in `PascalCase`.\r\n\r\nCode must use formal namespaces.\r\n\r\nFor example:\r\n\r\n~~~php\r\n\u003c?php\r\nnamespace Vendor\\Model;\r\n\r\nclass Foo {\r\n}\r\n~~~\r\n\r\n## 2.5. Class Constants, Properties, and Methods\r\n\r\nThe term \"class\" refers to all classes, interfaces, and traits.\r\n\r\n### 2.5.1. Constants\r\n\r\nClass constants must be declared in all upper case with underscore separators.\r\nFor example:\r\n\r\n~~~php\r\n\u003c?php\r\nnamespace Vendor\\Model;\r\n\r\nclass Foo {\r\n    const VERSION = '1.0';\r\n    const DATE_APPROVED = '2012-06-01';\r\n}\r\n~~~\r\n\r\n### 2.5.2. Properties\r\n\r\nWhatever naming convention is used should be applied consistently within a\r\nreasonable scope. That scope may be vendor-level, package-level, class-level,\r\nor method-level but `camelCase` should be preferred.\r\n\r\n### 2.5.3. Methods\r\n\r\nMethod names must be declared in `camelCase()`, unless overriding a method of the parent class.\r\n\r\n### 2.5.4. Keywords and Types\r\n\r\nAll PHP reserved keywords and types [[1]][keywords][[2]][types] must be in lower case.\r\n\r\nAny new types and keywords added to future PHP versions must be in lower case.\r\n\r\nShort form of type keywords must be used i.e. `bool` instead of `boolean`,\r\n`int` instead of `integer` etc.\r\n\r\nUsing types is preferred but not required. Use with caution!\r\n\r\n~~~php\r\nfunction fooBar(string $foo): string {\r\n    return sprintf('%sBar', $foo);\r\n}\r\n\r\n// nullable string\r\nfunction fooBarBaz(?string $foo): ?string {\r\n    if ($foo) {\r\n        return sprintf('%sBarBaz', $foo);\r\n    }\r\n    \r\n    return null;\r\n}\r\n~~~\r\n\r\n~~~php\r\n// nullable string staring from PHP 8\r\nfunction fooBarBaz(string|null $foo): string|null {\r\n    if ($foo) {\r\n        return sprintf('%sBarBaz', $foo);\r\n    }\r\n    \r\n    return null;\r\n}\r\n~~~\r\n\r\n## 3. Declare Statements, Namespace, and Import Statements\r\n\r\nThe header of a PHP file may consist of a number of different blocks. If present,\r\neach of the blocks below must be separated by a single blank line, and must not contain\r\na blank line. Each block must be in the order listed below, although blocks that are\r\nnot relevant may be omitted.\r\n\r\n* Opening `\u003c?php` tag.\r\n* File-level docblock.\r\n* One or more declare statements.\r\n* The namespace declaration of the file.\r\n* One or more class-based `use` import statements.\r\n* One or more function-based `use` import statements.\r\n* One or more constant-based `use` import statements.\r\n* The remainder of the code in the file.\r\n\r\nWhen a file contains a mix of HTML and PHP, any of the above sections may still\r\nbe used. If so, they must be present at the top of the file, even if the\r\nremainder of the code consists of a closing PHP tag and then a mixture of HTML and\r\nPHP.\r\n\r\nWhen the opening `\u003c?php` tag is on the first line of the file, it must be on its\r\nown line with no other statements unless it is a file containing markup outside of PHP\r\nopening and closing tags.\r\n\r\nImport statements must never begin with a leading backslash as they\r\nmust always be fully qualified.\r\n\r\nImport statements should be logically ordered and grouped.\r\n\r\nThe following example illustrates a complete list of all blocks:\r\n\r\n~~~php\r\n\u003c?php\r\n\r\n/**\r\n * This file contains an example of coding styles.\r\n */\r\n\r\ndeclare(strict_types=1);\r\n\r\nnamespace Vendor\\Package;\r\n\r\nuse Vendor\\Package\\{ClassA as A, ClassB, ClassC as C};\r\nuse Vendor\\Package\\SomeNamespace\\ClassD as D;\r\nuse Vendor\\Package\\AnotherNamespace\\ClassE as E;\r\n\r\nuse function Vendor\\Package\\{functionA, functionB, functionC};\r\nuse function Another\\Vendor\\functionD;\r\n\r\nuse const Vendor\\Package\\{CONSTANT_A, CONSTANT_B, CONSTANT_C};\r\nuse const Another\\Vendor\\CONSTANT_D;\r\n\r\n/**\r\n * FooBar is an example class.\r\n */\r\nclass FooBar {\r\n    // ... additional PHP code ...\r\n}\r\n\r\n~~~\r\n\r\nCompound namespaces with a depth of more than two must not be used. Therefore the\r\nfollowing is the maximum compounding depth allowed:\r\n~~~php\r\n\u003c?php\r\n\r\nuse Vendor\\Package\\SomeNamespace\\{\r\n    SubnamespaceOne\\ClassA,\r\n    SubnamespaceOne\\ClassB,\r\n    SubnamespaceTwo\\ClassY,\r\n    ClassZ,\r\n};\r\n~~~\r\n\r\nAnd the following would not be allowed:\r\n\r\n~~~php\r\n\u003c?php\r\n\r\nuse Vendor\\Package\\SomeNamespace\\{\r\n    SubnamespaceOne\\AnotherNamespace\\ClassA,\r\n    SubnamespaceOne\\ClassB,\r\n    ClassZ,\r\n};\r\n~~~\r\n\r\nWhen wishing to declare strict types in files containing markup outside PHP\r\nopening and closing tags, the declaration must be on the first line of the file\r\nand include an opening PHP tag, the strict types declaration and closing tag.\r\n\r\nFor example:\r\n~~~php\r\n\u003c?php declare(strict_types=1) ?\u003e\r\n\u003chtml\u003e\r\n\u003cbody\u003e\r\n    \u003c?php\r\n        // ... additional PHP code ...\r\n    ?\u003e\r\n\u003c/body\u003e\r\n\u003c/html\u003e\r\n~~~\r\n\r\nDeclare statements must contain no spaces and must be exactly `declare(strict_types=1)`\r\n(with an optional semi-colon terminator).\r\n\r\nBlock declare statements are allowed and must be formatted as below. Note position of\r\nbraces and spacing:\r\n~~~php\r\ndeclare(ticks=1) {\r\n    // some code\r\n}\r\n~~~\r\n\r\n## 4. Classes, Properties, and Methods\r\n\r\nThe term \"class\" refers to all classes, interfaces, and traits.\r\n\r\nAny closing brace must not be followed by any comment or statement on the\r\nsame line.\r\n\r\nWhen instantiating a new class, parentheses must always be present even when\r\nthere are no arguments passed to the constructor.\r\n\r\n~~~php\r\nnew Foo();\r\n~~~\r\n\r\n### 4.1 Extends and Implements\r\n\r\nThe `extends` and `implements` keywords must be declared on the same line as\r\nthe class name.\r\n\r\nThe opening brace for the class must go on the same line; the closing brace\r\nfor the class must go on the next line after the body.\r\n\r\nOpening braces must be on the same line and must not be preceded or followed\r\nby a blank line.\r\n\r\nClosing braces must be on their own line and must not be preceded by a blank\r\nline.\r\n\r\n~~~php\r\n\u003c?php\r\n\r\nnamespace Vendor\\Package;\r\n\r\nuse FooClass;\r\nuse BarClass as Bar;\r\nuse OtherVendor\\OtherPackage\\BazClass;\r\n\r\nclass ClassName extends ParentClass implements \\ArrayAccess, \\Countable {\r\n    // constants, properties, methods\r\n}\r\n~~~\r\n\r\nLists of `implements` and, in the case of interfaces, `extends` may be split\r\nacross multiple lines, where each subsequent line is indented once. When doing\r\nso, the first item in the list must be on the next line, and there must be only\r\none interface per line.\r\n\r\n~~~php\r\n\u003c?php\r\n\r\nnamespace Vendor\\Package;\r\n\r\nuse FooClass;\r\nuse BarClass as Bar;\r\nuse OtherVendor\\OtherPackage\\BazClass;\r\n\r\nclass ClassName extends ParentClass implements\r\n    \\ArrayAccess,\r\n    \\Countable,\r\n    \\Serializable {\r\n    // constants, properties, methods\r\n}\r\n~~~\r\n\r\n### 4.2 Using traits\r\n\r\nThe `use` keyword used inside the classes to implement traits must be\r\ndeclared on the next line after the opening brace.\r\n\r\n~~~php\r\n\u003c?php\r\n\r\nnamespace Vendor\\Package;\r\n\r\nuse Vendor\\Package\\FirstTrait;\r\n\r\nclass ClassName {\r\n    use FirstTrait;\r\n}\r\n~~~\r\n\r\nEach individual trait that is imported into a class must be included\r\none-per-line and each inclusion must have its own `use` import statement.\r\n\r\n~~~php\r\n\u003c?php\r\n\r\nnamespace Vendor\\Package;\r\n\r\nuse Vendor\\Package\\FirstTrait;\r\nuse Vendor\\Package\\SecondTrait;\r\nuse Vendor\\Package\\ThirdTrait;\r\n\r\nclass ClassName {\r\n    use FirstTrait;\r\n    use SecondTrait;\r\n    use ThirdTrait;\r\n}\r\n~~~\r\n\r\nWhen the class has nothing after the `use` import statement, the class\r\nclosing brace must be on the next line after the `use` import statement.\r\n\r\n~~~php\r\n\u003c?php\r\n\r\nnamespace Vendor\\Package;\r\n\r\nuse Vendor\\Package\\FirstTrait;\r\n\r\nclass ClassName {\r\n    use FirstTrait;\r\n}\r\n~~~\r\n\r\nOtherwise, it must have a blank line after the `use` import statement.\r\n\r\n~~~php\r\n\u003c?php\r\n\r\nnamespace Vendor\\Package;\r\n\r\nuse Vendor\\Package\\FirstTrait;\r\n\r\nclass ClassName {\r\n    use FirstTrait;\r\n\r\n    private $property;\r\n}\r\n~~~\r\n\r\nWhen using the `insteadof` and `as` operators they must be used as follows taking\r\nnote of indentation, spacing, and new lines.\r\n\r\n~~~php\r\n\u003c?php\r\n\r\nclass Talker {\r\n    use A;\r\n    use B {\r\n        A::smallTalk insteadof B;\r\n    }\r\n    use C {\r\n        B::bigTalk insteadof C;\r\n        C::mediumTalk as FooBar;\r\n    }\r\n}\r\n~~~\r\n\r\n### 4.3 Properties and Constants\r\n\r\nVisibility must be declared on all properties and constants.\r\n\r\nThe `var` keyword must not be used to declare a property.\r\n\r\nThere must not be more than one property declared per statement.\r\n\r\nProperty names must not be prefixed with a single underscore to indicate\r\nprotected or private visibility. That is, an underscore prefix explicitly has\r\nno meaning.\r\n\r\nThere must be a space between type declaration and property name.\r\n\r\nA property declaration looks like the following:\r\n\r\nStatic class properties should be declared before non-static properties.\r\n\r\n~~~php\r\n\u003c?php\r\n\r\nnamespace Vendor\\Package;\r\n\r\nclass ClassName {\r\n    public static int $bar = 0;\r\n    public $foo = null;\r\n}\r\n~~~\r\n\r\n### 4.4 Methods and Functions\r\n\r\nVisibility must be declared on all methods.\r\n\r\nMethod names must not be prefixed with a single underscore to indicate\r\nprotected or private visibility. That is, an underscore prefix explicitly has\r\nno meaning.\r\n\r\nMethod and function names must not be declared with space after the method name. The\r\nopening brace must go on the same line, and the closing brace must go on the\r\nnext line following the body. There must not be a space after the opening\r\nparenthesis, and there must not be a space before the closing parenthesis.\r\n\r\nStatic class methods should be declared after non-static methods.\r\n\r\nA method declaration looks like the following. Note the placement of\r\nparentheses, commas, spaces, and braces:\r\n\r\n~~~php\r\n\u003c?php\r\n\r\nnamespace Vendor\\Package;\r\n\r\nclass ClassName {\r\n    public function fooBarBaz($arg1, \u0026$arg2, $arg3 = []) {\r\n        // method body\r\n    }\r\n    \r\n    public static function fooBar($arg1, \u0026$arg2, $arg3 = []) {\r\n        // method body\r\n    }\r\n}\r\n~~~\r\n\r\nA function declaration looks like the following. Note the placement of\r\nparentheses, commas, spaces, and braces:\r\n\r\n~~~php\r\n\u003c?php\r\n\r\nfunction fooBarBaz($arg1, \u0026$arg2, $arg3 = []) {\r\n    // function body\r\n}\r\n~~~\r\n\r\n### 4.5 Method and Function Arguments\r\n\r\nIn the argument list, there must not be a space before each comma, and there\r\nmust be one space after each comma.\r\n\r\nMethod and function arguments with default values must go at the end of the argument\r\nlist.\r\n\r\n~~~php\r\n\u003c?php\r\n\r\nnamespace Vendor\\Package;\r\n\r\nclass ClassName {\r\n    public function foo(int $arg1, \u0026$arg2, $arg3 = []) {\r\n        // method body\r\n    }\r\n}\r\n~~~\r\n\r\nArgument lists may be split across multiple lines, where each subsequent line\r\nis indented once. When doing so, the first item in the list must be on the\r\nnext line, and there must be only one argument per line.\r\n\r\nWhen the argument list is split across multiple lines, the closing parenthesis\r\nand opening brace must be placed together on their own line with one space\r\nbetween them.\r\n\r\n~~~php\r\n\u003c?php\r\n\r\nnamespace Vendor\\Package;\r\n\r\nclass ClassName {\r\n    public function aVeryLongMethodName(\r\n        ClassTypeHint $arg1,\r\n        \u0026$arg2,\r\n        array $arg3 = []\r\n    ) {\r\n        // method body\r\n    }\r\n}\r\n~~~\r\n\r\nWhen you have a return type declaration present, there must be one space after\r\nthe colon followed by the type declaration. The colon and declaration must be\r\non the same line as the argument list closing parenthesis with no spaces between\r\nthe two characters.\r\n\r\n~~~php\r\n\u003c?php\r\n\r\ndeclare(strict_types=1);\r\n\r\nnamespace Vendor\\Package;\r\n\r\nclass ReturnTypeVariations {\r\n    public function functionName(int $arg1, $arg2): string {\r\n        return 'foo';\r\n    }\r\n\r\n    public function anotherFunction(\r\n        string $foo,\r\n        string $bar,\r\n        int $baz\r\n    ): string {\r\n        return 'foo';\r\n    }\r\n}\r\n~~~\r\n\r\nIn nullable type declarations, there must not be a space between the question mark\r\nand the type.\r\n\r\n~~~php\r\n\u003c?php\r\n\r\ndeclare(strict_types=1);\r\n\r\nnamespace Vendor\\Package;\r\n\r\nclass ReturnTypeVariations {\r\n    public function functionName(?string $arg1, ?int \u0026$arg2): ?string {\r\n        return 'foo';\r\n    }\r\n}\r\n~~~\r\n\r\nWhen using the reference operator `\u0026` before an argument, there must not be\r\na space after it, like in the previous example.\r\n\r\nThere must not be a space between the variadic three dot operator and the argument\r\nname:\r\n\r\n```php\r\npublic function process(string $algorithm, ...$parts) {\r\n    // processing\r\n}\r\n```\r\n\r\nWhen combining both the reference operator and the variadic three dot operator,\r\nthere must not be any space between the two of them:\r\n\r\n```php\r\npublic function process(string $algorithm, \u0026...$parts) {\r\n    // processing\r\n}\r\n```\r\n\r\n### 4.6 `abstract`, `final`, and `static`\r\n\r\nWhen present, the `abstract` and `final` declarations must precede the\r\nvisibility declaration.\r\n\r\nWhen present, the `static` declaration must come after the visibility\r\ndeclaration.\r\n\r\n~~~php\r\n\u003c?php\r\n\r\nnamespace Vendor\\Package;\r\n\r\nabstract class ClassName {\r\n    protected static $foo;\r\n\r\n    abstract protected function zim();\r\n\r\n    final public static function bar() {\r\n        // method body\r\n    }\r\n}\r\n~~~\r\n\r\n### 4.7 Method and Function Calls\r\n\r\nWhen making a method or function call, there must not be a space between the\r\nmethod or function name and the opening parenthesis, there must not be a space\r\nafter the opening parenthesis, and there must not be a space before the\r\nclosing parenthesis. In the argument list, there must not be a space before\r\neach comma, and there must be one space after each comma.\r\n\r\n~~~php\r\n\u003c?php\r\n\r\nbar();\r\n$foo-\u003ebar($arg1);\r\nFoo::bar($arg2, $arg3);\r\n~~~\r\n\r\nArgument lists may be split across multiple lines, where each subsequent line\r\nis indented once. When doing so, the first item in the list must be on the\r\nnext line, and there must be only one argument per line. A single argument (except for anonymous function) being\r\nsplit across multiple lines (as might be the case with an anonymous function or\r\narray) constitutes splitting the argument list itself.\r\n\r\n~~~php\r\n\u003c?php\r\n\r\n$foo-\u003ebar(\r\n    $longArgument,\r\n    $longerArgument,\r\n    $muchLongerArgument\r\n);\r\n~~~\r\n\r\n~~~php\r\n\u003c?php\r\n\r\nsomefunction(function () {\r\n    // ...\r\n}, $foo, $bar);\r\n\r\nsomefunction(\r\n    $foo,\r\n    $bar,\r\n    [\r\n      // ...\r\n    ],\r\n    $baz\r\n);\r\n\r\n$app-\u003eget('/hello/{name}', function ($name) use ($app) {\r\n    return 'Hello ' . $app-\u003eescape($name);\r\n});\r\n~~~\r\n\r\n## 5. Control Structures\r\n\r\nThe general style rules for control structures are as follows:\r\n\r\n- There must be one space after the control structure keyword\r\n- There must not be a space after the opening parenthesis\r\n- There must not be a space before the closing parenthesis\r\n- There must be one space between the closing parenthesis and the opening\r\n  brace\r\n- The structure body must be indented once\r\n- The body must be on the next line after the opening brace\r\n- The closing brace must be on the next line after the body\r\n\r\nThe body of each structure must be enclosed by braces. This standardizes how\r\nthe structures look and reduces the likelihood of introducing errors as new\r\nlines get added to the body.\r\n\r\n### 5.1 `if`, `else if`, `else`\r\n\r\nAn `if` structure looks like the following. Note the placement of parentheses,\r\nspaces, and braces; and that `else` and `else if` are on the same line as the\r\nclosing brace from the earlier body.\r\n\r\n~~~php\r\n\u003c?php\r\n\r\nif ($expr1) {\r\n    // if body\r\n} else if ($expr2) {\r\n    // else if body\r\n} else {\r\n    // else body;\r\n}\r\n~~~\r\n\r\nThe keyword `else if` should be used instead of `elseif`.\r\n\r\nExpressions in parentheses may be split across multiple lines, where each\r\nsubsequent line is indented at least once. When doing so, the first condition\r\nmust be on the next line. The closing parenthesis and opening brace must be\r\nplaced together on their own line with one space between them. Boolean\r\noperators between conditions must always be at the beginning or at the end of\r\nthe line, not a mix of both.\r\n\r\n~~~php\r\n\u003c?php\r\n\r\nif (\r\n    $expr1\r\n    \u0026\u0026 ($expr2 || $expr3)\r\n    \u0026\u0026 $expr4\r\n) {\r\n    // if body\r\n} else if (\r\n    $expr1\r\n    \u0026\u0026 $expr2\r\n) {\r\n    // else if body\r\n}\r\n~~~\r\n\r\n### 5.2 `switch`, `case`\r\n\r\nA `switch` structure looks like the following. Note the placement of\r\nparentheses, spaces, and braces. The `case` statement must be indented once\r\nfrom `switch`, and the `break` keyword (or other terminating keywords) must be\r\nindented at the same level as the `case` body. There must be a comment such as\r\n`// no break` when fall-through is intentional in a non-empty `case` body.\r\n\r\n~~~php\r\n\u003c?php\r\n\r\nswitch ($expr) {\r\n    case 0:\r\n        echo 'First case, with a break';\r\n        break;\r\n    case 1:\r\n        echo 'Second case, which falls through';\r\n        // no break\r\n    case 2:\r\n    case 3:\r\n    case 4:\r\n        echo 'Third case, return instead of break';\r\n        return;\r\n    default:\r\n        echo 'Default case';\r\n        break;\r\n}\r\n~~~\r\n\r\nExpressions in parentheses may be split across multiple lines, where each\r\nsubsequent line is indented at least once. When doing so, the first condition\r\nmust be on the next line. The closing parenthesis and opening brace must be\r\nplaced together on their own line with one space between them. Boolean\r\noperators between conditions must always be at the beginning or at the end of\r\nthe line, not a mix of both.\r\n\r\n~~~php\r\n\u003c?php\r\n\r\nswitch (\r\n    $expr1\r\n    \u0026\u0026 $expr2\r\n) {\r\n    // structure body\r\n}\r\n~~~\r\n\r\n### 5.3 `while`, `do while`\r\n\r\nA `while` statement looks like the following. Note the placement of\r\nparentheses, spaces, and braces.\r\n\r\n~~~php\r\n\u003c?php\r\n\r\nwhile ($expr) {\r\n    // structure body\r\n}\r\n~~~\r\n\r\nExpressions in parentheses may be split across multiple lines, where each\r\nsubsequent line is indented at least once. When doing so, the first condition\r\nmust be on the next line. The closing parenthesis and opening brace must be\r\nplaced together on their own line with one space between them. Boolean\r\noperators between conditions must always be at the beginning or at the end of\r\nthe line, not a mix of both.\r\n\r\n~~~php\r\n\u003c?php\r\n\r\nwhile (\r\n    $expr1\r\n    \u0026\u0026 $expr2\r\n) {\r\n    // structure body\r\n}\r\n~~~\r\n\r\nSimilarly, a `do while` statement looks like the following. Note the placement\r\nof parentheses, spaces, and braces.\r\n\r\n~~~php\r\n\u003c?php\r\n\r\ndo {\r\n    // structure body;\r\n} while ($expr);\r\n~~~\r\n\r\nExpressions in parentheses may be split across multiple lines, where each\r\nsubsequent line is indented at least once. When doing so, the first condition\r\nmust be on the next line. Boolean operators between conditions must\r\nalways be at the beginning or at the end of the line, not a mix of both.\r\n\r\n~~~php\r\n\u003c?php\r\n\r\ndo {\r\n    // structure body;\r\n} while (\r\n    $expr1\r\n    \u0026\u0026 $expr2\r\n);\r\n~~~\r\n\r\n### 5.4 `for`\r\n\r\nA `for` statement looks like the following. Note the placement of parentheses,\r\nspaces, and braces.\r\n\r\n~~~php\r\n\u003c?php\r\n\r\nfor ($i = 0; $i \u003c 10; $i++) {\r\n    // for body\r\n}\r\n~~~\r\n\r\nExpressions in parentheses may be split across multiple lines, where each\r\nsubsequent line is indented at least once. When doing so, the first expression\r\nmust be on the next line. The closing parenthesis and opening brace must be\r\nplaced together on their own line with one space between them.\r\n\r\n~~~php\r\n\u003c?php\r\n\r\nfor (\r\n    $i = 0;\r\n    $i \u003c 10;\r\n    $i++\r\n) {\r\n    // for body\r\n}\r\n~~~\r\n\r\n### 5.5 `foreach`\r\n\r\nA `foreach` statement looks like the following. Note the placement of\r\nparentheses, spaces, and braces.\r\n\r\n~~~php\r\n\u003c?php\r\n\r\nforeach ($iterable as $key =\u003e $value) {\r\n    // foreach body\r\n}\r\n~~~\r\n\r\n### 5.6 `try`, `catch`, `finally`\r\n\r\nA `try-catch-finally` block looks like the following. Note the placement of\r\nparentheses, spaces, and braces.\r\n\r\n~~~php\r\n\u003c?php\r\n\r\ntry {\r\n    // try body\r\n} catch (FirstThrowableType $e) {\r\n    // catch body\r\n} catch (OtherThrowableType | AnotherThrowableType $e) {\r\n    // catch body\r\n} finally {\r\n    // finally body\r\n}\r\n~~~\r\n\r\n## 6. Strings\r\n\r\nUse single quotes '' for strings or \"\" for string templates.\r\n\r\nStrings that cause the line to go over 100 characters should not be written across multiple lines using string concatenation.\r\n\r\n~~~php\r\n$errorMessage = 'This is a super long error that was thrown because of Batman. When you stop to think about how Batman had anything to do with this, you would get nowhere fast.';\r\n~~~\r\n\r\n### 6.1 String interpolation\r\n\r\nPrefer sprintf/printf or string template \"\" for string interpolation.\r\n\r\n~~~php\r\n$text = sprintf('Hello %s, welcome to our class %s.', $name, $className);\r\n~~~\r\n\r\n~~~php\r\n$text = \"Hello $name, welcome to our class $className.\";\r\n~~~\r\n\r\nUse dot operator only for single concatenation.\r\n\r\n~~~php\r\n$price = $price . '€';\r\n~~~\r\n\r\n## 7. Operators\r\n\r\nStyle rules for operators are grouped by arity (the number of operands they take).\r\n\r\nWhen space is permitted around an operator, multiple spaces may be\r\nused for readability purposes.\r\n\r\nAll operators not described here are left undefined.\r\n\r\n### 7.1. Unary operators\r\n\r\nThe increment/decrement operators must not have any space between\r\nthe operator and operand.\r\n~~~php\r\n$i++;\r\n++$j;\r\n~~~\r\n\r\nType casting operators must not have any space within the parentheses:\r\n~~~php\r\n$intValue = (int) $input;\r\n~~~\r\n\r\n### 7.2. Binary operators\r\n\r\nAll binary [arithmetic][], [comparison][], [assignment][], [bitwise][],\r\n[logical][], [string][], and [type][] operators must be preceded and\r\nfollowed by at least one space:\r\n~~~php\r\nif ($a === $b) {\r\n    $foo = $bar ?? $a ?? $b;\r\n} else if ($a \u003e $b) {\r\n    $foo = $a + $b * $c;\r\n}\r\n~~~\r\n\r\n### 7.3. Ternary operators\r\n\r\nThe conditional operator, also known simply as the ternary operator, must be\r\npreceded and followed by at least one space around both the `?`\r\nand `:` characters:\r\n~~~php\r\n$variable = $foo ? 'foo' : 'bar';\r\n~~~\r\n\r\nWhen the middle operand of the conditional operator is omitted, the operator\r\nmust follow the same style rules as other binary [comparison][] operators:\r\n~~~php\r\n$variable = $foo ?: 'bar';\r\n~~~\r\n\r\n## 8. Closures\r\n\r\nClosures must be declared with a space after the `function` keyword, and a\r\nspace before and after the `use` keyword.\r\n\r\nThe opening brace must go on the same line, and the closing brace must go on\r\nthe next line following the body.\r\n\r\nThere must not be a space after the opening parenthesis of the argument list\r\nor variable list, and there must not be a space before the closing parenthesis\r\nof the argument list or variable list.\r\n\r\nIn the argument list and variable list, there must not be a space before each\r\ncomma, and there must be one space after each comma.\r\n\r\nClosure arguments with default values must go at the end of the argument\r\nlist.\r\n\r\nIf a return type is present, it must follow the same rules as with normal\r\nfunctions and methods; if the `use` keyword is present, the colon must follow\r\nthe `use` list closing parentheses with no spaces between the two characters.\r\n\r\nA closure declaration looks like the following. Note the placement of\r\nparentheses, commas, spaces, and braces:\r\n\r\n~~~php\r\n\u003c?php\r\n\r\n$closureWithArgs = function ($arg1, $arg2) {\r\n    // body\r\n};\r\n\r\n$closureWithArgsAndVars = function ($arg1, $arg2) use ($var1, $var2) {\r\n    // body\r\n};\r\n\r\n$closureWithArgsVarsAndReturn = function ($arg1, $arg2) use ($var1, $var2): bool {\r\n    // body\r\n};\r\n~~~\r\n\r\nArgument lists and variable lists may be split across multiple lines, where\r\neach subsequent line is indented once. When doing so, the first item in the\r\nlist must be on the next line, and there must be only one argument or variable\r\nper line.\r\n\r\nWhen the ending list (whether of arguments or variables) is split across\r\nmultiple lines, the closing parenthesis and opening brace must be placed\r\ntogether on their own line with one space between them.\r\n\r\nThe following are examples of closures with and without argument lists and\r\nvariable lists split across multiple lines.\r\n\r\n~~~php\r\n\u003c?php\r\n\r\n$longArgs_noVars = function (\r\n    $longArgument,\r\n    $longerArgument,\r\n    $muchLongerArgument\r\n) {\r\n   // body\r\n};\r\n\r\n$noArgs_longVars = function () use (\r\n    $longVar1,\r\n    $longerVar2,\r\n    $muchLongerVar3\r\n) {\r\n   // body\r\n};\r\n\r\n$longArgs_longVars = function (\r\n    $longArgument,\r\n    $longerArgument,\r\n    $muchLongerArgument\r\n) use (\r\n    $longVar1,\r\n    $longerVar2,\r\n    $muchLongerVar3\r\n) {\r\n   // body\r\n};\r\n\r\n$longArgs_shortVars = function (\r\n    $longArgument,\r\n    $longerArgument,\r\n    $muchLongerArgument\r\n) use ($var1) {\r\n   // body\r\n};\r\n\r\n$shortArgs_longVars = function ($arg) use (\r\n    $longVar1,\r\n    $longerVar2,\r\n    $muchLongerVar3\r\n) {\r\n   // body\r\n};\r\n~~~\r\n\r\nNote that the formatting rules also apply when the closure is used directly\r\nin a function or method call as an argument.\r\n\r\n~~~php\r\n\u003c?php\r\n\r\n$foo-\u003ebar(\r\n    $arg1,\r\n    function ($arg2) use ($var1) {\r\n        // body\r\n    },\r\n    $arg3\r\n);\r\n~~~\r\n\r\n## 9. Anonymous Classes\r\n\r\nAnonymous Classes must follow the same guidelines and principles as closures\r\nin the above section.\r\n\r\n~~~php\r\n\u003c?php\r\n\r\n$instance = new class {};\r\n~~~\r\n\r\nThe opening brace may be on the same line as the `class` keyword so long as\r\nthe list of `implements` interfaces does not wrap. If the list of interfaces\r\nwraps, the brace must be placed on the same line following the last\r\ninterface.\r\n\r\n~~~php\r\n\u003c?php\r\n\r\n// Brace on the same line\r\n$instance = new class extends \\Foo implements \\HandleableInterface {\r\n    // Class content\r\n};\r\n\r\n$instance = new class extends \\Foo implements\r\n    \\ArrayAccess,\r\n    \\Countable,\r\n    \\Serializable {\r\n    // Class content\r\n};\r\n~~~\r\n\r\n## 10. Comments\r\n\r\nUse /** ... */ for multiline comments.\r\n\r\nUse // for single line comments. Place single line comments on a newline above the subject of the comment. Put an empty line before the comment unless it’s on the first line of a block.\r\n\r\nStart all comments with a space to make it easier to read.\r\n\r\n~~~php\r\n// fooBarBaz\r\n\r\n/**\r\n * foo\r\n * bar\r\n * baz\r\n */\r\n~~~\r\n\r\nPrefer meaningful and pronounceable names over comments.\r\n\r\n~~~php\r\n// add body classes\r\nadd_filter('body_class', [$this, 'bodyClassFilter']);\r\n~~~\r\n\r\n~~~php\r\n// class FooBar\r\nclass FooBar {\r\n    /**\r\n     * Current object ID\r\n     *\r\n     * @var int|string|null\r\n     */\r\n    public $IO;\r\n}\r\n~~~\r\n\r\n~~~php\r\nadd_filter('body_class', [$this, 'addBodyClasses']);\r\n~~~\r\n\r\n~~~php\r\nclass FooBar {\r\n    public int|string|null $IO;\r\n}\r\n~~~\r\n\r\n## 10.1 TODO and FIXME\r\n\r\nPrefixing your comments with FIXME or TODO helps other developers quickly understand if you’re pointing out a problem that needs to be revisited, or if you’re suggesting a solution to the problem that needs to be implemented. These are different than regular comments because they are actionable. The actions are FIXME: -- need to figure this out or TODO: -- need to implement.\r\n\r\nUse // FIXME: to annotate problems.\r\n~~~php\r\n// FIXME: shouldn’t use a global here\r\nglobal $total;\r\n\r\n$total = 0;\r\n~~~\r\n\r\nUse // TODO: to annotate solutions to problems.\r\n~~~php\r\nclass Calculator {\r\n    private $total;\r\n\r\n    public function __constructor() {\r\n        // TODO: total should be configurable by an options param\r\n        $this-\u003etotal = 0;\r\n    }\r\n}\r\n~~~\r\n\r\n## Resources\r\n- [PHP Manual][PHP]\r\n- [PHP Standards Recommendations][PSR]\r\n- [PHP Clean Code][clean-code], please do read this\r\n\r\n[PSR]: https://www.php-fig.org/psr/\r\n[PHP]: https://www.php.net/manual/en/\r\n[PSR-12]: https://www.php-fig.org/psr/psr-12/\r\n[PSR-1]: https://www.php-fig.org/psr/psr-1/\r\n[PSR-2]: https://www.php-fig.org/psr/psr-2/\r\n[keywords]: http://php.net/manual/en/reserved.keywords.php\r\n[types]: http://php.net/manual/en/reserved.other-reserved-words.php\r\n[arithmetic]: http://php.net/manual/en/language.operators.arithmetic.php\r\n[assignment]: http://php.net/manual/en/language.operators.assignment.php\r\n[comparison]: http://php.net/manual/en/language.operators.comparison.php\r\n[bitwise]: http://php.net/manual/en/language.operators.bitwise.php\r\n[logical]: http://php.net/manual/en/language.operators.logical.php\r\n[string]: http://php.net/manual/en/language.operators.string.php\r\n[type]: http://php.net/manual/en/language.operators.type.php\r\n[clean-code]: https://github.com/jupeter/clean-code-php\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgtap-dev%2Fphp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgtap-dev%2Fphp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgtap-dev%2Fphp/lists"}