Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A helper class for testing phpcs sniffs
use PHP_CodeSniffer\Files\LocalFile;
use PHP_CodeSniffer\Ruleset;
use PHP_CodeSniffer\Config;
class SniffTestHelper {
public function prepareLocalFileForSniffs($sniffFiles, string $fixtureFile): LocalFile {
$config = new Config();
$ruleset = new Ruleset($config);
if (! is_array($sniffFiles)) {
$sniffFiles = [$sniffFiles];
$ruleset->registerSniffs($sniffFiles, [], []);
return new LocalFile($fixtureFile, $ruleset, $config);
public function getLineNumbersFromMessages(array $messages): array {
return array_keys($messages);
public function getWarningLineNumbersFromFile(LocalFile $phpcsFile): array {
return $this->getLineNumbersFromMessages($phpcsFile->getWarnings());
public function getErrorLineNumbersFromFile(LocalFile $phpcsFile): array {
return $this->getLineNumbersFromMessages($phpcsFile->getErrors());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment