@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.)
hq.recaptime.dev/wiki/Phorge
phorge
phabricator
1@title PHP Coding Standards
2@group standards
3
4This document describes PHP coding standards for Phorge and related
5projects (like Arcanist).
6
7= Overview =
8
9This document outlines technical and style guidelines which are followed in
10Phorge and Arcanist. Contributors should also follow these guidelines.
11Many of these guidelines are automatically enforced by lint.
12
13These guidelines are essentially identical to the Facebook guidelines, since I
14basically copy-pasted them. If you are already familiar with the Facebook
15guidelines, you probably don't need to read this super thoroughly.
16
17
18= Spaces, Linebreaks and Indentation =
19
20 - Use two spaces for indentation. Don't use tab literal characters.
21 - Use Unix linebreaks ("\n"), not MSDOS ("\r\n") or OS9 ("\r").
22 - Put a space after control keywords like `if` and `for`.
23 - Put a space after commas in argument lists.
24 - Put a space around operators like `=`, `<`, etc.
25 - Don't put spaces after function names.
26 - Parentheses should hug their contents.
27 - Generally, prefer to wrap code at 80 columns.
28
29= Case and Capitalization =
30
31 - Name variables and functions using `lowercase_with_underscores`.
32 - Name classes using `UpperCamelCase`.
33 - Name methods and properties using `lowerCamelCase`.
34 - Use uppercase for common acronyms like ID and HTML.
35 - Name constants using `UPPERCASE`.
36 - Write `true`, `false` and `null` in lowercase.
37
38= Comments =
39
40 - Do not use "#" (shell-style) comments.
41 - Prefer "//" comments inside function and method bodies.
42
43= PHP Language Style =
44
45 - Use "<?php", not the "<?" short form. Omit the closing "?>" tag.
46 - Prefer casts like `(string)` to casting functions like `strval()`.
47 - Prefer type checks like `$v === null` to type functions like
48 `is_null()`.
49 - Avoid all crazy alternate forms of language constructs like "endwhile"
50 and "<>".
51 - Always put braces around conditional and loop blocks.
52
53= PHP Language Features =
54
55 - Use PHP as a programming language, not a templating language.
56 - Avoid globals.
57 - Avoid extract().
58 - Avoid eval().
59 - Avoid variable variables.
60 - Prefer classes over functions.
61 - Prefer class constants over defines.
62 - Avoid naked class properties; instead, define accessors.
63 - Use exceptions for error conditions.
64 - Use type hints, use `assert_instances_of()` for arrays holding objects.
65
66= Examples =
67
68**if/else:**
69
70 lang=php
71 if ($some_variable > 3) {
72 // ...
73 } else if ($some_variable === null) {
74 // ...
75 } else {
76 // ...
77 }
78
79You should always put braces around the body of an if clause, even if it is only
80one line long. Note spaces around operators and after control statements. Do not
81use the "endif" construct, and write "else if" as two words.
82
83**for:**
84
85 lang=php
86 for ($ii = 0; $ii < 10; $ii++) {
87 // ...
88 }
89
90Prefer $ii, $jj, $kk, etc., as iterators, since they're easier to pick out
91visually and react better to "Find Next..." in editors.
92
93**foreach:**
94
95 lang=php
96 foreach ($map as $key => $value) {
97 // ...
98 }
99
100**switch:**
101
102 lang=php
103 switch ($value) {
104 case 1:
105 // ...
106 break;
107 case 2:
108 if ($flag) {
109 // ...
110 break;
111 }
112 break;
113 default:
114 // ...
115 break;
116 }
117
118`break` statements should be indented to block level.
119
120**array literals:**
121
122 lang=php
123 $junk = array(
124 'nuts',
125 'bolts',
126 'refuse',
127 );
128
129Use a trailing comma and put the closing parenthesis on a separate line so that
130diffs which add elements to the array affect only one line.
131
132**operators:**
133
134 lang=php
135 $a + $b; // Put spaces around operators.
136 $omg.$lol; // Exception: no spaces around string concatenation.
137 $arr[] = $element; // Couple [] with the array when appending.
138 $obj = new Thing(); // Always use parens.
139
140**function/method calls:**
141
142 lang=php
143 // One line
144 eject($cargo);
145
146 // Multiline
147 AbstractFireFactoryFactoryEngine::promulgateConflagrationInstance(
148 $fuel,
149 $ignition_source);
150
151**function/method definitions:**
152
153 lang=php
154 function example_function($base_value, $additional_value) {
155 return $base_value + $additional_value;
156 }
157
158 class C {
159 public static function promulgateConflagrationInstance(
160 IFuel $fuel,
161 IgnitionSource $source) {
162 // ...
163 }
164 }
165
166**class:**
167
168 lang=php
169 class Dog extends Animal {
170
171 const CIRCLES_REQUIRED_TO_LIE_DOWN = 3;
172
173 private $favoriteFood = 'dirt';
174
175 public function getFavoriteFood() {
176 return $this->favoriteFood;
177 }
178 }
179
180# Extra Readings
181
182* @{article:PHP Pitfalls}