Skip to content
This repository has been archived by the owner on Dec 3, 2023. It is now read-only.

Commit

Permalink
[ECS] Refactor bstractSnippetFormatterCommand to composition
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Feb 22, 2021
1 parent 24c8481 commit d537cb9
Show file tree
Hide file tree
Showing 11 changed files with 249 additions and 159 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

declare(strict_types=1);

namespace Symplify\EasyCodingStandard\SnippetFormatter\Application;

use Symfony\Component\Console\Style\SymfonyStyle;
use Symplify\EasyCodingStandard\Configuration\Configuration;
use Symplify\EasyCodingStandard\Reporter\ProcessedFileReporter;
use Symplify\EasyCodingStandard\SnippetFormatter\Formatter\SnippetFormatter;
use Symplify\EasyCodingStandard\SnippetFormatter\Reporter\SnippetReporter;
use Symplify\PackageBuilder\Console\ShellCode;
use Symplify\SmartFileSystem\SmartFileInfo;
use Symplify\SmartFileSystem\SmartFileSystem;

final class SnippetFormatterApplication
{
/**
* @var Configuration
*/
private $configuration;

/**
* @var SnippetReporter
*/
private $snippetReporter;

/**
* @var SnippetFormatter
*/
private $snippetFormatter;

/**
* @var SmartFileSystem
*/
private $smartFileSystem;

/**
* @var SymfonyStyle
*/
private $symfonyStyle;

/**
* @var ProcessedFileReporter
*/
private $processedFileReporter;

public function __construct(
Configuration $configuration,
SnippetReporter $snippetReporter,
SnippetFormatter $snippetFormatter,
SmartFileSystem $smartFileSystem,
SymfonyStyle $symfonyStyle,
ProcessedFileReporter $processedFileReporter
) {
$this->configuration = $configuration;
$this->snippetReporter = $snippetReporter;
$this->snippetFormatter = $snippetFormatter;
$this->smartFileSystem = $smartFileSystem;
$this->symfonyStyle = $symfonyStyle;
$this->processedFileReporter = $processedFileReporter;
}

/**
* @param SmartFileInfo[] $fileInfos
*/
public function processFileInfosWithSnippetPattern(
Configuration $configuration,
array $fileInfos,
string $snippetPattern,
string $kind
): int {
$sources = $configuration->getSources();

$fileCount = count($fileInfos);
if ($fileCount === 0) {
$this->snippetReporter->reportNoFilesFound($sources);
return ShellCode::SUCCESS;
}

$this->symfonyStyle->progressStart($fileCount);
foreach ($fileInfos as $fileInfo) {
$this->processFileInfoWithPattern($fileInfo, $snippetPattern, $kind);
$this->symfonyStyle->progressAdvance();
}

return $this->processedFileReporter->report($fileCount);
}

private function processFileInfoWithPattern(SmartFileInfo $phpFileInfo, string $snippetPattern, string $kind): void
{
$fixedContent = $this->snippetFormatter->format($phpFileInfo, $snippetPattern, $kind);
if ($phpFileInfo->getContents() === $fixedContent) {
// nothing has changed
return;
}

if (! $this->configuration->isFixer()) {
return;
}

$this->smartFileSystem->dumpFile($phpFileInfo->getPathname(), $fixedContent);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,24 @@

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symplify\EasyCodingStandard\Console\Command\AbstractCheckCommand;
use Symplify\EasyCodingStandard\SnippetFormatter\Application\SnippetFormatterApplication;
use Symplify\EasyCodingStandard\SnippetFormatter\ValueObject\SnippetPattern;

final class CheckHeredocNowdocCommand extends AbstractSnippetFormatterCommand
final class CheckHeredocNowdocCommand extends AbstractCheckCommand
{
/**
* @var SnippetFormatterApplication
*/
private $snippetFormatterApplication;

public function __construct(SnippetFormatterApplication $snippetFormatterApplication)
{
$this->snippetFormatterApplication = $snippetFormatterApplication;

parent::__construct();
}

protected function configure(): void
{
$this->setDescription('Format Heredoc/Nowdoc PHP snippets in PHP files');
Expand All @@ -19,9 +33,13 @@ protected function configure(): void

protected function execute(InputInterface $input, OutputInterface $output): int
{
return $this->doExecuteSnippetFormatterWithFileNamesAndSnippetPattern(
$input,
'*.php',
$this->configuration->resolveFromInput($input);
$sources = $this->configuration->getSources();
$phpFileInfos = $this->smartFinder->find($sources, '*.php', ['Fixture']);

return $this->snippetFormatterApplication->processFileInfosWithSnippetPattern(
$this->configuration,
$phpFileInfos,
SnippetPattern::HERENOWDOC_SNIPPET_REGEX,
'heredocnowdox'
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,24 @@

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symplify\EasyCodingStandard\Console\Command\AbstractCheckCommand;
use Symplify\EasyCodingStandard\SnippetFormatter\Application\SnippetFormatterApplication;
use Symplify\EasyCodingStandard\SnippetFormatter\ValueObject\SnippetPattern;

final class CheckMarkdownCommand extends AbstractSnippetFormatterCommand
final class CheckMarkdownCommand extends AbstractCheckCommand
{
/**
* @var SnippetFormatterApplication
*/
private $snippetFormatterApplication;

public function __construct(SnippetFormatterApplication $snippetFormatterApplication)
{
$this->snippetFormatterApplication = $snippetFormatterApplication;

parent::__construct();
}

protected function configure(): void
{
$this->setDescription('Format Markdown PHP code');
Expand All @@ -19,9 +33,13 @@ protected function configure(): void

protected function execute(InputInterface $input, OutputInterface $output): int
{
return $this->doExecuteSnippetFormatterWithFileNamesAndSnippetPattern(
$input,
'*.md',
$this->configuration->resolveFromInput($input);
$sources = $this->configuration->getSources();
$phpFileInfos = $this->smartFinder->find($sources, '*.php', ['Fixture']);

return $this->snippetFormatterApplication->processFileInfosWithSnippetPattern(
$this->configuration,
$phpFileInfos,
SnippetPattern::MARKDOWN_PHP_SNIPPET_REGEX,
'markdown'
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Symplify\EasyCodingStandard\SnippetFormatter\Reporter;

use Symfony\Component\Console\Style\SymfonyStyle;

final class SnippetReporter
{
/**
* @var SymfonyStyle
*/
private $symfonyStyle;

public function __construct(SymfonyStyle $symfonyStyle)
{
$this->symfonyStyle = $symfonyStyle;
}

/**
* @param string[] $sources
*/
public function reportNoFilesFound(array $sources): void
{
$message = sprintf(
'No files found in "%s" paths.%sCheck CLI arguments or "Option::PATHS" parameter in "ecs.php" config file',
implode('", ', $sources),
PHP_EOL
);

$this->symfonyStyle->warning($message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
use Symplify\EasyCodingStandard\Application\EasyCodingStandardApplication;
use Symplify\EasyCodingStandard\Configuration\Configuration;
use Symplify\EasyCodingStandard\Console\Output\ConsoleOutputFormatter;
use Symplify\EasyCodingStandard\Console\Output\OutputFormatterCollector;
use Symplify\EasyCodingStandard\Console\Style\EasyCodingStandardStyle;
use Symplify\EasyCodingStandard\Error\ErrorAndDiffCollector;
use Symplify\EasyCodingStandard\Error\ErrorAndDiffResultFactory;
use Symplify\EasyCodingStandard\Guard\LoadedCheckersGuard;
use Symplify\EasyCodingStandard\ValueObject\Option;
use Symplify\PackageBuilder\Console\Command\AbstractSymplifyCommand;
Expand All @@ -36,21 +33,6 @@ abstract class AbstractCheckCommand extends AbstractSymplifyCommand
*/
protected $easyCodingStandardApplication;

/**
* @var OutputFormatterCollector
*/
private $outputFormatterCollector;

/**
* @var ErrorAndDiffCollector
*/
private $errorAndDiffCollector;

/**
* @var ErrorAndDiffResultFactory
*/
private $errorAndDiffResultFactory;

/**
* @var LoadedCheckersGuard
*/
Expand All @@ -63,17 +45,11 @@ public function autowireAbstractCheckCommand(
Configuration $configuration,
EasyCodingStandardApplication $easyCodingStandardApplication,
EasyCodingStandardStyle $easyCodingStandardStyle,
OutputFormatterCollector $outputFormatterCollector,
ErrorAndDiffCollector $errorAndDiffCollector,
ErrorAndDiffResultFactory $errorAndDiffResultFactory,
LoadedCheckersGuard $loadedCheckersGuard
): void {
$this->configuration = $configuration;
$this->easyCodingStandardApplication = $easyCodingStandardApplication;
$this->easyCodingStandardStyle = $easyCodingStandardStyle;
$this->outputFormatterCollector = $outputFormatterCollector;
$this->errorAndDiffCollector = $errorAndDiffCollector;
$this->errorAndDiffResultFactory = $errorAndDiffResultFactory;
$this->loadedCheckersGuard = $loadedCheckersGuard;
}

Expand Down Expand Up @@ -124,13 +100,4 @@ protected function initialize(InputInterface $input, OutputInterface $output): v
{
$this->loadedCheckersGuard->ensureSomeCheckersAreRegistered();
}

protected function reportProcessedFiles(int $processedFileCount): int
{
$outputFormat = $this->configuration->getOutputFormat();
$outputFormatter = $this->outputFormatterCollector->getByName($outputFormat);

$errorAndDiffResult = $this->errorAndDiffResultFactory->create($this->errorAndDiffCollector);
return $outputFormatter->report($errorAndDiffResult, $processedFileCount);
}
}
Loading

0 comments on commit d537cb9

Please sign in to comment.