/**
* @see Command
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$stdErr = $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : null;
if ($stdErr && extension_loaded('xdebug')) {
$stdErr->writeln(sprintf($stdErr->isDecorated() ? '<bg=yellow;fg=black;>%s</>' : '%s', 'You are running php-cs-fixer with xdebug enabled. This has a major impact on runtime performance.'));
}
$verbosity = $output->getVerbosity();
$resolver = new ConfigurationResolver();
$resolver->setCwd(getcwd())->setDefaultConfig($this->defaultConfig)->setOptions(array('allow-risky' => $input->getOption('allow-risky'), 'config' => $input->getOption('config'), 'dry-run' => $input->getOption('dry-run'), 'rules' => $input->getOption('rules'), 'path' => $input->getArgument('path'), 'progress' => OutputInterface::VERBOSITY_VERBOSE <= $verbosity && 'txt' === $input->getOption('format'), 'using-cache' => $input->getOption('using-cache'), 'cache-file' => $input->getOption('cache-file'), 'format' => $input->getOption('format')))->resolve();
$config = $resolver->getConfig();
$configFile = $resolver->getConfigFile();
if ($configFile && 'txt' === $input->getOption('format')) {
$output->writeln(sprintf('Loaded config from "%s"', $configFile));
}
if ($config->usingLinter()) {
try {
$this->fixer->setLinter(new Linter($config->getPhpExecutable()));
} catch (UnavailableLinterException $e) {
if ($configFile && 'txt' === $input->getOption('format')) {
$output->writeln('Unable to use linter, can not find PHP executable');
}
}
}
$showProgress = $resolver->getProgress();
if ($showProgress) {
$this->fixer->setEventDispatcher($this->eventDispatcher);
$progressOutput = new ProcessOutput($this->eventDispatcher);
}
$this->stopwatch->start('fixFiles');
$changed = $this->fixer->fix($config, $resolver->isDryRun(), $input->getOption('diff'));
$this->stopwatch->stop('fixFiles');
if ($showProgress) {
$progressOutput->printLegend();
$this->fixer->setEventDispatcher(null);
}
$i = 1;
switch ($resolver->getFormat()) {
case 'txt':
$fixerDetailLine = false;
if (OutputInterface::VERBOSITY_VERBOSE <= $verbosity) {
$fixerDetailLine = $output->isDecorated() ? ' (<comment>%s</comment>)' : ' %s';
}
foreach ($changed as $file => $fixResult) {
$output->write(sprintf('%4d) %s', $i++, $file));
if ($fixerDetailLine) {
$output->write(sprintf($fixerDetailLine, implode(', ', $fixResult['appliedFixers'])));
}
if ($input->getOption('diff')) {
$output->writeln('');
$output->writeln('<comment> ---------- begin diff ----------</comment>');
$output->writeln($fixResult['diff']);
$output->writeln('<comment> ---------- end diff ----------</comment>');
}
$output->writeln('');
}
if (OutputInterface::VERBOSITY_DEBUG <= $verbosity) {
$output->writeln('Fixing time per file:');
foreach ($this->stopwatch->getSectionEvents('fixFile') as $file => $event) {
if ('__section__' === $file) {
continue;
}
$output->writeln(sprintf('[%.3f s] %s', $event->getDuration() / 1000, $file));
}
$output->writeln('');
}
$fixEvent = $this->stopwatch->getEvent('fixFiles');
$output->writeln(sprintf('%s all files in %.3f seconds, %.3f MB memory used', $input->getOption('dry-run') ? 'Checked' : 'Fixed', $fixEvent->getDuration() / 1000, $fixEvent->getMemory() / 1024 / 1024));
break;
case 'xml':
$dom = new \DOMDocument('1.0', 'UTF-8');
// new nodes should be added to this or existing children
$root = $dom->createElement('report');
$dom->appendChild($root);
$filesXML = $dom->createElement('files');
$root->appendChild($filesXML);
foreach ($changed as $file => $fixResult) {
$fileXML = $dom->createElement('file');
$fileXML->setAttribute('id', $i++);
$fileXML->setAttribute('name', $file);
$filesXML->appendChild($fileXML);
if (OutputInterface::VERBOSITY_VERBOSE <= $verbosity) {
$appliedFixersXML = $dom->createElement('applied_fixers');
$fileXML->appendChild($appliedFixersXML);
foreach ($fixResult['appliedFixers'] as $appliedFixer) {
$appliedFixerXML = $dom->createElement('applied_fixer');
$appliedFixerXML->setAttribute('name', $appliedFixer);
$appliedFixersXML->appendChild($appliedFixerXML);
}
}
if ($input->getOption('diff')) {
$diffXML = $dom->createElement('diff');
$diffXML->appendChild($dom->createCDATASection($fixResult['diff']));
$fileXML->appendChild($diffXML);
}
}
$fixEvent = $this->stopwatch->getEvent('fixFiles');
$timeXML = $dom->createElement('time');
$memoryXML = $dom->createElement('memory');
$root->appendChild($timeXML);
$root->appendChild($memoryXML);
$memoryXML->setAttribute('value', round($fixEvent->getMemory() / 1024 / 1024, 3));
$memoryXML->setAttribute('unit', 'MB');
$timeXML->setAttribute('unit', 's');
$timeTotalXML = $dom->createElement('total');
$timeTotalXML->setAttribute('value', round($fixEvent->getDuration() / 1000, 3));
$timeXML->appendChild($timeTotalXML);
if (OutputInterface::VERBOSITY_DEBUG <= $verbosity) {
$timeFilesXML = $dom->createElement('files');
$timeXML->appendChild($timeFilesXML);
$eventCounter = 1;
foreach ($this->stopwatch->getSectionEvents('fixFile') as $file => $event) {
if ('__section__' === $file) {
continue;
}
$timeFileXML = $dom->createElement('file');
$timeFilesXML->appendChild($timeFileXML);
$timeFileXML->setAttribute('id', $eventCounter++);
$timeFileXML->setAttribute('name', $file);
$timeFileXML->setAttribute('value', round($event->getDuration() / 1000, 3));
}
}
$dom->formatOutput = true;
$output->write($dom->saveXML());
break;
case 'json':
$jFiles = array();
foreach ($changed as $file => $fixResult) {
$jfile = array('name' => $file);
if (OutputInterface::VERBOSITY_VERBOSE <= $verbosity) {
$jfile['appliedFixers'] = $fixResult['appliedFixers'];
}
if ($input->getOption('diff')) {
$jfile['diff'] = $fixResult['diff'];
}
$jFiles[] = $jfile;
}
$fixEvent = $this->stopwatch->getEvent('fixFiles');
$json = array('files' => $jFiles, 'memory' => round($fixEvent->getMemory() / 1024 / 1024, 3), 'time' => array('total' => round($fixEvent->getDuration() / 1000, 3)));
if (OutputInterface::VERBOSITY_DEBUG <= $verbosity) {
$jFileTime = array();
foreach ($this->stopwatch->getSectionEvents('fixFile') as $file => $event) {
if ('__section__' === $file) {
continue;
}
$jFileTime[$file] = round($event->getDuration() / 1000, 3);
}
$json['time']['files'] = $jFileTime;
}
$output->write(json_encode($json));
break;
}
$invalidErrors = $this->errorsManager->getInvalidErrors();
if (!empty($invalidErrors)) {
$this->listErrors($output, 'linting before fixing', $invalidErrors);
}
$exceptionErrors = $this->errorsManager->getExceptionErrors();
if (!empty($exceptionErrors)) {
$this->listErrors($output, 'fixing', $exceptionErrors);
}
$lintErrors = $this->errorsManager->getLintErrors();
if (!empty($lintErrors)) {
$this->listErrors($output, 'linting after fixing', $lintErrors);
}
$exitStatus = 0;
if ($resolver->isDryRun()) {
if (!empty($invalidErrors)) {
$exitStatus |= self::EXIT_STATUS_FLAG_HAS_INVALID_FILES;
}
if (!empty($changed)) {
$exitStatus |= self::EXIT_STATUS_FLAG_HAS_CHANGED_FILES;
}
}
return $exitStatus;
}