Creating a Custom PHP_CodeSniffer Rule to Disallow Hash Comments
This tutorial explains how to build a custom PHP_CodeSniffer standard that forbids the use of hash (#) for single‑line comments, covering directory setup, rule implementation in PHP, detailed code walkthrough, execution commands, and interpreting the generated XML report.
After learning the basics of lexical analysis, we demonstrate how to create a simple PHP_CodeSniffer rule that prohibits the use of # for single‑line comments.
Test code with violations:
<?php
# Check for valid contents.
if ($obj->contentsAreValid($array)) {
$value = $obj->getValue();
# Value needs to be an array.
if (is_array($value) === false) {
# Error.
$obj->throwError();
exit();
}
}
?>The PHP_CodeSniffer rule libraries reside in /src/Standards/ . To add a custom standard, create a new folder (e.g., FireLine ) with sub‑folders Sniffs and a ruleset.xml file.
Sample ruleset.xml :
<?xml version="1.0"?>
360 FireLine rule for test.Inside Sniffs/Commenting/ create DisallowHashCommentsSniff.php implementing the Sniff interface. The class registers for T_COMMENT tokens and in process() checks whether the first character of the token content is # . If so, it adds an error message.
<?php
/**
* This sniff prohibits the use of Perl style hash comments.
*/
namespace PHP_CodeSniffer\Standards\FireLine\Sniffs\Commenting;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;
class DisallowHashCommentsSniff implements Sniff {
public function register() {
return array(T_COMMENT);
}
public function process(File $phpcsFile, $stackPtr) {
$tokens = $phpcsFile->getTokens();
if ($tokens[$stackPtr]['content']{0} === '#') {
$error = '禁止使用#号进行单行注释;扫描发现 %s';
$data = array(trim($tokens[$stackPtr]['content']));
$phpcsFile->addError($error, $stackPtr, 'Found', $data);
}
}
}
?>The register() method tells PHP_CodeSniffer to look for T_COMMENT tokens, which represent // , # , and /* */ comments. The process() method receives the file object and the token index, extracts the token content, and reports an error when a hash comment is found.
To run the custom standard, execute:
php D:/git/PHP_CodeSniffer/bin/phpcs \
--standard=D:/git/PHP_CodeSniffer/src/Standards/FireLine \
D:/git/PHP_CodeSniffer/src/Standards/FireLine/Tests \
--report=xml --report-file=E:/RedlineReport/php_report01.xmlThe generated XML report lists three errors, each corresponding to a hash comment in the test file, confirming that the custom rule works as intended.
<?xml version="1.0" encoding="UTF-8"?>
禁止使用#号进行注释;扫描发现 # Check for valid contents.
禁止使用#号进行注释;扫描发现 # Value needs to be an array.
禁止使用#号进行注释;扫描发现 # Error.Thus, by following these steps you can extend PHP_CodeSniffer with your own coding‑standard rules and verify them through automated scans.
360 Quality & Efficiency
360 Quality & Efficiency focuses on seamlessly integrating quality and efficiency in R&D, sharing 360’s internal best practices with industry peers to foster collaboration among Chinese enterprises and drive greater efficiency value.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.