Yellow-black warning stripes

How to ignore PHP_CodeSniffer warning: Line exceeds 120 characters

Written by

in

When using CodeSniffer to check your code, a lot of warnings might appear when the lines are too long:

----------------------------------------------------------------------
FOUND 0 ERRORS AND 4 WARNINGS AFFECTING 4 LINES
----------------------------------------------------------------------
  73 | WARNING | Line exceeds 120 characters; contains 162 characters
  75 | WARNING | Line exceeds 120 characters; contains 124 characters
 102 | WARNING | Line exceeds 120 characters; contains 168 characters
 108 | WARNING | Line exceeds 120 characters; contains 121 characters
----------------------------------------------------------------------

Limiting the length of a line of code improves code readability. It also makes comparing code side-by-side easier and improves compatibility with various editors, IDEs, and diff viewers.

Nevertheless, it may be helpful to ignore those warnings in some cases. There are different possibilities to ignore warnings or errors in CodeSniffer. Let’s have a look at them.

Ignore a single code line

To ignore those warnings, we can add // phpcs:ignore as a comment. Placing the comment at the beginning of the line will ignore the line and the following one:

PHP
// phpcs:ignore
$message = 'This is my extreeeeeeeeeeeeeeeeeeeeeeeeeeeeeemly long message.';

If the comment is added to the end of a (too long) line, only that line will be ignored:

PHP
$message = 'This is my extreeeeeeeeeeeeeeeeeeeeeeeeeeeeeemly long message.'; // phpcs:ignore

Another posibility is to add the comment // @codingStandardsIgnoreLine on the line in question:

PHP
$message = 'This is my extreeeeeeeeeeeeeeeeeeeeeeeeeeeeeemly long message.'; // @codingStandardsIgnoreLine

Ignore multiple code lines

If you want to ignore the warning for multiple lines in a file, you can add the following comments around the lines:

PHP
// phpcs:disable
$message = 'This is my extreeeeeeeeeeeeeeeeeeeeeeeeeeeeeemly long message.';
$log = 'This is my extreeeeeeeeeeeeeeeeeeeeeeeeeeeeeemly long log message.';
// phpcs:enable

Note: Before PHP_CodeSniffer version 3.2.0, use // @codingStandardsIgnoreStart instead of // phpcs:disable, and use // @codingStandardsIgnoreEnd instead of // phpcs:enable. The @codingStandards syntax is deprecated and will be removed in PHP_CodeSniffer version 4.0. Source

The old syntax uses @codingStandardsIgnoreStart and @codingStandardsIgnoreEnd:

PHP
// @codingStandardsIgnoreStart
$message = 'This is my extreeeeeeeeeeeeeeeeeeeeeeeeeeeeeemly long message.';
$log = 'This is my extreeeeeeeeeeeeeeeeeeeeeeeeeeeeeemly long log message.';
// @codingStandardsIgnoreEnd

Adjust the CodeSniffer rules

Another way to ignore the “Line exceeds” warning is to use phpcs.xml file and set the rule maxLineLength to the desired value:

XML
<rule ref="PSR2.ControlStructures.ControlStructureSpacing">
    <properties>
        <property name="maxLineLength" value="120" />
    </properties>
</rule>

It’s worth noting that while ignoring warnings can be useful in some cases, it’s generally a better practice to address the underlying issues that are causing the warnings, as they can indicate potential problems in your code.

A full list of CodeSniffer features to handle the warnings and errors is documented in the CodeSniffer Wiki.

Photo by Pop & Zebra on Unsplash


Comments

Leave a Reply

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