/**
* Run PHPCS on a file.
*
* Returns a string representation of the output that would normally
* be printed to the console. Artifically sets the `-s` (showSources)
* command line switch to make it possible to parse which rules failed
* for a given sample file.
*
* @param string $file to run.
* @return string The output from phpcs.
*/
public function runPhpCs($file)
{
$defaults = $this->_phpcs->getDefaults();
$standard = $this->_rootDir . '/ruleset.xml';
if (defined('PHP_CodeSniffer::VERSION') && version_compare(PHP_CodeSniffer::VERSION, '1.5.0') != -1) {
$standard = [$standard];
}
$options = ['encoding' => 'utf-8', 'files' => [$file], 'standard' => $standard, 'showSources' => true] + $defaults;
// New PHPCS has a strange issue where the method arguments
// are not stored on the instance causing weird errors.
$reflection = new ReflectionProperty($this->_phpcs, 'values');
$reflection->setAccessible(true);
$reflection->setValue($this->_phpcs, $options);
ob_start();
$this->_phpcs->process($options);
$result = ob_get_contents();
ob_end_clean();
return $result;
}