Skip to content

Commit

Permalink
Introduce results writer for existing cli text
Browse files Browse the repository at this point in the history
  • Loading branch information
dkreuer committed Dec 6, 2021
1 parent 1d996a8 commit 05a6604
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 14 deletions.
32 changes: 18 additions & 14 deletions src/ComposerRequireChecker/Cli/CheckCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace ComposerRequireChecker\Cli;

use ComposerRequireChecker\ASTLocator\LocateASTFromFiles;
use ComposerRequireChecker\Cli\ResultsWriter\CliText;
use ComposerRequireChecker\DefinedExtensionsResolver\DefinedExtensionsResolver;
use ComposerRequireChecker\DefinedSymbolsLocator\LocateDefinedSymbolsFromASTRoots;
use ComposerRequireChecker\DefinedSymbolsLocator\LocateDefinedSymbolsFromComposerRuntimeApi;
Expand All @@ -24,19 +25,19 @@
use PhpParser\Lexer;
use PhpParser\ParserFactory;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Webmozart\Assert\Assert;

use function array_combine;
use function array_diff;
use function array_map;
use function array_merge;
use function count;
use function dirname;
use function gettype;
use function implode;
use function is_string;
use function realpath;
use function sprintf;
Expand Down Expand Up @@ -153,20 +154,23 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return 0;
}

$resultsWriter = new CliText($output);

$output->writeln('The following ' . count($unknownSymbols) . ' unknown symbols were found:');
$table = new Table($output);
$table->setHeaders(['Unknown Symbol', 'Guessed Dependency']);
$guesser = new DependencyGuesser($options);
foreach ($unknownSymbols as $unknownSymbol) {
$guessedDependencies = [];
foreach ($guesser($unknownSymbol) as $guessedDependency) {
$guessedDependencies[] = $guessedDependency;
}

$table->addRow([$unknownSymbol, implode("\n", $guessedDependencies)]);
}

$table->render();
$resultsWriter->write(
array_map(
static function (string $unknownSymbol) use ($guesser): array {
$guessedDependencies = [];
foreach ($guesser($unknownSymbol) as $guessedDependency) {
$guessedDependencies[] = $guessedDependency;
}

return $guessedDependencies;
},
array_combine($unknownSymbols, $unknownSymbols)
),
);

return (int) (bool) $unknownSymbols;
}
Expand Down
42 changes: 42 additions & 0 deletions src/ComposerRequireChecker/Cli/ResultsWriter/CliText.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace ComposerRequireChecker\Cli\ResultsWriter;

use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Output\OutputInterface;

use function count;
use function implode;

final class CliText implements ResultsWriter
{
private OutputInterface $output;

public function __construct(OutputInterface $output)
{
$this->output = $output;
}

/**
* {@inheritdoc}
*/
public function write(array $unknownSymbols): void
{
if (! $unknownSymbols) {
$this->output->writeln('There were no unknown symbols found.');

return;
}

$this->output->writeln('The following ' . count($unknownSymbols) . ' unknown symbols were found:');
$table = new Table($this->output);
$table->setHeaders(['Unknown Symbol', 'Guessed Dependency']);
foreach ($unknownSymbols as $unknownSymbol => $guessedDependencies) {
$table->addRow([$unknownSymbol, implode("\n", $guessedDependencies)]);
}

$table->render();
}
}
13 changes: 13 additions & 0 deletions src/ComposerRequireChecker/Cli/ResultsWriter/ResultsWriter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace ComposerRequireChecker\Cli\ResultsWriter;

interface ResultsWriter
{
/**
* @param array<array-key, string[]> $unknownSymbols the unknown symbols found
*/
public function write(array $unknownSymbols): void;
}
50 changes: 50 additions & 0 deletions test/ComposerRequireCheckerTest/Cli/ResultsWriter/CliTextTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace ComposerRequireCheckerTest\Cli\ResultsWriter;

use ComposerRequireChecker\Cli\ResultsWriter\CliText;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Output\BufferedOutput;

use const PHP_EOL;

final class CliTextTest extends TestCase
{
private CliText $writer;
private BufferedOutput $output;

protected function setUp(): void
{
parent::setUp();

$this->output = new BufferedOutput();
$this->writer = new CliText($this->output);
}

public function testWriteReportNoUnknownSymbolsFound(): void
{
$this->writer->write([]);

self::assertSame('There were no unknown symbols found.' . PHP_EOL, $this->output->fetch());
}

public function testWriteReportWithUnknownSymbols(): void
{
$this->writer->write([
'Foo' => [],
'opcache_get_status' => ['ext-opcache'],
'dummy' => ['ext-dummy', 'ext-other'],
]);

$buffer = $this->output->fetch();
self::assertStringContainsString('The following 3 unknown symbols were found:', $buffer);
self::assertStringContainsString('Foo', $buffer);
self::assertStringContainsString('| opcache_get_status', $buffer);
self::assertStringContainsString('| ext-opcache', $buffer);
self::assertStringContainsString('| dummy', $buffer);
self::assertStringContainsString('| ext-dummy', $buffer);
self::assertStringContainsString('| ext-other', $buffer);
}
}

0 comments on commit 05a6604

Please sign in to comment.