Tag: PHP_CodeSniffer

How to ignore PHP_CodeSniffer warning: Line exceeds 120 characters;

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
----------------------------------------------------------------------

To ignore those warnings, we can add // phpcs:ignore as a comment to the end of a (too long) line. For example:

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

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

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

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

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

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

<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.