Skip to content

Commit dd84b7f

Browse files
committed
[Yaml] Avoid using both Input/Output and SymfonyStyle in LintCommand
Remove input/output from method arguments, use SymfonyStyle instead Avoid 'error in filename' if no filename as argument Add missing array typehint to files argument of display*() methods Store format option as member rather than pass it in method calls CS fixes
1 parent 06f5c86 commit dd84b7f

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

src/Symfony/Component/Yaml/Command/LintCommand.php

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
class LintCommand extends Command
2929
{
3030
private $parser;
31+
private $format;
32+
private $displayCorrectFiles;
3133

3234
/**
3335
* {@inheritdoc}
@@ -65,13 +67,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
6567
{
6668
$io = new SymfonyStyle($input, $output);
6769
$filename = $input->getArgument('filename');
70+
$this->format = $input->getOption('format');
71+
$this->displayCorrectFiles = $output->isVerbose();
6872

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

74-
return $this->display($input, $output, $io, array($this->validate($stdin)));
78+
return $this->display($io, array($this->validate($stdin)));
7579
}
7680

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

86-
return $this->display($input, $output, $io, $filesInfo);
90+
return $this->display($io, $filesInfo);
8791
}
8892

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

100-
private function display(InputInterface $input, OutputInterface $output, SymfonyStyle $io, $files)
104+
private function display(SymfonyStyle $io, array $files)
101105
{
102-
switch ($input->getOption('format')) {
106+
switch ($this->format) {
103107
case 'txt':
104-
return $this->displayTxt($output, $io, $files);
108+
return $this->displayTxt($io, $files);
105109
case 'json':
106110
return $this->displayJson($io, $files);
107111
default:
108-
throw new \InvalidArgumentException(sprintf('The format "%s" is not supported.', $input->getOption('format')));
112+
throw new \InvalidArgumentException(sprintf('The format "%s" is not supported.', $this->format));
109113
}
110114
}
111115

112-
private function displayTxt(OutputInterface $output, SymfonyStyle $io, $filesInfo)
116+
private function displayTxt(SymfonyStyle $io, array $filesInfo)
113117
{
114118
$countFiles = count($filesInfo);
115119
$erroredFiles = 0;
116120

117121
foreach ($filesInfo as $info) {
118-
if ($info['valid'] && $output->isVerbose()) {
122+
if ($info['valid'] && $this->displayCorrectFiles) {
119123
$io->comment('<info>OK</info>'.($info['file'] ? sprintf(' in %s', $info['file']) : ''));
120124
} elseif (!$info['valid']) {
121125
++$erroredFiles;
122-
$io->text(sprintf('<error> ERROR </error> in %s', $info['file']));
126+
$io->text('<error> ERROR </error>'.($info['file'] ? sprintf(' in %s', $info['file']) : ''));
123127
$io->text(sprintf('<error> >> %s</error>', $info['message']));
124128
}
125129
}
@@ -133,7 +137,7 @@ private function displayTxt(OutputInterface $output, SymfonyStyle $io, $filesInf
133137
return min($erroredFiles, 1);
134138
}
135139

136-
private function displayJson(OutputInterface $output, $filesInfo)
140+
private function displayJson(SymfonyStyle $io, array $filesInfo)
137141
{
138142
$errors = 0;
139143

@@ -144,7 +148,7 @@ private function displayJson(OutputInterface $output, $filesInfo)
144148
}
145149
});
146150

147-
$output->writeln(json_encode($filesInfo, JSON_PRETTY_PRINT));
151+
$io->writeln(json_encode($filesInfo, JSON_PRETTY_PRINT));
148152

149153
return min($errors, 1);
150154
}

0 commit comments

Comments
 (0)