3 Maximum line length
LogMANOriginal edited this page 2018-11-05 13:50:28 +01:00

The maximum line length should not exceed 80 characters

One line of code should have no more than 80 characters (soft limit) and must never exceed 120 characters (hard limit).

Notice: Travis-CI enforces the hard limit of 120 characters. Maintainers may ask you to indent lines longer than 80 characters before merging. This is generally done to keep the code as readable and maintainable as possible.

For long conditional statements, consider indenting the statement into multiple lines.

Example

Bad (the total length of the line is 94 characters)

if($time !== false && (time() - $duration < $time) && (!defined('DEBUG') || DEBUG !== true)) {

}

Good (add line breaks)

if($time !== false
&& (time() - $duration < $time)
&& (!defined('DEBUG') || DEBUG !== true)) {

}

For long text, either add line feeds, or make use of the heredoc syntax.

Example

Bad (the total length of the line is 340 characters - from Lorem Ipsum)

$longtext = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse condimentum nec est eget posuere. Proin at sagittis risus. Fusce faucibus lectus leo, eu ornare velit tristique eu. Curabitur elementum facilisis ultricies. Praesent dictum fermentum lectus a rhoncus. Donec vitae justo metus. Sed molestie faucibus egestas.';

Good (use heredoc syntax - this will add line-breaks)

$longtext = <<<EOD
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse
condimentum nec est eget posuere. Proin at sagittis risus. Fusce faucibus
lectus leo, eu ornare velit tristique eu. Curabitur elementum facilisis
ultricies. Praesent dictum fermentum lectus a rhoncus. Donec vitae justo metus.
Sed molestie faucibus egestas.
EOD;

Reference: Generic.Files.LineLength