Difference between revisions of "Coding Standards"

From GLMWiki
Jump to: navigation, search
(Space Usage)
(Space Usage)
Line 94: Line 94:
  
 
<pre>
 
<pre>
function my_function( $param1 = 'foo', $param2 = 'bar' ) { ...
+
function my_function( $param1 = 'foo', $param2 = 'bar' )  
 +
{ ...
 
   
 
   
function my_other_function() { ...
+
function my_other_function()  
 +
{ ...
 
</pre>
 
</pre>
 
When calling a function, do it like so:
 
When calling a function, do it like so:
Line 136: Line 138:
  
 
<pre>
 
<pre>
function sum( $a, $b ): float {
+
function sum( $a, $b ): float  
 +
{
 
     return $a + $b;
 
     return $a + $b;
 
}
 
}

Revision as of 08:39, 11 October 2019

PHP Coding Standards

Word Press coding standards (except for tabs) no tabs. only spaces (4)

https://make.wordpress.org/core/handbook/best-practices/coding-standards/php/

Stardards for PHP Coding

Indentation

Tabs or Spaces

Convert tabs to spaces 4 spaces per tab.

Brace Style

Braces shall be used for all blocks in the style shown here:

if ( condition ) {
    action1();
    action2();
} elseif ( condition2 && condition3 ) {
    action3();
    action4();
} else {
    defaultaction();
}


Braces should always be used, even when they are not required:

if ( condition ) {
    action0();
}
 
if ( condition ) {
    action1();
} elseif ( condition2 ) {
    action2a();
    action2b();
}
 
foreach ( $items as $item ) {
    process_item( $item );
}

Multiline Function Calls

When splitting a function call over multiple lines, each parameter must be on a seperate line. Single line inline comments can take up their own line.

Each parameter must take up no more than a single line. Multi-line parameter values must be assigned to a variable and then that variable should be passed to the function call.

$bar = array(
    'use_this' => true,
    'meta_key' => 'field_name',
);
$baz = sprintf(
    /* translators: %s: Friend's name */
    esc_html__( 'Hello, %s!', 'yourtextdomain' ),
    $friend_name
);
 
$a = foo(
    $bar,
    $baz,
    /* translators: %s: cat */
    sprintf( __( 'The best pet is a %s.' ), 'cat' )
);

Remove Trailing Spaces

Remove trailing whitespace at the end of each line of code. Omitting the closing PHP tag at the end of a file is preferred. If you use the tag, make sure you remove trailing whitespace.

Space Usage

Always put spaces after commas, and on both sides of logical, comparison, string and assignment operators.

x == 23
foo && bar
! foo
array( 1, 2, 3 )
$baz . '-5'
$term .= 'X'

Put spaces on both sides of the opening and closing parentheses of if, elseif, foreach, for, and switch blocks.

foreach ( $foo as $bar ) { ...

When defining a function, do it like so:

function my_function( $param1 = 'foo', $param2 = 'bar' ) 
{ ...
 
function my_other_function() 
{ ...

When calling a function, do it like so:

my_function( $param1, func_param( $param2 ) );
my_other_function();

Type casts must be lowercase. Always prefer the short form of type casts, (int) instead of (integer) and (bool) rather than (boolean). For float casts use (float).:

foreach ( (array) $foo as $bar ) { ...
 
$foo = (bool) $bar;

When referring to array items, never include a space around the index, even if it is a variable, for example:

$x = $foo['bar']; // correct
$x = $foo[ 'bar' ]; // incorrect
 
$x = $foo[0]; // correct
$x = $foo[ 0 ]; // incorrect
 
$x = $foo[$bar]; // correct
$x = $foo[ $bar ]; // incorrect

In a switch block, there must be no space before the colon for a case statement.

switch ( $foo ) {
case 'bar': // correct
case 'ba' : // incorrect
}

Similarly, there should be no space before the colon on return type declarations.

function sum( $a, $b ): float 
{
    return $a + $b;
}

Unless otherwise specified, parentheses should have spaces inside of them.

if ( $foo && ( $bar || $baz ) ) { ...
 
my_function( ( $x - 1 ) * 5, $y );

For multiline arrays align the = sign and use only one space after the longest key.

$ui = [
    'sectionColor' => '#ffffff',
    'nowrap'       => true
]

Naming Conventions

Class Names

CamelCase, first letter capitalized, Contains reference to the plugin/add-on or use

class GlmDataFileLibrary extends GlmDataAbstract

class GlmDataRegistrationsRegNotification extends GlmDataAbstract


Exception: Class names for GLM Associate models use {prefix}_{menu}_{action} Where {prefix} is GlmMembers

class GlmMembersFromt_members_detail extends GlmDataMemberInfo

class GlmMembersAdmin_members_index extends GlmDataMembers

Class Method Names

CamelCase, first letter lower case

public function entryPostProcessing( $r, $a )

public function modelAction( $actionData = false )

Variables

For all global, Class, and Method variables

CamelCase, first letter lower case, upper case only for first letter of each word


$alwaysEnqueue

$memberProfileId   <--- correct
$memberProfileID   <--- wrong

Constants

All capitals with words separated by underscores

Reference plugin where constant is defined


GLM_MEMBERS_PLUGIN_UNDERSCORED_NAME

GLM_MEMBERS_REGISTRATIONS_PLUGIN_ADMIN_URL

Deprecated

each() in php 7.2

CSS Coding Standards

HTML Coding Standards

Old Standards