# CodeIgniter Coding Style Guide 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][psr12] 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](http://tools.ietf.org/html/rfc2119). _Portions of the following rules are from and attributed to [PSR-12][psr12]. Even if we do not copy all the rules to this coding style guide explicitly, such uncopied rules SHALL still apply._ [psr12]: https://www.php-fig.org/psr/psr-12/ ## Implementation Our team uses [PHP CS Fixer](https://github.com/FriendsOfPHP/PHP-CS-Fixer) to apply coding standard fixes automatically. If you would like to leverage these tools yourself visit the [Official CodeIgniter Coding Standard](https://github.com/CodeIgniter/coding-standard) repository for details. ## General ### 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. ### Lines - There MUST NOT be a hard limit on line length. - The soft limit on line length MUST be 120 characters. - Lines SHOULD NOT be longer than 80 characters; lines longer than that SHOULD be split into multiple subsequent lines of no more than 80 characters each. - There MUST NOT be trailing whitespace at the end of lines. - Blank lines MAY be added to improve readability and to indicate related blocks of code except where explicitly forbidden. - There MUST NOT be more than one statement per line. ### Indenting - Code MUST use an indent of 4 spaces for each indent level, and MUST NOT use tabs for indenting. ### Keywords and Types - All PHP reserved [keywords][1] and [types][2] MUST be in lower case. - Any new types and keywords added to future PHP versions MUST be in lower case. - Short form of type keywords MUST be used i.e. `bool` instead of `boolean`, `int` instead of `integer` etc. [1]: http://php.net/manual/en/reserved.keywords.php [2]: http://php.net/manual/en/reserved.other-reserved-words.php ## Declare Statements, Namespace, and Import Statements 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. - Opening ` [!NOTE] > All the preceding rules are quoted from PSR-12. You may visit its website to view the code block samples. ## Custom Conventions ### File Naming - Files containing PHP code SHOULD end with a ".php" extension. - Files containing templates SHOULD end with a ".tpl" extension. - Files containing classes, interfaces, or traits MUST have their base name exactly matching the name of the classes they declare. - Files declaring procedural functions SHOULD be written in snake_case format. ### Naming of Structural Elements - Constants MUST be declared in UPPERCASE_SEPARATED_WITH_UNDERSCORES. - Class names MUST be declared in PascalCase. - Method and property names MUST be declared in camelCase. - Procedural functions MUST be in snake_case. - Abbreviations/acronyms/initialisms SHOULD be written in their own natural format. ### Logical Operators - The negation operator `!` SHOULD have one space from its argument. ```diff -!$result +! $result ``` - Use parentheses to clarify potentially confusing logical expressions. ### PHP Docblocks (PHPDoc) - There SHOULD be no useless PHPDoc annotations. ```diff -/** - * @param string $data Data - * @return void - */ public function analyse(string $data): void {}; ``` ### PHPUnit Assertions - As much as possible, you SHOULD always use the strict version of assertions. ```diff -$this->assertEquals(12, (int) $axis); +$this->assertSame(12, (int) $axis); ``` - Use the dedicated assertion instead of using internal types. ```diff -$this->assertSame(true, is_cli()); +$this->assertTrue(is_cli()); -$this->assertTrue(array_key_exists('foo', $array)); +$this->assertArrayHasKey('foo', $array); ```