public function fixFile()
{
$fixable = $this->_currentFile->getFixableCount();
if ($fixable === 0) {
// Nothing to fix.
return false;
}
$stdin = false;
$cliValues = $this->_currentFile->phpcs->cli->getCommandLineValues();
if (empty($cliValues['files']) === true) {
$stdin = true;
}
$this->enabled = true;
$this->loops = 0;
while ($this->loops < 50) {
ob_start();
// Only needed once file content has changed.
$contents = $this->getContents();
if (PHP_CODESNIFFER_VERBOSITY > 2) {
@ob_end_clean();
echo '---START FILE CONTENT---' . PHP_EOL;
$lines = explode($this->_currentFile->eolChar, $contents);
$max = strlen(count($lines));
foreach ($lines as $lineNum => $line) {
$lineNum++;
echo str_pad($lineNum, $max, ' ', STR_PAD_LEFT) . '|' . $line . PHP_EOL;
}
echo '--- END FILE CONTENT ---' . PHP_EOL;
ob_start();
}
$this->_inConflict = false;
$this->_currentFile->refreshTokenListeners();
$this->_currentFile->start($contents);
ob_end_clean();
$this->loops++;
if (PHP_CODESNIFFER_CBF === true && $stdin === false) {
echo "\r" . str_repeat(' ', 80) . "\r";
echo "\t=> Fixing file: {$this->_numFixes}/{$fixable} violations remaining [made {$this->loops} pass";
if ($this->loops > 1) {
echo 'es';
}
echo ']... ';
}
if ($this->_numFixes === 0 && $this->_inConflict === false) {
// Nothing left to do.
break;
} else {
if (PHP_CODESNIFFER_VERBOSITY > 1) {
echo "\t* fixed {$this->_numFixes} violations, starting loop " . ($this->loops + 1) . ' *' . PHP_EOL;
}
}
}
//end while
$this->enabled = false;
if ($this->_numFixes > 0) {
if (PHP_CODESNIFFER_VERBOSITY > 1) {
@ob_end_clean();
echo "\t*** Reached maximum number of loops with {$this->_numFixes} violations left unfixed ***" . PHP_EOL;
ob_start();
}
return false;
}
return true;
}