Created
December 5, 2017 22:19
-
-
Save sirbrillig/56cb370455e77b10afbdc20103e0c2a1 to your computer and use it in GitHub Desktop.
A helper class for testing phpcs sniffs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
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, [], []); | |
$ruleset->populateTokenListeners(); | |
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