====== Coding conventions - standards for OSInet-originated PHP and HTML code ======
* OSInet contributions to Drupal
* Contributions to Drupal core follow the [[http://drupal.org/node/318|Drupal coding standard]].
* Contributions to non-OSInet contributed code follow the coding convention of said code, usually the same Drupal coding standard as core.
* Contributions outside core follow the here-defined OSInet coding style.
* Contributions to PEAR follow the [[http://pear.php.net/manual/en/standards.php|PEAR coding standards]]
* Contributions to the Zend Framework follow the [[http://framework.zend.com/manual/en/coding-standard.html|Zend Framework coding style]]
The OSInet coding style is mostly the Zend Framework style, except for its indenting rules, which follow the second variant of the [[http://en.wikipedia.org/wiki/One_True_Brace#Whitesmiths_style|Whitesmiths]] / Symbian style with spacing set to two spaces. For HTML the coding style is [[http://en.wikipedia.org/wiki/One_True_Brace#Banner_style|banner]].
Examples on this page are given in the context of a module (Drupal) or class called G2.
All code must work under error_reporting(E_ALL | E_STRICT)
====== Indenting ======
===== Function Declarations =====
Category |
OSInet |
Drupal |
PEAR |
ZF |
Documentation format |
PHPDocumentor |
Doxygen |
? |
PHPDocumentor |
File header |
like ZF, but
- use CeCill 2.0 license if possible
- @package and @subpackage tags to group files
- see tag to cross-refer elements
- @version tag can use other VCS' equivalents to CVS' $Id$
|
// $Id$
use GPL 2.0 license for core and preferably GPL 2.0 for contrib too |
? |
/**
* Short description for file
*
* Long description for file (if any)...
*
* LICENSE: Some license information
*
* @copyright 2005 Zend Technologies
* @license http://www.zend.com/license/3_0.txt PHP License 3.0
* @version CVS: $Id:$
* @link http://dev.zend.com/package/PackageName
* @since File available since Release 1.2.0
*/ |
File header for class file |
as ZF, except @version, use $Id$ |
N.A.: no classes |
? |
/**
* Short description for class
*
* Long description for class (if any)...
*
* @copyright 2005 Zend Technologies
* @license http://www.zend.com/license/3_0.txt PHP License 3.0
* @version Release: @package_version@
* @link http://dev.zend.com/package/PackageName
* @since Class available since Release 1.2.0
* @deprecated Class deprecated in Release 2.0.0
*/ |
Function header |
as ZF |
unspecified |
? |
- Textual description
- all @param
- @return
- @throws if the function throws an exception
- @access now obsoleted by visibility attributes
|
====== Using CVS ======
**Identical**
Additional rule: modules must define a constant containing the version information to be displayed on the module settings page. See the rules for constants below.
====== Example URLs ======
**Identical**
====== Naming Conventions ======
===== General =====
Element |
OSInet |
Drupal |
PEAR |
ZF |
Functions |
as ZF |
g2_get_element_by_id() |
? |
getElementById() |
Function scope |
as ZF |
No classes, so use "<module_name>_" as a namespace to avoid name collision |
? |
Floating functions are discouraged. Use a static class. |
Accessors |
as ZF |
N.A.: no classes |
? |
getSomeField setSomeField |
Design patterns |
as ZF |
unspecified |
? |
Include name of pattern in the name of the method, where practical |
Constants |
class G2
{
const SOME_CONSTANT = 'foo';
} |
define('G2_SOME_CONSTANT', 'foo'); |
? |
class G2 {
const SOME_CONSTANT = 'foo';
} |
Class fields |
private _someFieldOne;
protected _someFieldTwo;
public someFieldThree; |
PHP4 pseudo-private field: _some_field |
? |
private _someFieldOne;
protected _someFieldTwo;
public someFieldThree; |
Global variables |
no specific rule |
_g2_some_variable |
? |
no specific rule |
Code files |
- Drupal components: as Drupal
- elsewhere: as ZF
|
- Module: .module
- Module install: .install
- Themes:
- Plain PHP: .theme
- PHPTemplate: .tpl.php
- Includes: .inc
|
? |
- all files containing PHP code must bear the .php extension
- the file containing class Foo must be named Foo.php
|
Documentation |
- Standard files: LICENSE.txt
- Specific files:
- CamelCaps.txt
- Allowed text formats:
- txt (UTF8)
- PDF
- OpenDocument
- Allowed image formats:
- SVG, SVGZ
- PNG, including Fireworks
- JPG
|
LICENSE.txt |
? |
? |
===== Variables =====
Variables naming following the rules for functions and methods. Exceptions:
* existing libraries or APIs are not renamed. Facade APIs may be used until existing libraries match the new format
* the standard variable $ret should be used for all return values. Always use it to return a value, instead of directly returning the latest instruction result. This helps with a typical debugger configuration: breakpoint on return and $ret as a watched value.
* some Hungarian notation is used:
* the name of "unsafe" strings is prefixed by "us_"
* http://www.joelonsoftware.com/articles/Wrong.html
* naming resulting from external objects applies the rule by pieces. Example:
* GTK widgets are named