Skip to content

Instantly share code, notes, and snippets.

@gsherwood
Created September 8, 2016 02:21
Show Gist options
  • Save gsherwood/aafd2c16631a8a872f0c4a23916962ac to your computer and use it in GitHub Desktop.
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
<?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();
@gsherwood
Copy link
Author

The output for this file looks like this:

$ php CustomPHPCSRunner.php

PHP CODE SNIFFER REPORT SUMMARY
----------------------------------------------------------------------
FILE                                                  ERRORS  WARNINGS
----------------------------------------------------------------------
/path/to/my/file.php                                  1       0
----------------------------------------------------------------------
A TOTAL OF 1 ERROR AND 0 WARNINGS WERE FOUND IN 1 FILE
----------------------------------------------------------------------
PHPCBF CAN FIX 1 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------

Time: 62ms; Memory: 5Mb


FILE: /path/to/my/file.php
--------------------------------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
--------------------------------------------------------------------------------------------------
 2 | ERROR | [x] Expected 1 newline at end of file; 0 found (PSR2.Files.EndFileNewline.NoneFound)
--------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
--------------------------------------------------------------------------------------------------

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy