Skip to content

Instantly share code, notes, and snippets.

@alwinaugustin
Last active April 5, 2021 09:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alwinaugustin/022e44109c2ec70c4f7a6bebbfef7f29 to your computer and use it in GitHub Desktop.
Save alwinaugustin/022e44109c2ec70c4f7a6bebbfef7f29 to your computer and use it in GitHub Desktop.
Welcome file.md
---
---
<h2 id="coding-style">Coding Style</h2>
<ol>
<li>
<p><strong>Basic Coding Standards</strong></p>
<ul>
<li>Code must follow all rules outlined in <a href="https://www.php-fig.org/psr/psr-1/">PSR-1</a></li>
</ul>
</li>
<li>
<p><strong>Files</strong></p>
<ul>
<li>All PHP files must use the Unix LF (linefeed) line ending only.</li>
<li>All PHP files must end with a non-blank line, terminated with a single LF.</li>
<li>The closing <code>?&gt;</code> tag must be omitted from files containing only PHP.</li>
</ul>
</li>
<li>
<p><strong>Lines</strong></p>
<ul>
<li>There must not be a hard limit on line length</li>
<li>Lines should not be longer than 80 characters. Lines longer than 80 characters should be split into multiple lines with 80 character length.</li>
<li>There should not be trailing whitespaces at the end of the lines.</li>
<li>There must not be more than one statement per line.</li>
</ul>
</li>
<li>
<p><strong>Indenting</strong></p>
<ul>
<li>Code must use an indent of 4 spaces for each indent level, and must not use tabs for indenting.</li>
</ul>
</li>
<li>
<p><strong>Keywords and Types</strong></p>
<ul>
<li>All PHP reserved keywords and types <a href="http://php.net/manual/en/reserved.keywords.php">[1]</a><a href="http://php.net/manual/en/reserved.other-reserved-words.php">[2]</a> must be in lower case.</li>
<li>Short form of type keywords must be used i.e. <code>bool</code> instead of <code>boolean</code>, <code>int</code> instead of <code>integer</code> etc.</li>
</ul>
</li>
<li>
<p><strong>Declare Statements, Namespace, and Import Statements</strong></p>
<ul>
<li>
<p>Import statements must never begin with a leading backslash as they must always be fully qualified.</p>
</li>
<li>
<p>Each of the header blocks in the PHP file like <strong>declare, use, namespace</strong> <strong>declaration and file level doc blocks</strong> should be separated by one single blank line. Also, it should follow the following order:</p>
<ol>
<li>Opening &lt;?php tag.</li>
<li>File-level docblock.</li>
<li>One or more declare statements.</li>
<li>The namespace declaration of the file.</li>
<li>One or more class-based use import statements.</li>
<li>One or more function-based use import statements.</li>
<li>One or more constant-based use import statements.</li>
<li>The remainder of the code in the file.</li>
</ol>
<p>Example :</p>
<pre class=" language-php"><code class="prism : language-php"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span>
<span class="token comment">/**
* This file contains an example of coding styles.
*/</span>
<span class="token keyword">declare</span><span class="token punctuation">(</span>strict_types<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">namespace</span> <span class="token package">Vendor<span class="token punctuation">\</span>Package</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">Vendor<span class="token punctuation">\</span>Package<span class="token punctuation">\</span></span><span class="token punctuation">{</span>ClassA <span class="token keyword">as</span> A<span class="token punctuation">,</span> ClassB<span class="token punctuation">,</span> ClassC <span class="token keyword">as</span> C<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">Vendor<span class="token punctuation">\</span>Package<span class="token punctuation">\</span>SomeNamespace<span class="token punctuation">\</span>ClassD</span> <span class="token keyword">as</span> D<span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">Vendor<span class="token punctuation">\</span>Package<span class="token punctuation">\</span>AnotherNamespace<span class="token punctuation">\</span>ClassE</span> <span class="token keyword">as</span> E<span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">function</span> Vendor\<span class="token package">Package<span class="token punctuation">\</span></span><span class="token punctuation">{</span>functionA<span class="token punctuation">,</span> functionB<span class="token punctuation">,</span> functionC<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">function</span> Another\<span class="token package">Vendor<span class="token punctuation">\</span>functionD</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">const</span> Vendor\<span class="token package">Package<span class="token punctuation">\</span></span><span class="token punctuation">{</span><span class="token constant">CONSTANT_A</span><span class="token punctuation">,</span> <span class="token constant">CONSTANT_B</span><span class="token punctuation">,</span> <span class="token constant">CONSTANT_C</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">const</span> Another\<span class="token package">Vendor<span class="token punctuation">\</span>CONSTANT_D</span><span class="token punctuation">;</span>
<span class="token comment">/**
* FooBar is an example class.
*/</span>
<span class="token keyword">class</span> <span class="token class-name">FooBar</span>
<span class="token punctuation">{</span>
<span class="token comment">// ... additional PHP code ...</span>
<span class="token punctuation">}</span>
</span></code></pre>
</li>
</ul>
</li>
<li>
<p><strong>Classes, Traits, Properties, and Methods</strong></p>
<ul>
<li>When instantiating a new class, parentheses must always be present even when there are no arguments passed to the constructor.</li>
<li>The opening brace for the class must go on its own line; the closing brace for the class must go on the next line after the body.</li>
<li>The <code>extends</code> and <code>implements</code> keywords MUST be declared on the same line as the class name.</li>
</ul>
</li>
<li>
<p><strong>Traits</strong></p>
<ul>
<li>The <code>use</code> keyword used inside the classes to implement traits must be declared on the next line after the opening brace</li>
<li>Each individual trait that is imported into a class MUST be included one-per-line and each inclusion MUST have its own <code>use</code> import statement.<br>
Example :<pre class=" language-php"><code class="prism : language-php"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span>
<span class="token keyword">namespace</span> <span class="token package">Vendor<span class="token punctuation">\</span>Package</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">Vendor<span class="token punctuation">\</span>Package<span class="token punctuation">\</span>FirstTrait</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">Vendor<span class="token punctuation">\</span>Package<span class="token punctuation">\</span>SecondTrait</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">Vendor<span class="token punctuation">\</span>Package<span class="token punctuation">\</span>ThirdTrait</span><span class="token punctuation">;</span>
<span class="token keyword">class</span> <span class="token class-name">ClassName</span>
<span class="token punctuation">{</span>
<span class="token keyword">use</span> <span class="token package">FirstTrait</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">SecondTrait</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">ThirdTrait</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</span></code></pre>
</li>
</ul>
</li>
<li>
<p><strong>Properties and Constants</strong></p>
<ul>
<li>Visibility must be declared on all properties.</li>
<li>There must not be more than one property declared per statement.</li>
<li>Property names must not be prefixed with a single underscore to indicate protected or private visibility. That is, an underscore prefix explicitly has no meaning.</li>
<li>There MUST be a space between type declaration and property name.<br>
Example:<pre class=" language-php"><code class="prism : language-php"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span>
<span class="token keyword">namespace</span> <span class="token package">Vendor<span class="token punctuation">\</span>Package</span><span class="token punctuation">;</span>
<span class="token keyword">class</span> <span class="token class-name">ClassName</span>
<span class="token punctuation">{</span>
<span class="token keyword">public</span> <span class="token variable">$foo</span> <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> int <span class="token variable">$bar</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</span></code></pre>
</li>
</ul>
</li>
<li>
<p><strong>Methods and Functions</strong></p>
<ul>
<li>
<p>Visibility must be declared on all methods.</p>
</li>
<li>
<p>Return type must be declared for all the methods.</p>
</li>
<li>
<p>Method and function names must not be declared with space after the method name. The opening brace must go on its own line, and the closing brace must go on the next line following the body. There must not be a space after the opening parenthesis, and there must not be a space before the closing parenthesis.</p>
<p>Example:</p>
<pre class=" language-php"><code class="prism : language-php"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span>
<span class="token keyword">namespace</span> <span class="token package">Vendor<span class="token punctuation">\</span>Package</span><span class="token punctuation">;</span>
<span class="token keyword">class</span> <span class="token class-name">ClassName</span>
<span class="token punctuation">{</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">fooBarBaz</span><span class="token punctuation">(</span>int <span class="token variable">$arg1</span><span class="token punctuation">,</span> <span class="token keyword">array</span> <span class="token variable">$arg2</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> mixed <span class="token variable">$arg3</span><span class="token punctuation">)</span><span class="token punctuation">:</span> string
<span class="token punctuation">{</span>
<span class="token comment">// method body</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</span></code></pre>
</li>
</ul>
</li>
<li>
<p><strong>Method and Function Arguments</strong></p>
<ul>
<li>
<p>In the argument list, there must not be a space before each comma, and there must be one space after each comma.</p>
</li>
<li>
<p>Method and function arguments with default values MUST go at the end of the argument list.</p>
</li>
<li>
<p>Corresponding type must be specified for each of the arguments.<br>
Example:</p>
<pre class=" language-php"><code class="prism : language-php"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span>
<span class="token keyword">declare</span><span class="token punctuation">(</span>strict_types<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">namespace</span> <span class="token package">Vendor<span class="token punctuation">\</span>Package</span><span class="token punctuation">;</span>
<span class="token keyword">class</span> <span class="token class-name">ReturnTypeVariations</span>
<span class="token punctuation">{</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">functionName</span><span class="token punctuation">(</span>int <span class="token variable">$arg1</span><span class="token punctuation">,</span> int <span class="token variable">$arg2</span><span class="token punctuation">)</span><span class="token punctuation">:</span> string
<span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token string">'foo'</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">anotherFunction</span><span class="token punctuation">(</span>
string <span class="token variable">$foo</span><span class="token punctuation">,</span>
string <span class="token variable">$bar</span><span class="token punctuation">,</span>
int <span class="token variable">$baz</span>
<span class="token punctuation">)</span><span class="token punctuation">:</span> string <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token string">'foo'</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</span></code></pre>
</li>
</ul>
</li>
<li>
<p><strong>Abstract, Final, and Static</strong></p>
<ul>
<li>When present, the <code>abstract</code> and <code>final</code> declarations must precede the visibility declaration.</li>
<li>When present, the <code>static</code> declaration must come after the visibility declaration.</li>
</ul>
</li>
<li>
<p><strong>Control Structures</strong></p>
<ul>
<li>The general style rules for control structures are as follows:
<ul>
<li>There must be one space after the control structure keyword</li>
<li>There must not be a space after the opening parenthesis</li>
<li>There must not be a space before the closing parenthesis</li>
<li>There must be one space between the closing parenthesis and the opening brace</li>
<li>The structure body must be indented once</li>
<li>The body must be on the next line after the opening brace</li>
<li>The closing brace be on the next line after the body</li>
</ul>
</li>
<li>The body of each structure must be enclosed by braces.</li>
<li><strong>if, elseif, else</strong>
<ul>
<li>The keyword <code>elseif</code> should be used instead of <code>else if</code> so that all control keywords look like single words.</li>
<li>Whenever possible, use early returns</li>
<li>Expressions in parentheses MAY be split across multiple lines, where each subsequent line is indented at least once. When doing so, the first condition MUST be on the next line. The closing parenthesis and opening brace MUST be placed together on their own line with one space between them. Boolean operators between conditions MUST always be at the beginning or at the end of the line, not a mix of both.<br>
Example:<pre class=" language-php"><code class="prism : language-php"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>
<span class="token variable">$expr1</span>
<span class="token operator">&amp;&amp;</span> <span class="token variable">$expr2</span>
<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">// if body</span>
<span class="token punctuation">}</span> <span class="token keyword">elseif</span> <span class="token punctuation">(</span>
<span class="token variable">$expr3</span>
<span class="token operator">&amp;&amp;</span> <span class="token variable">$expr4</span>
<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">// elseif body</span>
<span class="token punctuation">}</span>
</span></code></pre>
</li>
</ul>
</li>
<li><strong>switch, case</strong>
<ul>
<li>The <code>case</code> statement MUST be indented once from <code>switch</code>, and the <code>break</code> keyword (or other terminating keywords) MUST be indented at the same level as the <code>case</code> body</li>
<li>There MUST be a comment such as <code>// no break</code> when fall-through is intentional in a non-empty <code>case</code> body.</li>
</ul>
</li>
<li><strong>Loops (while, do-while, for, foreach)</strong>
<ul>
<li>Expressions in parentheses MAY be split across multiple lines, where each subsequent line is indented at least once. When doing so, the first condition MUST be on the next line. The closing parenthesis and opening brace MUST be placed together on their own line with one space between them. Boolean operators between conditions MUST always be at the beginning or at the end of the line, not a mix of both.<br>
Example:<pre class=" language-php"><code class="prism : language-php"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span>
<span class="token keyword">for</span> <span class="token punctuation">(</span>
<span class="token variable">$i</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token variable">$i</span> <span class="token operator">&lt;</span> <span class="token number">10</span><span class="token punctuation">;</span>
<span class="token variable">$i</span><span class="token operator">++</span>
<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">// for body</span>
<span class="token punctuation">}</span>
</span></code></pre>
<pre class=" language-php"><code class="prism : language-php"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span>
<span class="token keyword">while</span> <span class="token punctuation">(</span>
<span class="token variable">$expr1</span>
<span class="token operator">&amp;&amp;</span> <span class="token variable">$expr2</span>
<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">// structure body</span>
<span class="token punctuation">}</span>
</span></code></pre>
</li>
</ul>
</li>
<li><strong>try, catch, finally</strong>
<ul>
<li>A <code>try-catch-finally</code> block looks like the following. Note the placement of parentheses, spaces, and braces.<pre class=" language-php"><code class="prism : language-php"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span>
<span class="token keyword">try</span> <span class="token punctuation">{</span>
<span class="token comment">// try body</span>
<span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">FirstThrowableType</span> <span class="token variable">$e</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">// catch body</span>
<span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">OtherThrowableType</span> <span class="token operator">|</span> AnotherThrowableType <span class="token variable">$e</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">// catch body</span>
<span class="token punctuation">}</span> <span class="token keyword">finally</span> <span class="token punctuation">{</span>
<span class="token comment">// finally body</span>
<span class="token punctuation">}</span>
</span></code></pre>
</li>
</ul>
</li>
</ul>
</li>
<li>
<p><strong>Operators</strong></p>
<ul>
<li>When space is permitted around an operator, multiple spaces may be used for readability purposes.</li>
<li>Unary Operators
<ul>
<li>The increment/decrement operators must not have any space between the operator and operand.</li>
<li>Type casting operators must not have any space within the parentheses<br>
Example:<pre class=" language-php"><code class="prism : language-php"><span class="token variable">$i</span><span class="token operator">++</span><span class="token punctuation">;</span>
<span class="token operator">++</span><span class="token variable">$j</span><span class="token punctuation">;</span>
<span class="token variable">$intValue</span> <span class="token operator">=</span> <span class="token punctuation">(</span>int<span class="token punctuation">)</span> <span class="token variable">$input</span><span class="token punctuation">;</span>
</code></pre>
</li>
</ul>
</li>
<li>Binary operators
<ul>
<li>All binary <a href="http://php.net/manual/en/language.operators.arithmetic.php">arithmetic</a>, <a href="http://php.net/manual/en/language.operators.comparison.php">comparison</a>, <a href="http://php.net/manual/en/language.operators.assignment.php">assignment</a>, <a href="http://php.net/manual/en/language.operators.bitwise.php">bitwise</a>, <a href="http://php.net/manual/en/language.operators.logical.php">logical</a>, <a href="http://php.net/manual/en/language.operators.string.php">string</a>, and <a href="http://php.net/manual/en/language.operators.type.php">type</a> operators must be preceded and followed by at least one space:<br>
Example :<pre class=" language-php"><code class="prism : language-php"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$a</span> <span class="token operator">===</span> <span class="token variable">$b</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token variable">$foo</span> <span class="token operator">=</span> <span class="token variable">$bar</span> <span class="token operator">?</span><span class="token operator">?</span> <span class="token variable">$a</span> <span class="token operator">?</span><span class="token operator">?</span> <span class="token variable">$b</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">elseif</span> <span class="token punctuation">(</span><span class="token variable">$a</span> <span class="token operator">&gt;</span> <span class="token variable">$b</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token variable">$foo</span> <span class="token operator">=</span> <span class="token variable">$a</span> <span class="token operator">+</span> <span class="token variable">$b</span> <span class="token operator">*</span> <span class="token variable">$c</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
</li>
</ul>
</li>
<li>Ternary operators
<ul>
<li>
<p>The conditional operator, also known simply as the ternary operator, must be preceded and followed by at least one space around both the <code>?</code> and <code>:</code> characters:</p>
</li>
<li>
<p>When the middle operand of the conditional operator is omitted, the operator MUST follow the same style rules as other binary <a href="http://php.net/manual/en/language.operators.comparison.php">comparison</a> operators<br>
Example:</p>
<pre class=" language-php"><code class="prism : language-php"><span class="token variable">$variable</span> <span class="token operator">=</span> <span class="token variable">$foo</span> <span class="token operator">?</span> <span class="token string">'foo'</span> <span class="token punctuation">:</span> <span class="token string">'bar'</span><span class="token punctuation">;</span>
<span class="token variable">$variable</span> <span class="token operator">=</span> <span class="token variable">$foo</span> <span class="token operator">?</span><span class="token punctuation">:</span> <span class="token string">'bar'</span><span class="token punctuation">;</span>
</code></pre>
</li>
</ul>
</li>
</ul>
</li>
<li>
<p><strong>Closures</strong></p>
<ul>
<li>Closures must be declared with a space after the <code>function</code> keyword, and a space before and after the <code>use</code> keyword.</li>
<li>Closure arguments with default values must go at the end of the argument list.</li>
<li>If a return type is present, it must follow the same rules as with normal functions and methods; if the <code>use</code> keyword is present, the colon MUST follow the <code>use</code> list closing parentheses with no spaces between the two characters.<br>
Example:<pre class=" language-php"><code class="prism : language-php"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span>
<span class="token variable">$closureWithArgs</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$arg1</span><span class="token punctuation">,</span> <span class="token variable">$arg2</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">// body</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token variable">$closureWithArgsAndVars</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$arg1</span><span class="token punctuation">,</span> <span class="token variable">$arg2</span><span class="token punctuation">)</span> <span class="token keyword">use</span> <span class="token punctuation">(</span><span class="token variable">$var1</span><span class="token punctuation">,</span> <span class="token variable">$var2</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">// body</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token variable">$closureWithArgsVarsAndReturn</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$arg1</span><span class="token punctuation">,</span> <span class="token variable">$arg2</span><span class="token punctuation">)</span> <span class="token keyword">use</span> <span class="token punctuation">(</span><span class="token variable">$var1</span><span class="token punctuation">,</span> <span class="token variable">$var2</span><span class="token punctuation">)</span><span class="token punctuation">:</span> bool <span class="token punctuation">{</span>
<span class="token comment">// body</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</span></code></pre>
</li>
<li>The formatting rules also apply when the closure is used directly in a function or method call as an argument.</li>
</ul>
</li>
<li>
<p><strong>Anonymous Classes</strong></p>
<ul>
<li>Anonymous Classes must follow the same guidelines and principles as closures in the above section.</li>
<li>The opening brace MAY be on the same line as the <code>class</code> keyword so long as the list of <code>implements</code> interfaces does not wrap. If the list of interfaces wraps, the brace MUST be placed on the line immediately following the last interface.<br>
Example<pre class=" language-php"><code class="prism : language-php"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span>
<span class="token comment">// Brace on the same line</span>
<span class="token variable">$instance</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">class</span> <span class="token keyword">extends</span> <span class="token class-name"><span class="token punctuation">\</span>Foo</span> <span class="token keyword">implements</span> <span class="token class-name"><span class="token punctuation">\</span>HandleableInterface</span> <span class="token punctuation">{</span>
<span class="token comment">// Class content</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token comment">// Brace on the next line</span>
<span class="token variable">$instance</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">class</span> <span class="token keyword">extends</span> <span class="token class-name"><span class="token punctuation">\</span>Foo</span> <span class="token keyword">implements</span>
<span class="token class-name"><span class="token punctuation">\</span>ArrayAccess</span><span class="token punctuation">,</span>
\<span class="token package">Countable</span><span class="token punctuation">,</span>
\<span class="token package">Serializable</span>
<span class="token punctuation">{</span>
<span class="token comment">// Class content</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</span></code></pre>
</li>
</ul>
</li>
</ol>
<p>In order to ensure the coding style, add <strong><em><a href="https://github.com/squizlabs/PHP_CodeSniffer">php-cs</a> and <a href="https://phpmd.org/">php-md</a></em></strong> extensions to the IDE. These extensions are available in <strong>.phar</strong> format and can be used in all the environments.</p>
<h2 id="best-practices">Best Practices</h2>
<ul>
<li>Use early return for if – else statements whenever it is possible.</li>
<li>Use <a href="https://fractal.thephpleague.com/">Fractal Transformer</a>s for the for the presentation and transformation of data.</li>
<li>Use <a href="https://carbon.nesbot.com/docs/">Carbon</a> extension for date and time related operations.</li>
<li>All the credentials should be added to the environment configuration and do not hard code any credentials to the code.</li>
<li>All the passwords should be hashed (not encrypted) and should be individually salted.</li>
<li>Use HHVM structure for large code bases, so that each module will be self-contained. In the case of Laravel, use <a href="https://nwidart.com/laravel-modules/v6/introduction">Laravel Modules</a> extension.</li>
<li>Add unit tests and incorporate them to the build pipeline.</li>
<li>Filter and sanitise all user inputs before it is used in the code.</li>
<li>Throw appropriate exceptions whenever necessary and use logging with appropriate log levels. User defined exceptions should be extended from a single Standardized Exception class.</li>
<li>Perl compatible regular expressions (PCRE, preg_ functions) should be used in preference to their POSIX counterparts.</li>
</ul>


Coding Style

  1. Basic Coding Standards

    • Code must follow all rules outlined in PSR-1
  2. Files

    • All PHP files must use the Unix LF (linefeed) line ending only.
    • All PHP files must end with a non-blank line, terminated with a single LF.
    • The closing ?> tag must be omitted from files containing only PHP.
  3. Lines

    • There must not be a hard limit on line length
    • Lines should not be longer than 80 characters. Lines longer than 80 characters should be split into multiple lines with 80 character length.
    • There should not be trailing whitespaces at the end of the lines.
    • There must not be more than one statement per line.
  4. Indenting

    • Code must use an indent of 4 spaces for each indent level, and must not use tabs for indenting.
  5. Keywords and Types

    • All PHP reserved keywords and types [1][2] must be in lower case.
    • Short form of type keywords must be used i.e. bool instead of boolean, int instead of integer etc.
  6. Declare Statements, Namespace, and Import Statements

    • Import statements must never begin with a leading backslash as they must always be fully qualified.

    • Each of the header blocks in the PHP file like declare, use, namespace declaration and file level doc blocks should be separated by one single blank line. Also, it should follow the following order:

      1. Opening <?php tag.
      2. File-level docblock.
      3. One or more declare statements.
      4. The namespace declaration of the file.
      5. One or more class-based use import statements.
      6. One or more function-based use import statements.
      7. One or more constant-based use import statements.
      8. The remainder of the code in the file.

      Example :

      <?php
      /**
      
      This file contains an example of coding styles.
      */
      
      declare(strict_types=1);
      namespace Vendor</span>Package;
      use Vendor</span>Package</span>{ClassA as A, ClassB, ClassC as C};
      use Vendor</span>Package</span>SomeNamespace</span>ClassD as D;
      use Vendor</span>Package</span>AnotherNamespace</span>ClassE as E;
      use function Vendor<span class="token package">Package</span>{functionA, functionB, functionC};
      use function Another<span class="token package">Vendor</span>functionD;
      use const Vendor<span class="token package">Package</span>{CONSTANT_A, CONSTANT_B, CONSTANT_C};
      use const Another<span class="token package">Vendor</span>CONSTANT_D;
      /**
      
      FooBar is an example class.
      */
      class FooBar
      {
      // ... additional PHP code ...
      }
      

  7. Classes, Traits, Properties, and Methods

    • When instantiating a new class, parentheses must always be present even when there are no arguments passed to the constructor.
    • The opening brace for the class must go on its own line; the closing brace for the class must go on the next line after the body.
    • The extends and implements keywords MUST be declared on the same line as the class name.
  8. Traits

    • The use keyword used inside the classes to implement traits must be declared on the next line after the opening brace
    • Each individual trait that is imported into a class MUST be included one-per-line and each inclusion MUST have its own use import statement.
      Example :
      <?php
      namespace Vendor</span>Package;
      use Vendor</span>Package</span>FirstTrait;
      use Vendor</span>Package</span>SecondTrait;
      use Vendor</span>Package</span>ThirdTrait;
      class ClassName
      {
      use FirstTrait;
      use SecondTrait;
      use ThirdTrait;
      }

  9. Properties and Constants

    • Visibility must be declared on all properties.
    • There must not be more than one property declared per statement.
    • Property names must not be prefixed with a single underscore to indicate protected or private visibility. That is, an underscore prefix explicitly has no meaning.
    • There MUST be a space between type declaration and property name.
      Example:
      <?php
      namespace Vendor</span>Package;
      class ClassName
      {
      public $foo = null;
      public static int $bar = 0;
      }

  10. Methods and Functions

    • Visibility must be declared on all methods.

    • Return type must be declared for all the methods.

    • Method and function names must not be declared with space after the method name. The opening brace must go on its own line, and the closing brace must go on the next line following the body. There must not be a space after the opening parenthesis, and there must not be a space before the closing parenthesis.

      Example:

      <?php
      namespace Vendor</span>Package;
      class ClassName
      {
      public function fooBarBaz(int $arg1, array $arg2 = [], mixed $arg3): string
      {
      // method body
      }
      }

  11. Method and Function Arguments

    • In the argument list, there must not be a space before each comma, and there must be one space after each comma.

    • Method and function arguments with default values MUST go at the end of the argument list.

    • Corresponding type must be specified for each of the arguments.
      Example:

      <?php
      declare(strict_types=1);
      namespace Vendor</span>Package;
      class ReturnTypeVariations
      {
      public function functionName(int $arg1, int $arg2): string
      {
      return 'foo';
      }
      <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">anotherFunction</span><span class="token punctuation">(</span>
          string <span class="token variable">$foo</span><span class="token punctuation">,</span>
          string <span class="token variable">$bar</span><span class="token punctuation">,</span>
          int <span class="token variable">$baz</span>
      <span class="token punctuation">)</span><span class="token punctuation">:</span> string <span class="token punctuation">{</span>
          <span class="token keyword">return</span> <span class="token string">'foo'</span><span class="token punctuation">;</span>
      <span class="token punctuation">}</span>
      
      }

  12. Abstract, Final, and Static

    • When present, the abstract and final declarations must precede the visibility declaration.
    • When present, the static declaration must come after the visibility declaration.
  13. Control Structures

    • The general style rules for control structures are as follows:
      • There must be one space after the control structure keyword
      • There must not be a space after the opening parenthesis
      • There must not be a space before the closing parenthesis
      • There must be one space between the closing parenthesis and the opening brace
      • The structure body must be indented once
      • The body must be on the next line after the opening brace
      • The closing brace be on the next line after the body
    • The body of each structure must be enclosed by braces.
    • if, elseif, else
      • The keyword elseif should be used instead of else if so that all control keywords look like single words.
      • Whenever possible, use early returns
      • Expressions in parentheses MAY be split across multiple lines, where each subsequent line is indented at least once. When doing so, the first condition MUST be on the next line. The closing parenthesis and opening brace MUST be placed together on their own line with one space between them. Boolean operators between conditions MUST always be at the beginning or at the end of the line, not a mix of both.
        Example:
        <?php
        if (
        $expr1
        && $expr2
        ) {
        // if body
        } elseif (
        $expr3
        && $expr4
        ) {
        // elseif body
        }

    • switch, case
      • The case statement MUST be indented once from switch, and the break keyword (or other terminating keywords) MUST be indented at the same level as the case body
      • There MUST be a comment such as // no break when fall-through is intentional in a non-empty case body.
    • Loops (while, do-while, for, foreach)
      • Expressions in parentheses MAY be split across multiple lines, where each subsequent line is indented at least once. When doing so, the first condition MUST be on the next line. The closing parenthesis and opening brace MUST be placed together on their own line with one space between them. Boolean operators between conditions MUST always be at the beginning or at the end of the line, not a mix of both.
        Example:
        <?php
        for (
        $i = 0;
        $i < 10;
        $i++
        ) {
        // for body
        }

        <?php
        
        while (
            $expr1
            && $expr2
        ) {
            // structure body
        }
    • try, catch, finally
      • A try-catch-finally block looks like the following. Note the placement of parentheses, spaces, and braces.
        <?php
        try {
        // try body
        } catch (FirstThrowableType $e) {
        // catch body
        } catch (OtherThrowableType | AnotherThrowableType $e) {
        // catch body
        } finally {
        // finally body
        }

  14. Operators

    • When space is permitted around an operator, multiple spaces may be used for readability purposes.
    • Unary Operators
      • The increment/decrement operators must not have any space between the operator and operand.
      • Type casting operators must not have any space within the parentheses
        Example:
        $i++;
        ++$j;
        $intValue = (int) $input;
    • Binary operators
    • Ternary operators
      • The conditional operator, also known simply as the ternary operator, must be preceded and followed by at least one space around both the ? and : characters:

      • When the middle operand of the conditional operator is omitted, the operator MUST follow the same style rules as other binary comparison operators
        Example:

        $variable = $foo ? 'foo' : 'bar';
        $variable = $foo ?: 'bar';
  15. Closures

    • Closures must be declared with a space after the function keyword, and a space before and after the use keyword.
    • Closure arguments with default values must go at the end of the argument list.
    • If a return type is present, it must follow the same rules as with normal functions and methods; if the use keyword is present, the colon MUST follow the use list closing parentheses with no spaces between the two characters.
      Example:
      <?php
      $closureWithArgs = function ($arg1, $arg2) {
      // body
      };
      $closureWithArgsAndVars = function ($arg1, $arg2) use ($var1, $var2) {
      // body
      };
      $closureWithArgsVarsAndReturn = function ($arg1, $arg2) use ($var1, $var2): bool {
      // body
      };

    • The formatting rules also apply when the closure is used directly in a function or method call as an argument.
  16. Anonymous Classes

    • Anonymous Classes must follow the same guidelines and principles as closures in the above section.
    • The opening brace MAY be on the same line as the class keyword so long as the list of implements interfaces does not wrap. If the list of interfaces wraps, the brace MUST be placed on the line immediately following the last interface.
      Example
      <?php
      // Brace on the same line
      $instance = new class extends </span>Foo implements </span>HandleableInterface {
      // Class content
      };
      // Brace on the next line
      $instance = new class extends </span>Foo implements
      </span>ArrayAccess,
      <span class="token package">Countable,
      <span class="token package">Serializable
      {
      // Class content
      };

In order to ensure the coding style, add php-cs and php-md extensions to the IDE. These extensions are available in .phar format and can be used in all the environments.

Best Practices

  • Use early return for if – else statements whenever it is possible.
  • Use Fractal Transformers for the for the presentation and transformation of data.
  • Use Carbon extension for date and time related operations.
  • All the credentials should be added to the environment configuration and do not hard code any credentials to the code.
  • All the passwords should be hashed (not encrypted) and should be individually salted.
  • Use HHVM structure for large code bases, so that each module will be self-contained. In the case of Laravel, use Laravel Modules extension.
  • Add unit tests and incorporate them to the build pipeline.
  • Filter and sanitise all user inputs before it is used in the code.
  • Throw appropriate exceptions whenever necessary and use logging with appropriate log levels. User defined exceptions should be extended from a single Standardized Exception class.
  • Perl compatible regular expressions (PCRE, preg_ functions) should be used in preference to their POSIX counterparts.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment