contributing/styleguide.md
This document declares a set of coding conventions and rules to be followed when contributing PHP code to the CodeIgniter project.
[!NOTE] While we would recommend it, there's no requirement that you follow these conventions and rules in your own projects. Usage is discretionary within your projects but strictly enforceable within the framework.
We follow the PSR-12: Extended Coding Style plus a set of our own styling conventions.
The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.
Portions of the following rules are from and attributed to PSR-12. Even if we do not copy all the rules to this coding style guide explicitly, such uncopied rules SHALL still apply.
Our team uses PHP CS Fixer to apply coding standard fixes automatically. If you would like to leverage these tools yourself visit the Official CodeIgniter Coding Standard repository for details.
?> tag MUST be omitted from files containing only PHP.bool instead of boolean, int instead of integer etc.The header of a PHP file may consist of a number of different blocks. If present, each of the blocks below MUST be separated by a single blank line, and MUST NOT contain a blank line. Each block MUST be in the order listed below, although blocks that are not relevant may be omitted.
<?php tag.use import statements.use import statements.use import statements.When a file contains a mix of HTML and PHP, any of the above sections may still be used. If so, they MUST be present at the top of the file, even if the remainder of the code consists of a closing PHP tag and then a mixture of HTML and PHP.
When the opening <?php tag is on the first line of the file, it MUST be on its own line with no other
statements unless it is a file containing markup outside of PHP opening and closing tags.
Import statements MUST never begin with a leading backslash as they must always be fully qualified.
The term "class" refers to all classes, interfaces, and traits.
The extends and implements keywords MUST be declared on the same line as the class name.
Lists of implements and, in the case of interfaces, extends MAY be split across multiple lines,
where each subsequent line is indented once. When doing so, the first item in the list MUST be on
the next line, and there MUST be only one interface per line.
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.
Opening braces MUST be on their own line and MUST NOT be preceded or followed by a blank line.
Closing braces MUST be on their own line and MUST NOT be preceded by a blank line.
use keyword used inside the classes to implement traits MUST be declared on the next line after the opening brace.var keyword MUST NOT be used to declare a property.abstract, final, and staticabstract and final declarations MUST precede the visibility declaration.static declaration MUST come after the visibility declaration.The general style rules for control structures are as follows:
The body of each structure MUST be enclosed by braces. This standardizes how the structures look and reduces the likelihood of introducing errors as new lines get added to the body.
if, elseif, elseelseif SHOULD be used instead of else if so that all control keywords look like single words.switch, case// no break when fall-through is intentional in a non-empty case body.while, do while? and : characters.[!NOTE] All the preceding rules are quoted from PSR-12. You may visit its website to view the code block samples.
! SHOULD have one space from its argument.-!$result
+! $result
-/**
- * @param string $data Data
- * @return void
- */
public function analyse(string $data): void {};
-$this->assertEquals(12, (int) $axis);
+$this->assertSame(12, (int) $axis);
-$this->assertSame(true, is_cli());
+$this->assertTrue(is_cli());
-$this->assertTrue(array_key_exists('foo', $array));
+$this->assertArrayHasKey('foo', $array);