{"id":18975094,"url":"https://github.com/bufferapp/buffer-php-style-guide","last_synced_at":"2025-08-22T15:06:40.775Z","repository":{"id":17773937,"uuid":"20640142","full_name":"bufferapp/buffer-php-style-guide","owner":"bufferapp","description":"Buffer's PHP style guide","archived":false,"fork":false,"pushed_at":"2016-09-27T08:53:37.000Z","size":9,"stargazers_count":19,"open_issues_count":0,"forks_count":3,"subscribers_count":39,"default_branch":"master","last_synced_at":"2025-06-02T13:23:10.068Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","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/bufferapp.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":"2014-06-09T08:53:27.000Z","updated_at":"2020-10-11T17:54:23.000Z","dependencies_parsed_at":"2022-07-26T19:17:14.203Z","dependency_job_id":null,"html_url":"https://github.com/bufferapp/buffer-php-style-guide","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/bufferapp/buffer-php-style-guide","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bufferapp%2Fbuffer-php-style-guide","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bufferapp%2Fbuffer-php-style-guide/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bufferapp%2Fbuffer-php-style-guide/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bufferapp%2Fbuffer-php-style-guide/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bufferapp","download_url":"https://codeload.github.com/bufferapp/buffer-php-style-guide/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bufferapp%2Fbuffer-php-style-guide/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":271657603,"owners_count":24797935,"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","status":"online","status_checked_at":"2025-08-22T02:00:08.480Z","response_time":65,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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-08T15:17:17.995Z","updated_at":"2025-08-22T15:06:40.741Z","avatar_url":"https://github.com/bufferapp.png","language":null,"readme":"Buffer PHP Coding Style Guide\n==================\nOur PHP coding guide very much derived from [PSR-2](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md). We follow PSR-2 coding standard.\n\nThe goal of this guide is to make our code more consistent and improve code readability.\n\n1. Overview and Important Points\n-----------\n- Code MUST use 4 spaces for indenting, not tabs.\n\n- Opening braces for classes/methods MUST go to the next line, and closing braces MUST\n  go on the next line after the body.\n\n- New controllers and models are always CapFirst. The first letter is always capital E.g. `InteractionsController.php`.\n\n- Camel case for classes and methods, with class names always capitalized. E.g. `class SampleClassName` and `public function getTeamMembers()`\n\n- Camel case for all instance, global and local variables, properties defined within the class or file.\n\nI think keeping all the foundational rules above is a great start to keep our code consistent. **Those were the points we all should remember.**\n\n### 1.1. Example\n\nHere is an example snippet that illustrates the main points above.\n\n```php\n\u003c?php\nnamespace Vendor\\Package;\n\nuse FooInterface;\nuse BarClass as Bar;\nuse OtherVendor\\OtherPackage\\BazClass;\n\nclass Foo extends Bar implements FooInterface\n{\n    public function sampleFunction($a, $b = null)\n    {\n        if ($a === $b) {\n            bar();\n        } elseif ($a \u003e $b) {\n            $foo-\u003ebar($arg1);\n        } else {\n            BazClass::bar($arg2, $arg3);\n        }\n    }\n\n    final public static function bar()\n    {\n        // method body\n    }\n}\n```\n## Goal\n\nTo establish more consistency and readability across the Buffer team and our\nopen source projects.\n\n## Ideals\n\n\u003e **Have a bias toward clarity** - from [The Buffer Values](http://www.slideshare.net/Bufferapp/buffer-culture-04)\n\n  - We should always aim to write code that is clear and readable.\n  - Use whitespace. Add comments liberally where needed, but strive to write code that's clear and self documenting\n  - Always try to write code that clearly demonstrates and communicates it's intent.\n  - If you write a very long comment for your code it means that something is possible off with the code. Long comments may add noise sometimes.\n\n\n## Indenting and Line Length\n- Use an indent of one tabs character. This helps to avoid problems with diffs, patches, git history and annotations.\n- Try to keep line within 75-85 characters long for better code readability.\n- PSR-2 requires that the line be less than 120 characters.\n\n## Control Structures\nThese include ``if``, ``for``, ``while``, ``switch``, etc.\nHere is an example if statement, since it is the most complicated of them:\n\n```php\n\u003c?php\nif (condition1 || (condition2) {\n    action1;\n} elseif (condition3 \u0026\u0026 condition4) {\n    action2;\n} else {\n    defaultaction;\n}\n?\u003e\n```\nControl statements should have one space between the control keyword and opening parenthesis, to distinguish them from function calls. Curly braces should be used on new lines.\nYou are strongly encouraged to always use curly braces even in situations where they are technically optional. Having them increases readability and decreases the likelihood of logic errors being introduced when new lines are added.\nFor switch statements:\n\n```php\n\u003c?php\nswitch (condition) {\n  case 1:\n      action1;\n      break;\n\n  case 2:\n      action2;\n      break;\n\n  default:\n      defaultaction;\n      break;\n}\n?\u003e\n```\n\nSplit long if statements onto several lines.\nLong ``if`` statements may be split onto several lines when the character/line limit\nwould be exceeded. The conditions have to be positioned onto the following line,\nand indented. The logical operators (``\u0026\u0026``, ``||``, etc.) should be at the\nbeginning of the line to make it easier to comment (and exclude) the condition.\nThe closing parenthesis gets its own line at the end of the conditions.\n\n\n\n##Ternary operators\nThe same rule as for if clauses also applies for the ternary operator:\n It may be split onto several lines, keeping the question mark and the colon at\n the front.\n \n```php\n\u003c?php\n\n$a = $condition1 \u0026\u0026 $condition2 ? $foo : $bar;\n\n$b = $condition3 \u0026\u0026 $condition4\n    ? $foo_man_this_is_too_long_what_should_i_do\n    : $bar;\n?\u003e\n```\n\n## Function Calls\nFunctions should be called with no spaces between the function name, the opening\nparenthesis, and the first parameter; spaces between commas and each parameter,\nand no space between the last parameter, the closing parenthesis, and the semicolon.\nHere's an example:\n\n```php\n\u003c?php\n\t$apples = getApplesByColor($tree, $color);\n?\u003e\n```\nAs displayed above, there should be one space on either side of an equals sign used to assign the return value of a function to a variable.\n\nIf the function has many arguments and it violates 120 chars rule, you can do something like this and move the arguments to the next line.\n\n```php\n\u003c?php\n\n$this-\u003esomeObject-\u003ecallThisFunctionWithManyArgs($parameterOne,\n\t$parameterTwo, $aVeryLongParameterThree);\n?\u003e\n```\n\nSeveral parameters per line are allowed. Parameters need to be indented 4 spaces compared to the level of the function call. The opening and closing parentheses are to be put at the end of the function call line.\n\n\n\n##Split long assigments onto several lines\nAssigments may be split onto several lines when the character/line limit would be exceeded. The equal sign has to be positioned onto the following line, and indented by 4 spaces.\n\n```php\n\u003c?php\n$GLOBALS['TSFE']-\u003eadditionalHeaderData[$this-\u003estrApplicationName]\n    = $this-\u003exajax-\u003egetJavascript(t3lib_extMgm::siteRelPath('nr_xajax'));\n?\u003e\n```\n\n\n\n##Class Definitions\nClass declarations have their opening brace on a new line:\n\n```php\n\u003c?php\nclass FooBar {\n\n    //... code goes here\n\n}\n?\u003e\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbufferapp%2Fbuffer-php-style-guide","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbufferapp%2Fbuffer-php-style-guide","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbufferapp%2Fbuffer-php-style-guide/lists"}