Skip to content

[Yaml] Avoid using both Input/Output and SymfonyStyle in LintCommand #19160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 29, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions src/Symfony/Component/Yaml/Command/LintCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
class LintCommand extends Command
{
private $parser;
private $format;
private $displayCorrectFiles;

/**
* {@inheritdoc}
Expand Down Expand Up @@ -65,13 +67,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new SymfonyStyle($input, $output);
$filename = $input->getArgument('filename');
$this->format = $input->getOption('format');
$this->displayCorrectFiles = $output->isVerbose();

if (!$filename) {
if (!$stdin = $this->getStdin()) {
throw new \RuntimeException('Please provide a filename or pipe file content to STDIN.');
}

return $this->display($input, $output, $io, array($this->validate($stdin)));
return $this->display($io, array($this->validate($stdin)));
}

if (!$this->isReadable($filename)) {
Expand All @@ -83,7 +87,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$filesInfo[] = $this->validate(file_get_contents($file), $file);
}

return $this->display($input, $output, $io, $filesInfo);
return $this->display($io, $filesInfo);
}

private function validate($content, $file = null)
Expand All @@ -97,29 +101,29 @@ private function validate($content, $file = null)
return array('file' => $file, 'valid' => true);
}

private function display(InputInterface $input, OutputInterface $output, SymfonyStyle $io, $files)
private function display(SymfonyStyle $io, array $files)
{
switch ($input->getOption('format')) {
switch ($this->format) {
case 'txt':
return $this->displayTxt($output, $io, $files);
return $this->displayTxt($io, $files);
case 'json':
return $this->displayJson($io, $files);
default:
throw new \InvalidArgumentException(sprintf('The format "%s" is not supported.', $input->getOption('format')));
throw new \InvalidArgumentException(sprintf('The format "%s" is not supported.', $this->format));
}
}

private function displayTxt(OutputInterface $output, SymfonyStyle $io, $filesInfo)
private function displayTxt(SymfonyStyle $io, array $filesInfo)
{
$countFiles = count($filesInfo);
$erroredFiles = 0;

foreach ($filesInfo as $info) {
if ($info['valid'] && $output->isVerbose()) {
if ($info['valid'] && $this->displayCorrectFiles) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove the displayCorrectFiles property entirely and replace it with $io->isVerbose()

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@javiereguiluz I was glad to use it, but it make tests failing as the verbosity methods are available since 3.1 only, dev requirements are to ~2.8|~3.0. Should I up the version bound to ~3.1 for that?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry I didn't think about that. Let's keep the property then, even its use looks so limited.

$io->comment('<info>OK</info>'.($info['file'] ? sprintf(' in %s', $info['file']) : ''));
} elseif (!$info['valid']) {
++$erroredFiles;
$io->text(sprintf('<error> ERROR </error> in %s', $info['file']));
$io->text('<error> ERROR </error>'.($info['file'] ? sprintf(' in %s', $info['file']) : ''));
$io->text(sprintf('<error> >> %s</error>', $info['message']));
}
}
Expand All @@ -133,7 +137,7 @@ private function displayTxt(OutputInterface $output, SymfonyStyle $io, $filesInf
return min($erroredFiles, 1);
}

private function displayJson(OutputInterface $output, $filesInfo)
private function displayJson(SymfonyStyle $io, array $filesInfo)
{
$errors = 0;

Expand All @@ -144,7 +148,7 @@ private function displayJson(OutputInterface $output, $filesInfo)
}
});

$output->writeln(json_encode($filesInfo, JSON_PRETTY_PRINT));
$io->writeln(json_encode($filesInfo, JSON_PRETTY_PRINT));

return min($errors, 1);
}
Expand Down