Created
September 8, 2016 02:21
-
-
Save gsherwood/aafd2c16631a8a872f0c4a23916962ac to your computer and use it in GitHub Desktop.
A custom runner for PHP_CodeSniffer to process a single piece of content
This file contains hidden or 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 | |
namespace MyCustomProject; | |
use PHP_CodeSniffer\Runner; | |
use PHP_CodeSniffer\Config; | |
use PHP_CodeSniffer\Reporter; | |
use PHP_CodeSniffer\Files\DummyFile; | |
use PHP_CodeSniffer\Util\Timing; | |
// Include the PHPCS autoloader however you need. | |
require_once __DIR__.'/autoload.php'; | |
// If you don't want timing information in the report, remove this. | |
Timing::startTiming(); | |
// Create and populate the runner using a fixed standard. | |
$runner = new Runner(); | |
$runner->config = new Config(); | |
$runner->config->standards = array('PSR2'); | |
$runner->init(); | |
// Hard-code some other config settings. | |
// Do this after init() so these values override anything that was set in | |
// the rulesets we processed during init(). Or do this before if you want | |
// to use them like defaults instead. | |
$runner->config->reports = array('summary' => null, 'full' => null); | |
$runner->config->verbosity = 0; | |
$runner->config->showProgress = false; | |
$runner->config->interactive = false; | |
$runner->config->cache = false; | |
$runner->config->showSources = true; | |
// Create the reporter, using the hard-coded settings from above. | |
$runner->reporter = new Reporter($runner->config); | |
// Create and process a single file, faking the path so the report looks nice. | |
$fileContent = "<?php\necho 'hi';"; | |
$file = new DummyFile($fileContent, $runner->ruleset, $runner->config); | |
$file->path = '/path/to/my/file.php'; | |
// Process the file. | |
$runner->processFile($file); | |
// Print out the reports. | |
$runner->reporter->printReports(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The output for this file looks like this: