2 Coding Standards
There is no translation yet for this section. Please help out and translate this.. More information about translations
Cake Developers will use the following coding standards.
It is recommended that others developing CakeIngredients follow the same standards.
# Adding new features
There is no translation yet for this section. Please help out and translate this.. More information about translations
No new features should be added, without having their own tests - which should be passed before committing them to the repository.
# Indentation
There is no translation yet for this section. Please help out and translate this.. More information about translations
One tab will be used for indentation.
So, indentation should look like this:
<?php
// base level
// level 1
// level 2
// level 1
// base level
?> <?php// base level// level 1// level 2// level 1// base level?>
Or:
$booleanVariable = true;
$stringVariable = "moose";
if ($booleanVariable) {
echo "Boolean value is true";
if ($stringVariable == "moose") {
echo "We have encountered a moose";
}
}
$booleanVariable = true;$stringVariable = "moose";if ($booleanVariable) {echo "Boolean value is true";if ($stringVariable == "moose") {echo "We have encountered a moose";}}
# Control Structures
There is no translation yet for this section. Please help out and translate this.. More information about translations
Control structures are for example "if", "for", "foreach", "while", "switch" etc. Below, an example with "if":
<?php
if ((expr_1) || (expr_2)) {
// action_1;
} elseif (!(expr_3) && (expr_4)) {
// action_2;
} else {
// default_action;
}
?>
<?phpif ((expr_1) || (expr_2)) {// action_1;} elseif (!(expr_3) && (expr_4)) {// action_2;} else {// default_action;}?>
- In the control structures there should be 1 (one) space before the first parenthesis and 1 (one) space between the last parenthesis and the opening bracket.
- Always use curly brackets in control structures, even if they are not needed. They increase the readability of the code, and they give you fewer logical errors.
- Opening curly brackets should be placed on the same line as the control structure. Closing curly brackets should be placed on new lines, and they should have same indentation level as the control structure. The statement included in curly brackets should begin on a new line, and code contained within it should gain a new level of indentation.
<?php
// wrong - no brackets, badly placed statement
if (expr) statement;
// wrong - no brackets
if (expr)
statement;
// good
if (expr) {
statement;
}
?>
<?php// wrong - no brackets, badly placed statementif (expr) statement;// wrong - no bracketsif (expr)statement;// goodif (expr) {statement;}?>
# Ternary Operator
There is no translation yet for this section. Please help out and translate this.. More information about translations
Ternary operators are permissible when the entire ternary operation fits on one line. Longer ternaries should be split into if else statements. Ternary operators should not ever be nested. Optionally parentheses can be used around the condition check of the ternary for clarity.
//Good, simple and readable $variable = isset($options['variable']) ? $options['variable'] : true; //Nested ternaries are bad $variable = isset($options['variable']) ? isset($options['othervar']) ? true : false : false;
//Good, simple and readable$variable = isset($options['variable']) ? $options['variable'] : true;//Nested ternaries are bad$variable = isset($options['variable']) ? isset($options['othervar']) ? true : false : false;
# Function Calls
There is no translation yet for this section. Please help out and translate this.. More information about translations
Functions should be called without space between function's name and starting bracket. There should be one space between every parameter of a function call.
<?php $var = foo($bar, $bar2, $bar3); ?>
<?php$var = foo($bar, $bar2, $bar3);?>
As you can see above there should be one space on both sides of equals sign (=).
# Method definition
There is no translation yet for this section. Please help out and translate this.. More information about translations
Example of a function definition:
<?php
function someFunction($arg1, $arg2 = '') {
if (expr) {
statement;
}
return $var;
}
?> <?phpfunction someFunction($arg1, $arg2 = '') {if (expr) {statement;}return $var;}?>
Parameters with a default value, should be placed last in function definition. Try to make your functions return something, at least true or false - so it can be determined whether the function call was successful.
<?php
function connection(&$dns, $persistent = false) {
if (is_array($dns)) {
$dnsInfo = &$dns;
} else {
$dnsInfo = BD::parseDNS($dns);
}
if (!($dnsInfo) || !($dnsInfo['phpType'])) {
return $this->addError();
}
return true;
}
?> <?phpfunction connection(&$dns, $persistent = false) {if (is_array($dns)) {$dnsInfo = &$dns;} else {$dnsInfo = BD::parseDNS($dns);}if (!($dnsInfo) || !($dnsInfo['phpType'])) {return $this->addError();}return true;}?>
There are spaces on both side of the equals sign.
# Commenting code
There is no translation yet for this section. Please help out and translate this.. More information about translations
All comments should be written in English, and should in a clear way describe the commented block of code.
Comments can include the following phpDocumentor tags:
- @access
- @author
- @copyright
- @deprecated
- @example
- @ignore
- @internal
- @link
- @see
- @since
- @tutorial
- @version
- inline {@internal}}
- inline {@inheritdoc}}
- inline {@link}}
PhpDoc tags are very much like JavaDoc tags in Java. Tags are only processed if they are the first thing in a DocBlock line, for example:
<?php /** * Tag example. * @author this tag is parsed, but this @version is ignored * @version 1.0 this tag is also parsed */ ?>
<?php/*** Tag example.* @author this tag is parsed, but this @version is ignored* @version 1.0 this tag is also parsed*/?>
There are 3 inline tags ({@internal}}, {@inheritdoc}} and {@link}}).
<?php
/**
* Example of inline phpDoc tags.
*
* This function works hard with {@link foo()} to rule the world.
*/
function bar() {
}
/**
* Foo function
*/
function foo() {
}
?> <?php/*** Example of inline phpDoc tags.** This function works hard with {@link foo()} to rule the world.*/function bar() {}/*** Foo function*/function foo() {}?>
Comment blocks, with the exception of the first block in a file, should always be preceeded by a newline.
# Including files
There is no translation yet for this section. Please help out and translate this.. More information about translations
When including files with classes or libraries, use only and always the require_once function.
# PHP tags
There is no translation yet for this section. Please help out and translate this.. More information about translations
Always use long tags (<?php ?>) Instead of short tags (<? ?>).
# Naming convention
# Functions
There is no translation yet for this section. Please help out and translate this.. More information about translations
Write all functions in camelBack
<?php
function longFunctionName() {
}
?> <?phpfunction longFunctionName() {}?>
# Classes
There is no translation yet for this section. Please help out and translate this.. More information about translations
Class names should be written in CamelCase, for example:
<?php
class ExampleClass {
}
?> <?phpclass ExampleClass {}?>
# Variables
There is no translation yet for this section. Please help out and translate this.. More information about translations
Variable names should be as descriptive as possible, but also as short as possible. Normal variables should start with a lowercase letter, and should be written in camelBack? in case of multiple words. Variables containing objects should start with a capital letter, and in some way associate to the class the variable is an object of. Example:
<?php
$user = 'John';
$users = array('John', 'Hans', 'Arne');
$Dispatcher = new Dispatcher();
?> <?php$user = 'John';$users = array('John', 'Hans', 'Arne');$Dispatcher = new Dispatcher();?>
# Member visibility
There is no translation yet for this section. Please help out and translate this.. More information about translations
As we cannot use PHP5's private and protected keywords for methods or variables, we agree on following rules:
- A protected method or variable name start with a single underscore ("_"). Example:
<?php class A { var $_iAmAProtectedVariable; function _iAmAProtectedMethod() { /*...*/ } } ?><?phpclass A {var $_iAmAProtectedVariable;function _iAmAProtectedMethod() {/*...*/}}?>
- A private method or variable name start with double underscore ("__"). Example:
<?php
class A {
var $__iAmAPrivateVariable;
function __iAmAPrivateMethod() {
/*...*/
}
}
?>
<?phpclass A {var $__iAmAPrivateVariable;function __iAmAPrivateMethod() {/*...*/}}?>
# Example addresses
There is no translation yet for this section. Please help out and translate this.. More information about translations
For all example URL and mail addresses use "example.com", "example.org" and "example.net", for example:
- Email: someone@example.com
- WWW: http://www.example.com
- FTP: ftp://ftp.example.com
The example.com domain name has been reserved for this (see RFC 2606) and is recommended for use in documentation or as examples.
# Files
There is no translation yet for this section. Please help out and translate this.. More information about translations
File names should be created with lower case. If a file name consist of multiple words, they should be divided by an underscore character, for example:
long_file_name.php
# Variable types
There is no translation yet for this section. Please help out and translate this.. More information about translations
Variable types for use in DocBlocks:
| Type | Description |
|---|---|
| mixed | A variable with undefined (or multiple) type. |
| integer | Integer type variable (whole number). |
| float | Float type (point number). |
| boolean | Logical type (true or false). |
| string | String type (any value in "" or ' '). |
| array | Array type. |
| object | Object type. |
| resource | Resource type (returned by for example mysql_connect()). |
Remember that when you specify the type as mixed, you should indicate whether it is unknown, or what the possible types are.
# Constants
There is no translation yet for this section. Please help out and translate this.. More information about translations
Constants should be defined in capital letters:
<?php
define('CONSTANT', 1);
?>
<?phpdefine('CONSTANT', 1);?>
If a constant name consists of multiple words, they should be separated by an underscore character, for example:
<?php
define('LONG_NAMED_CONSTANT', 2);
?>
<?phpdefine('LONG_NAMED_CONSTANT', 2);?>


























