Skip to content

Commit d34dd6d

Browse files
Print violations to stdout, everything else to stderr (#484)
* write all output messages in stderr apart from the list of violations
1 parent d7f99f7 commit d34dd6d

File tree

7 files changed

+164
-186
lines changed

7 files changed

+164
-186
lines changed

src/CLI/Command/Check.php

Lines changed: 28 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,15 @@
55
namespace Arkitect\CLI\Command;
66

77
use Arkitect\CLI\Config;
8-
use Arkitect\CLI\Printer\Printer;
98
use Arkitect\CLI\Progress\DebugProgress;
109
use Arkitect\CLI\Progress\ProgressBarProgress;
1110
use Arkitect\CLI\Runner;
1211
use Arkitect\CLI\TargetPhpVersion;
13-
use Arkitect\Rules\ParsingErrors;
1412
use Arkitect\Rules\Violations;
1513
use Symfony\Component\Console\Command\Command;
1614
use Symfony\Component\Console\Input\InputInterface;
1715
use Symfony\Component\Console\Input\InputOption;
16+
use Symfony\Component\Console\Output\ConsoleOutputInterface;
1817
use Symfony\Component\Console\Output\OutputInterface;
1918
use Webmozart\Assert\Assert;
2019

@@ -110,8 +109,18 @@ protected function execute(InputInterface $input, OutputInterface $output): int
110109
$useBaseline = (string) $input->getOption(self::USE_BASELINE_PARAM);
111110
$skipBaseline = (bool) $input->getOption(self::SKIP_BASELINE_PARAM);
112111
$ignoreBaselineLinenumbers = (bool) $input->getOption(self::IGNORE_BASELINE_LINENUMBERS_PARAM);
112+
$phpVersion = $input->getOption('target-php-version');
113113
$format = $input->getOption(self::FORMAT_PARAM);
114-
$onlyErrors = Printer::FORMAT_JSON === $format || Printer::FORMAT_GITLAB === $format;
114+
115+
// we write everything on STDERR apart from the list of violations which goes on STDOUT
116+
// this allows to pipe the output of this command to a file while showing output on the terminal
117+
$stdOut = $output;
118+
$output = $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output;
119+
120+
/** @var string|null $phpVersion */
121+
$targetPhpVersion = TargetPhpVersion::create($phpVersion);
122+
123+
$progress = $verbose ? new DebugProgress($output) : new ProgressBarProgress($output);
115124

116125
if (true !== $skipBaseline && !$useBaseline && file_exists(self::DEFAULT_BASELINE_FILENAME)) {
117126
$useBaseline = self::DEFAULT_BASELINE_FILENAME;
@@ -123,33 +132,22 @@ protected function execute(InputInterface $input, OutputInterface $output): int
123132
return self::ERROR_CODE;
124133
}
125134

126-
if (!$onlyErrors) {
127-
$output->writeln('<info>Baseline found: '.$useBaseline.'</info>');
128-
}
135+
$output->writeln('<info>Baseline found: '.$useBaseline.'</info>');
129136

130137
$generateBaseline = $input->getOption(self::GENERATE_BASELINE_PARAM);
131138

132-
/** @var string|null $phpVersion */
133-
$phpVersion = $input->getOption('target-php-version');
134-
$targetPhpVersion = TargetPhpVersion::create($phpVersion);
135-
136-
$progress = $verbose ? new DebugProgress($output) : new ProgressBarProgress($output);
137-
138-
if (!$onlyErrors) {
139-
$this->printHeadingLine($output);
140-
}
139+
$this->printHeadingLine($output);
141140

142141
$rulesFilename = $this->getConfigFilename($input);
143-
if (!$onlyErrors) {
144-
$output->writeln(\sprintf("Config file: %s\n", $rulesFilename));
145-
}
142+
143+
$output->writeln(\sprintf("Config file: %s\n", $rulesFilename));
146144

147145
$config = new Config();
148146

149147
$this->readRules($config, $rulesFilename);
150148

151149
$runner = new Runner($stopOnFailure);
152-
$runner->run($config, $progress, $targetPhpVersion, $onlyErrors);
150+
$runner->run($config, $progress, $targetPhpVersion);
153151

154152
$violations = $runner->getViolations();
155153
$violations->sort();
@@ -161,9 +159,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
161159
$this->saveBaseline($generateBaseline, $violations);
162160

163161
$output->writeln('<info>Baseline file \''.$generateBaseline.'\'created!</info>');
164-
if (!$onlyErrors) {
165-
$this->printExecutionTime($output, $startTime);
166-
}
162+
$this->printExecutionTime($output, $startTime);
167163

168164
return self::SUCCESS_CODE;
169165
}
@@ -174,18 +170,21 @@ protected function execute(InputInterface $input, OutputInterface $output): int
174170
$violations->remove($baseline, $ignoreBaselineLinenumbers);
175171
}
176172

173+
// we always print this so we do not have to do additional ifs later
174+
$stdOut->writeln($violations->toString($format));
175+
177176
if ($violations->count() > 0) {
178-
$this->printViolations($violations, $output, $format, $onlyErrors);
179-
if (!$onlyErrors) {
180-
$this->printExecutionTime($output, $startTime);
181-
}
177+
$output->writeln(\sprintf('<error>⚠️ %s violations detected!</error>', \count($violations)));
178+
$this->printExecutionTime($output, $startTime);
182179

183180
return self::ERROR_CODE;
184181
}
185182

186183
$parsedErrors = $runner->getParsingErrors();
184+
187185
if ($parsedErrors->count() > 0) {
188-
$this->printParsedErrors($parsedErrors, $output, $onlyErrors);
186+
$output->writeln('<error>❌ could not parse these files:</error>');
187+
$output->writeln($parsedErrors->toString());
189188
$this->printExecutionTime($output, $startTime);
190189

191190
return self::ERROR_CODE;
@@ -197,11 +196,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
197196
return self::ERROR_CODE;
198197
}
199198

200-
$this->printNoViolationsDetectedMessage($output, $onlyErrors, $format);
199+
$output->writeln('<info>✅ No violations detected</info>');
201200

202-
if (!$onlyErrors) {
203-
$this->printExecutionTime($output, $startTime);
204-
}
201+
$this->printExecutionTime($output, $startTime);
205202

206203
return self::SUCCESS_CODE;
207204
}
@@ -257,33 +254,4 @@ private function getConfigFilename(InputInterface $input): string
257254

258255
return $filename;
259256
}
260-
261-
private function printViolations(Violations $violations, OutputInterface $output, string $format, bool $onlyErrors = false): void
262-
{
263-
if (!$onlyErrors) {
264-
$output->writeln('<error>ERRORS!</error>');
265-
}
266-
267-
$output->writeln(\sprintf('%s', $violations->toString($format)));
268-
if (!$onlyErrors) {
269-
$output->writeln(\sprintf('<error>%s VIOLATIONS DETECTED!</error>', \count($violations)));
270-
}
271-
}
272-
273-
private function printParsedErrors(ParsingErrors $parsingErrors, OutputInterface $output, bool $onlyErrors = false): void
274-
{
275-
if (!$onlyErrors) {
276-
$output->writeln('<error>ERROR ON PARSING THESE FILES:</error>');
277-
}
278-
$output->writeln(\sprintf('%s', $parsingErrors->toString()));
279-
}
280-
281-
private function printNoViolationsDetectedMessage(OutputInterface $output, bool $onlyErrors = false, string $format = Printer::FORMAT_TEXT): void
282-
{
283-
if (!$onlyErrors) {
284-
$output->writeln('<info>NO VIOLATIONS DETECTED!</info>');
285-
} elseif (Printer::FORMAT_JSON === $format || Printer::FORMAT_GITLAB === $format) {
286-
$output->writeln('<info>[]</info>');
287-
}
288-
}
289257
}

src/CLI/Printer/TextPrinter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class TextPrinter implements Printer
99
{
1010
public function print(array $violationsCollection): string
1111
{
12-
$errors = '';
12+
$errors = "\n";
1313

1414
/**
1515
* @var string $key

src/CLI/Runner.php

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,22 @@ public function __construct(bool $stopOnFailure = false)
3030
$this->parsingErrors = new ParsingErrors();
3131
}
3232

33-
public function run(Config $config, Progress $progress, TargetPhpVersion $targetPhpVersion, bool $onlyErrors): void
33+
public function run(Config $config, Progress $progress, TargetPhpVersion $targetPhpVersion): void
3434
{
3535
/** @var FileParser $fileParser */
3636
$fileParser = FileParserFactory::createFileParser($targetPhpVersion, $config->isParseCustomAnnotationsEnabled());
3737

3838
/** @var ClassSetRules $classSetRule */
3939
foreach ($config->getClassSetRules() as $classSetRule) {
40-
if (!$onlyErrors) {
41-
$progress->startFileSetAnalysis($classSetRule->getClassSet());
42-
}
40+
$progress->startFileSetAnalysis($classSetRule->getClassSet());
4341

4442
try {
45-
$this->check($classSetRule, $progress, $fileParser, $this->violations, $this->parsingErrors, $onlyErrors);
43+
$this->check($classSetRule, $progress, $fileParser, $this->violations, $this->parsingErrors);
4644
} catch (FailOnFirstViolationException $e) {
4745
return;
4846
}
4947

50-
if (!$onlyErrors) {
51-
$progress->endFileSetAnalysis($classSetRule->getClassSet());
52-
}
48+
$progress->endFileSetAnalysis($classSetRule->getClassSet());
5349
}
5450
}
5551

@@ -58,16 +54,13 @@ public function check(
5854
Progress $progress,
5955
Parser $fileParser,
6056
Violations $violations,
61-
ParsingErrors $parsingErrors,
62-
bool $onlyErrors = false
57+
ParsingErrors $parsingErrors
6358
): void {
6459
/** @var SplFileInfo $file */
6560
foreach ($classSetRule->getClassSet() as $file) {
6661
$fileViolations = new Violations();
6762

68-
if (!$onlyErrors) {
69-
$progress->startParsingFile($file->getRelativePathname());
70-
}
63+
$progress->startParsingFile($file->getRelativePathname());
7164

7265
$fileParser->parse($file->getContents(), $file->getRelativePathname());
7366
$parsedErrors = $fileParser->getParsingErrors();
@@ -91,9 +84,7 @@ public function check(
9184

9285
$violations->merge($fileViolations);
9386

94-
if (!$onlyErrors) {
95-
$progress->endParsingFile($file->getRelativePathname());
96-
}
87+
$progress->endParsingFile($file->getRelativePathname());
9788
}
9889
}
9990

0 commit comments

Comments
 (0)