Skip to content

Commit 12abb1f

Browse files
Girgiasiluuu1994
andauthored
[skip ci] Amend coding style (#9797)
- Drop irrelevant rule about // comments since PHP is C99 + GNU extensions - Add rule about strlen() usage for constant string length calculation - Minor stylistic changes Co-authored-by: Ilija Tovilo <[email protected]>
1 parent 1ea1b63 commit 12abb1f

File tree

1 file changed

+30
-32
lines changed

1 file changed

+30
-32
lines changed

CODING_STANDARDS.md

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ rewritten to comply with these rules.
99

1010
1. Document your code in source files and the manual. (tm)
1111

12-
2. Functions that are given pointers to resources should not free them.
12+
1. Functions that are given pointers to resources should not free them.
1313

1414
For instance, `function int mail(char *to, char *from)` should NOT free `to`
1515
and/or `from`.
@@ -26,42 +26,42 @@ rewritten to comply with these rules.
2626
* Low-level parser routines, that are tightly integrated with the token
2727
cache and the bison code for minimum memory copying overhead.
2828

29-
3. Functions that are tightly integrated with other functions within the same
29+
1. Functions that are tightly integrated with other functions within the same
3030
module, and rely on each other's non-trivial behavior, should be documented as
3131
such and declared `static`. They should be avoided if possible.
3232

33-
4. Use definitions and macros whenever possible, so that constants have
33+
1. Use definitions and macros whenever possible, so that constants have
3434
meaningful names and can be easily manipulated. Any use of a numeric
3535
constant to specify different behavior or actions should be done through
3636
a `#define`.
3737

38-
5. When writing functions that deal with strings, be sure to remember that PHP
38+
1. When writing functions that deal with strings, be sure to remember that PHP
3939
holds the length property of each string, and that it shouldn't be
4040
calculated with `strlen()`. Write your functions in such a way so that
4141
they'll take advantage of the length property, both for efficiency and in
4242
order for them to be binary-safe. Functions that change strings and obtain
4343
their new lengths while doing so, should return that new length, so it
4444
doesn't have to be recalculated with `strlen()` (e.g. `php_addslashes()`).
4545

46-
6. NEVER USE `strncat()`. If you're absolutely sure you know what you're doing,
46+
1. NEVER USE `strncat()`. If you're absolutely sure you know what you're doing,
4747
check its man page again, and only then, consider using it, and even then,
4848
try avoiding it.
4949

50-
7. Use `PHP_*` macros in the PHP source, and `ZEND_*` macros in the Zend part of
50+
1. Use `PHP_*` macros in the PHP source, and `ZEND_*` macros in the Zend part of
5151
the source. Although the `PHP_*` macros are mostly aliased to the `ZEND_*`
5252
macros it gives a better understanding on what kind of macro you're calling.
5353

54-
8. When commenting out code using a `#if` statement, do NOT use `0` only.
55-
Instead use `"<git username here>_0"`. For example, `#if FOO_0`, where `FOO`
56-
is your git user `foo`. This allows easier tracking of why code was
57-
commented out, especially in bundled libraries.
54+
1. When commenting out code using a `#if` statement, do NOT use `0` only.
55+
Instead, use `"<git username here>_0"`. For example, `#if FOO_0`,
56+
where `FOO` is your git user `foo`. This allows easier tracking of why
57+
code was commented out, especially in bundled libraries.
5858

59-
9. Do not define functions that are not available. For instance, if a library is
59+
1. Do not define functions that are not available. For instance, if a library is
6060
missing a function, do not define the PHP version of the function, and do
6161
not raise a run-time error about the function not existing. End users should
6262
use `function_exists()` to test for the existence of a function.
6363

64-
10. Prefer `emalloc()`, `efree()`, `estrdup()`, etc. to their standard C library
64+
1. Prefer `emalloc()`, `efree()`, `estrdup()`, etc. to their standard C library
6565
counterparts. These functions implement an internal "safety-net" mechanism
6666
that ensures the deallocation of any unfreed memory at the end of a request.
6767
They also provide useful allocation and overflow information while running
@@ -106,7 +106,7 @@ rewritten to comply with these rules.
106106
jf_n_s_i
107107
```
108108

109-
2. If they are part of a "parent set" of functions, that parent should be
109+
1. If they are part of a "parent set" of functions, that parent should be
110110
included in the user function name, and should be clearly related to the
111111
parent program or function family. This should be in the form of `parent_*`:
112112

@@ -128,19 +128,19 @@ rewritten to comply with these rules.
128128
delete_foo_baz
129129
```
130130

131-
3. Function names used by user functions should be prefixed with `_php_`, and
131+
1. Function names used by user functions should be prefixed with `_php_`, and
132132
followed by a word or an underscore-delimited list of words, in lowercase
133133
letters, that describes the function. If applicable, they should be declared
134134
`static`.
135135

136-
4. Variable names must be meaningful. One letter variable names must be avoided,
136+
1. Variable names must be meaningful. One letter variable names must be avoided,
137137
except for places where the variable has no real meaning or a trivial
138138
meaning (e.g. `for (i=0; i<100; i++) ...`).
139139

140-
5. Variable names should be in lowercase. Use underscores to separate between
140+
1. Variable names should be in lowercase. Use underscores to separate between
141141
words.
142142

143-
6. Method names follow the *studlyCaps* (also referred to as *bumpy case* or
143+
1. Method names follow the *studlyCaps* (also referred to as *bumpy case* or
144144
*camel caps*) naming convention, with care taken to minimize the letter
145145
count. The initial letter of the name is lowercase, and each letter that
146146
starts a new `word` is capitalized:
@@ -161,7 +161,7 @@ rewritten to comply with these rules.
161161
getI()
162162
```
163163

164-
7. Class names should be descriptive nouns in *PascalCase* and as short as
164+
1. Class names should be descriptive nouns in *PascalCase* and as short as
165165
possible. Each word in the class name should start with a capital letter,
166166
without underscore delimiters. The class name should be prefixed with the
167167
name of the "parent set" (e.g. the name of the extension) if no namespaces
@@ -219,28 +219,20 @@ rewritten to comply with these rules.
219219
static int php_session_destroy()
220220
```
221221

222-
2. Main module source file must be named `modulename.c`.
222+
1. Main module source file must be named `modulename.c`.
223223

224-
3. Header file that is used by other sources must be named `php_modulename.h`.
224+
1. Header file that is used by other sources must be named `php_modulename.h`.
225225

226226
## Syntax and indentation
227227

228-
1. Never use C++ style comments (i.e. `//` comment). Always use C-style comments
229-
instead. PHP is written in C, and is aimed at compiling under any ANSI-C
230-
compliant compiler. Even though many compilers accept C++-style comments in
231-
C code, you have to ensure that your code would compile with other compilers
232-
as well. The only exception to this rule is code that is Win32-specific,
233-
because the Win32 port is MS-Visual C++ specific, and this compiler is known
234-
to accept C++-style comments in C code.
235-
236-
2. Use K&R-style. Of course, we can't and don't want to force anybody to use a
228+
1. Use K&R-style. Of course, we can't and don't want to force anybody to use a
237229
style he or she is not used to, but, at the very least, when you write code
238230
that goes into the core of PHP or one of its standard modules, please
239231
maintain the K&R style. This applies to just about everything, starting with
240232
indentation and comment styles and up to function declaration syntax. Also
241233
see [Indentstyle](http://www.catb.org/~esr/jargon/html/I/indent-style.html).
242234

243-
3. Be generous with whitespace and braces. Keep one empty line between the
235+
1. Be generous with whitespace and braces. Keep one empty line between the
244236
variable declaration section and the statements in a block, as well as
245237
between logical statement groups in a block. Maintain at least one empty
246238
line between two functions, preferably two. Always prefer:
@@ -257,14 +249,20 @@ rewritten to comply with these rules.
257249
if(foo)bar;
258250
```
259251

260-
4. When indenting, use the tab character. A tab is expected to represent four
252+
1. When indenting, use the tab character. A tab is expected to represent four
261253
spaces. It is important to maintain consistency in indentation so that
262254
definitions, comments, and control structures line up correctly.
263255

264-
5. Preprocessor statements (`#if` and such) MUST start at column one. To indent
256+
1. Preprocessor statements (`#if` and such) MUST start at column one. To indent
265257
preprocessor directives you should put the `#` at the beginning of a line,
266258
followed by any number of spaces.
267259

260+
1. The length of constant string literals should be calculated via ``strlen()``
261+
instead of using ``sizeof()-1`` as it is clearer and any modern compiler
262+
will optimize it away. Legacy usages of the latter style exists within the
263+
codebase but should not be refactored, unless larger refactoring around that
264+
code is taking place.
265+
268266
## Testing
269267

270268
1. Extensions should be well tested using `*.phpt` tests. Read more at

0 commit comments

Comments
 (0)