PHP_CodeSniffer is a set of PHP scripts to detect violations of a defined coding standard, and to automatically correct such coding standard violations.
When using CodeSniffer to check your code, a lot of warnings might appear for such violations. Accodring to PSR-2 coding style guide, a warning is raised 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:
// 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:
$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
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:
// 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
:
// @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:
<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
Leave a Reply