A sniff registers what token types it wishes to listen for, then, when PHP_CodeSniffer encounters that token, the sniff is invoked and passed information about where the token was found in the stack, and the PHP_CodeSniffer file in which the token was found.
void process(
PHP_CodeSniffer_File
$phpcsFile, int
$stackPtr)
The stackPtr variable indicates where in the stack the token was found. A sniff can acquire information this token, along with all the other tokens within the stack by first acquiring the token stack:
If the sniff discovers an anomilty in the code, they can raise an error by calling addError() on the PHP_CodeSniffer_File object, specifying an error message and the position of the offending token:
array(int) register(
)
An example return value for a sniff that wants to listen for whitespace and any comments would be: