Category as Ecommerce: PHP Coding Standard

PHP Standard code  

Code Formatting and Layout  

General considerations 

  • Follow the PSR-2 (PHP standard Recommendation) standard for code formatting. 
  • Almost every PHP file in Flow contains exactly one class and does not output anything if it is called directly.  
  • File should start with a <?php tag and end with ?> tag. 
  • Files should contain a declare(strict_types=1); statement. 
  • All file must contain a header stating namespace and licensing information 
  • Declare your namespace. 
  • The copyright header itself must not start with /**, as this may confuse documentation generators!  
The flow for standard file header: 
<?php 
declare(strict_types=1); 
namespace YourCompany\Package\Something\New; 
/* 
* This file is part of the YourCompany.Package package. 
* 
* (c) YourCompany 
* 
* This package is Open Source Software. For the full copyright and license information, 
*  please view the LICENSE file which was distributed with this source code. 
*/ 
..... 
  • Code lines are of arbitrary length, no strict limitations to 80 characters or something similar. Can break the lines for better readability if you think it makes sense. 
  • Lines end with a newline a.k.achr(10)- UNIX style 
  • Files must be encoded in UTF-8 without byte order mark (BOM) 
  • Use the correct license and mention the correct package in the header. 

Indentation and line formatting  

Correct use of indentation: 
/** 
* Returns the name of the currently set context. 
* 
* @return string Name of the current context 
*/ 
public function getContextName(): string 
{ 
return $this->contextName; 
}

Strings: 

In general, we use single quotes to enclose literal strings: 

$neos = 'A great project from a great team'; 
  • If you’d like to insert values from variables, concatenate strings.  
  • A space must be inserted before and after the dot for better readability: 
$message = 'Hey ' . $name . ', you look ' . $appearance . ' today!'; 
  • You may break a string into multiple lines if you use the dot operator.  
  • You’ll have to indent each following line to mark them as part of the value assignment: 
$neos = 'A great ' . 
'project from ' . 
 'a great ' . 
'team'; 

  You should also consider using a PHP function such as sprintf() to concatenate strings to increase readability: 

$message = sprintf('Hey %s, you look %s today!', $name, $appearance); 

Class documentation 

/** 
* First sentence is short description. Then you can write more, just as you like 
* 
* Here may follow some detailed description about what the class is for. 
* 
* Paragraphs are separated by an empty line. 
*/ 
class SomeClass 
{ 
 ... 
} 
Documenting variables, constants, includes 
/** 
* A short description, very much recommended 
* 
* @var string 
*/ 
protected $title = 'Untitled'; 
protected function someMethod(array $products) { 
/** @var $product \Acme\SomePackage\Domain\Model\Product */ 
foreach ($products as $product) { 
$product->getTitle(); 
} 
}

Method documentation 

/** 
* A description for this method 
* 
* Paragraphs are separated by an empty line. 
* 
* @param Post $post Some description for the $post parameter 
* @param string $someString Some description for the $someString parameter 
*/ 
public function addStringToPost(Post $post, string $someString): void 
{ 
... 
} 
Testcase documentation 
/** 
* @test 
 */ 
public function fooReturnsBarForQuux(): void 
{ 
... 
} 

 JavaScript Coding 

Class Doc Comments 

  • Class Doc Comments should always be in the following order. 
  • If the class has a non-empty constructor, the following doc comments need to be added as well, after a blank line: 
@constructor 

@param {<type>} <nameOfParameter> <description of parameter>for every parameter of the constructor. 

Code style: 

function() { 

        var myVariable1, myVariable2, someText; 

        // now, use myVariable1, .... 
}
  • Please do not assign values to the variables in the initialization, except empty default values. 
Do Don’t 
function() {      
   var myVariable1, myVariable2;       
  … } 
function() {        
var variable1 = ‘Hello’,                 variable2 = variable1 + ‘ World’;        
… } 
function() {    
     var myVariable1 = {}, myVariable2 = [], myVariable3;        
… } 
 

Leave a comment

Your email address will not be published. Required fields are marked *