DokuWiki

It's better when it's simple

User Tools

Site Tools


devel:coding_style

This is an old revision of the document!


Coding Style

There are currently no really strict rules in how the code should be formatted but a few basic things should be attended to when adding code to DokuWiki.

Brackets and Indentations

You should indent your code by 4 spaces to mark logical blocks. Please do not use tabs. Much code still uses 2 spaces; you should stick to this or change the whole file to 4 spaces.

Opening brackets should start on the same line as the keyword, closing bracket should be aligned below the first letter of the starting keyword. E.g.:

if ($foo == "bar") {
    call_bar();
} elseif ($foo == "baz") {
    call_baz();
} else {
    call_other();
}

Lineendings

Lines should end with a single linefeed character (UNIX style). Please try to avoid trailing whitespace. Have a look at our page about VIM to see how to set up VIM to spot these easily.

Commenting

Each function and class should have a PHPDocumentor style comment, giving at least the function's purpose and its author. Parameter and return value descriptions are encouraged but only mandatory if they are not obvious. If you enhance an existing function just add another author line.

Example:

/**
 * Check for foo in bar
 *
 * Checks if there is a foo in bar
 *
 * @author   Joe Schmoe <joe@example.com>
 * @param    string $in your input
 * @return   bool       true if foo in bar
 *
 */
function is_foo($in){
...
}

These comments are used for the autogenerated API Docs.

PHP closing tags

You should omit PHP's closing tags (?>) in all files to avoid premature output. This may sound strange but is actually mentioned in the PHP manual:

Note: The closing tag of a PHP block at the end of a file is optional, and in some cases omitting it is helpful when using include() or require(), so unwanted whitespace will not occur at the end of files, and you will still be able to add headers to the response later. It is also handy if you use output buffering, and would not like to see added unwanted whitespace at the end of the parts generated by the included files.

Checking for Coding Style Violations

The development git checkout contains a coding standard setup for the use with PHP Code Sniffer in _cs/DokuWiki.

devel/coding_style.1351132985.txt.gz · Last modified: 2012-10-25 04:43 by Matt2

Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International
CC Attribution-Share Alike 4.0 International Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki