Good coding standards are important in any software development project. These standards are even more important when a large, diverse and worldwide community of developers are contributing to a project.
The Joomla Coding Standards borrows heavily from the PEAR coding standard for PHP files, augmenting and diverging where it is deemed sensible to do so.
Always use the minimal doctype.
Always define which language the page is written in.
Always define the character encoding. The encoding should be defined as early as possible. Make sure your editor uses UTF-8 as character encoding Do not specify the encoding of style sheets as these assume UTF-8.
Attribute values should be quoted using double ("") quotes. Optional attributes should be omitted. Use attribute and value pairs for boolean attributes.
Use a new line for every block, list, or table element, and indent every such child element.
Joomla prefers readability over file-size savings when it comes to maintaining existing files. Plenty of whitespace is encouraged. Use whitespace to visually separate groups of related markup and to improve the readability and maintainability of your HTML. Remove trailing white spaces.
Documentation headers for PHP code in: files, classes, class properties, methods and functions, called the docblocks. The file header DocBlock consists of the following required and optional elements in the following order:
* @package Joomla.Installation
* @subpackage Controller
* @copyright Copyright (C) 2005 - 2014 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
Always use the full <?php ... ?> to delimit PHP code.
For files that contain only PHP code, the closing tag (?>) should not be included. It is not required by PHP. Leaving this out prevents trailing white space from being accidentally injected into the output that can introduce errors.
Anywhere you are unconditionally including a file, use require_once. Anywhere you are conditionally including a file (for example, factory methods), use include_once. Either of these will ensure that files are included only once. You should not enclose the filename in parentheses.
require_once JPATH_COMPONENT . '/helpers/helper.php';
Global variables should not be used. Use static class properties or constants instead of globals, following OOP and factory patterns.
For all control structures there is a space between the keyword and an opening parenthesis, then no space either after the opening parenthesis or before the closing bracket. This is done to distinguish control keywords from function names. All control structures must contain their logic within braces.
There should always be a space before and after the concatenation operator ('.').
Assignments (the => operator) in arrays may be aligned with spaces. When splitting array definitions onto several lines, the last value should also have a trailing comma.
Functions should be called with no spaces between the function name and the opening parenthesis, and no space between this and the first parameter; a space after the comma between each parameter (if they are present), and no space between the last parameter and the closing parenthesis. There should be space before and exactly one space after the equals sign.
Function definitions start on a new line with no spaces between the function name and the opening parenthesis. Additionally, the opening and closing braces are also placed on new lines. An empty line should precede lines specifying the return value.
Namespaces are formatted according to this flow. First there is the file docblock followed by the namespace the file lives in. When required, the namespace is followed by the defined check. Lastly, the imported classes using the use keyword. All namespace imports must be alphabetically ordered.
Constants should always be all-uppercase, with underscores to separate words.