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

Commit

Permalink
[ECS] Decouple AbstractSnippetFormatterCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Sep 14, 2020
1 parent c111f1b commit f9ce665
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 185 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

declare(strict_types=1);

namespace Symplify\EasyCodingStandard\SnippetFormatter\Command;

use Symfony\Component\Console\Input\InputInterface;
use Symplify\EasyCodingStandard\Console\Command\AbstractCheckCommand;
use Symplify\EasyCodingStandard\SnippetFormatter\Formatter\SnippetFormatter;
use Symplify\PackageBuilder\Console\ShellCode;
use Symplify\SmartFileSystem\Finder\SmartFinder;
use Symplify\SmartFileSystem\SmartFileInfo;
use Symplify\SmartFileSystem\SmartFileSystem;

abstract class AbstractSnippetFormatterCommand extends AbstractCheckCommand
{
/**
* @var SnippetFormatter
*/
private $snippetFormatter;

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

/**
* @var SmartFinder
*/
private $smartFinder;

/**
* @required
*/
public function autowireAbstractSnippetFormatterCommand(
SnippetFormatter $snippetFormatter,
SmartFileSystem $smartFileSystem,
SmartFinder $smartFinder
): void {
$this->snippetFormatter = $snippetFormatter;
$this->smartFileSystem = $smartFileSystem;
$this->smartFinder = $smartFinder;
}

protected function doExecuteSnippetFormatterWithFileNamesAndSnippetPattern(
InputInterface $input,
string $fileNames,
string $snippetPattern
): int {
$this->configuration->resolveFromInput($input);

$sources = $this->configuration->getSources();
$phpFileInfos = $this->smartFinder->find($sources, $fileNames);

$fileCount = count($phpFileInfos);

if ($fileCount === 0) {
return $this->printNoFilesFoundWarningAndExitSuccess($sources, $fileNames);
}

$this->easyCodingStandardStyle->progressStart($fileCount);
foreach ($phpFileInfos as $phpFileInfo) {
$this->processFileInfoWithPattern($phpFileInfo, $snippetPattern);
}

return $this->reportProcessedFiles($fileCount);
}

private function processFileInfoWithPattern(SmartFileInfo $phpFileInfo, string $snippetPattern): void
{
$fixedContent = $this->snippetFormatter->format($phpFileInfo, $snippetPattern);
$this->easyCodingStandardStyle->progressAdvance();

if ($phpFileInfo->getContents() === $fixedContent) {
// nothing has changed
return;
}

if ($this->configuration->isFixer()) {
$this->smartFileSystem->dumpFile($phpFileInfo->getPathname(), (string) $fixedContent);
}
}

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

$this->easyCodingStandardStyle->warning($warningMessage);

return ShellCode::SUCCESS;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,60 +6,11 @@

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symplify\EasyCodingStandard\Configuration\Configuration;
use Symplify\EasyCodingStandard\Console\Command\AbstractCheckCommand;
use Symplify\EasyCodingStandard\Console\Style\EasyCodingStandardStyle;
use Symplify\EasyCodingStandard\SnippetFormatter\Formatter\SnippetFormatter;
use Symplify\EasyCodingStandard\SnippetFormatter\ValueObject\SnippetPattern;
use Symplify\PackageBuilder\Console\Command\CommandNaming;
use Symplify\PackageBuilder\Console\ShellCode;
use Symplify\SmartFileSystem\Finder\SmartFinder;
use Symplify\SmartFileSystem\SmartFileInfo;
use Symplify\SmartFileSystem\SmartFileSystem;

final class CheckHeredocNowdocCommand extends AbstractCheckCommand
final class CheckHeredocNowdocCommand extends AbstractSnippetFormatterCommand
{
/**
* @var Configuration
*/
protected $configuration;

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

/**
* @var EasyCodingStandardStyle
*/
private $easyCodingStandardStyle;

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

/**
* @var SmartFinder
*/
private $smartFinder;

public function __construct(
SmartFileSystem $smartFileSystem,
EasyCodingStandardStyle $easyCodingStandardStyle,
SnippetFormatter $snippetFormatter,
Configuration $configuration,
SmartFinder $smartFinder
) {
$this->smartFileSystem = $smartFileSystem;
$this->easyCodingStandardStyle = $easyCodingStandardStyle;
$this->configuration = $configuration;
$this->snippetFormatter = $snippetFormatter;
$this->smartFinder = $smartFinder;

parent::__construct();
}

protected function configure(): void
{
$this->setName(CommandNaming::classToName(self::class));
Expand All @@ -70,50 +21,10 @@ protected function configure(): void

protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->configuration->resolveFromInput($input);

$sources = $this->configuration->getSources();
$phpFileInfos = $this->smartFinder->find($sources, '*.php');

$fileCount = count($phpFileInfos);

if ($fileCount > 0) {
$this->easyCodingStandardStyle->progressStart($fileCount);

foreach ($phpFileInfos as $phpFileInfo) {
$this->processPHPFileInfo($phpFileInfo);
}
} else {
return $this->printFileWarningAndExitSuccess($sources);
}

return $this->reportProcessedFiles($fileCount);
}

private function printFileWarningAndExitSuccess(array $sources): int
{
$warningMessage = sprintf(
'No PHP files found in "%s" paths.%sCheck CLI arguments or "Option::PATHS" parameter in "ecs.php" config file',
implode('", ', $sources),
PHP_EOL
return $this->doExecuteSnippetFormatterWithFileNamesAndSnippetPattern(
$input,
'*.php',
SnippetPattern::HERENOWDOC_SNIPPET_PATTERN
);
$this->easyCodingStandardStyle->warning($warningMessage);

return ShellCode::SUCCESS;
}

private function processPHPFileInfo(SmartFileInfo $phpFileInfo): void
{
$fixedContent = $this->snippetFormatter->format($phpFileInfo, SnippetPattern::HERENOWDOC_SNIPPET_PATTERN);
$this->easyCodingStandardStyle->progressAdvance();

if ($phpFileInfo->getContents() === $fixedContent) {
// nothing has changed
return;
}

if ($this->configuration->isFixer()) {
$this->smartFileSystem->dumpFile($phpFileInfo->getPathname(), (string) $fixedContent);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,55 +6,11 @@

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symplify\EasyCodingStandard\Console\Command\AbstractCheckCommand;
use Symplify\EasyCodingStandard\Console\Style\EasyCodingStandardStyle;
use Symplify\EasyCodingStandard\SnippetFormatter\Formatter\SnippetFormatter;
use Symplify\EasyCodingStandard\SnippetFormatter\ValueObject\SnippetPattern;
use Symplify\PackageBuilder\Console\Command\CommandNaming;
use Symplify\PackageBuilder\Console\ShellCode;
use Symplify\SmartFileSystem\Finder\SmartFinder;
use Symplify\SmartFileSystem\SmartFileInfo;
use Symplify\SmartFileSystem\SmartFileSystem;

/**
* @todo refactor to generic parent AbstractSnippetFormatterCommand
*/
final class CheckMarkdownCommand extends AbstractCheckCommand
final class CheckMarkdownCommand extends AbstractSnippetFormatterCommand
{
/**
* @var SmartFileSystem
*/
private $smartFileSystem;

/**
* @var EasyCodingStandardStyle
*/
private $easyCodingStandardStyle;

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

/**
* @var SmartFinder
*/
private $smartFinder;

public function __construct(
SmartFileSystem $smartFileSystem,
EasyCodingStandardStyle $easyCodingStandardStyle,
SnippetFormatter $snippetFormatter,
SmartFinder $smartFinder
) {
$this->smartFileSystem = $smartFileSystem;
$this->easyCodingStandardStyle = $easyCodingStandardStyle;
$this->snippetFormatter = $snippetFormatter;
$this->smartFinder = $smartFinder;

parent::__construct();
}

protected function configure(): void
{
$this->setName(CommandNaming::classToName(self::class));
Expand All @@ -65,52 +21,10 @@ protected function configure(): void

protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->configuration->resolveFromInput($input);

$sources = $this->configuration->getSources();
$markdownFileInfos = $this->smartFinder->find($sources, '*.md');

$fileCount = count($markdownFileInfos);
if ($fileCount > 0) {
$this->easyCodingStandardStyle->progressStart($fileCount);

foreach ($markdownFileInfos as $markdownFileInfo) {
$this->processMarkdownFileInfo($markdownFileInfo);
}
} else {
return $this->printNoFilesWarningAndExistSuccess($sources);
}

return $this->reportProcessedFiles($fileCount);
}

private function processMarkdownFileInfo(SmartFileInfo $markdownFileInfo): void
{
$fixedContent = $this->snippetFormatter->format(
$markdownFileInfo,
SnippetPattern::MARKDOWN_PHP_SNIPPET_PATTERN
);
$this->easyCodingStandardStyle->progressAdvance();

if ($markdownFileInfo->getContents() === $fixedContent) {
// nothing has changed
return;
}

if ($this->configuration->isFixer()) {
$this->smartFileSystem->dumpFile($markdownFileInfo->getPathname(), (string) $fixedContent);
}
}

private function printNoFilesWarningAndExistSuccess(array $sources): int
{
$warningMessage = sprintf(
'No Markdown files found in "%s" paths.%sCheck CLI arguments or "Option::PATHS" parameter in "ecs.php" config file',
implode('", ', $sources),
PHP_EOL
return $this->doExecuteSnippetFormatterWithFileNamesAndSnippetPattern(
$input,
'*.md',
SnippetPattern::HERENOWDOC_SNIPPET_PATTERN
);
$this->easyCodingStandardStyle->warning($warningMessage);

return ShellCode::SUCCESS;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Symplify\EasyCodingStandard\Configuration\Exception\NoCheckersLoadedException;
use Symplify\EasyCodingStandard\Console\Output\ConsoleOutputFormatter;
use Symplify\EasyCodingStandard\Console\Output\OutputFormatterCollector;
use Symplify\EasyCodingStandard\Console\Style\EasyCodingStandardStyle;
use Symplify\EasyCodingStandard\ValueObject\Option;

abstract class AbstractCheckCommand extends Command
Expand All @@ -21,6 +22,11 @@ abstract class AbstractCheckCommand extends Command
*/
protected $configuration;

/**
* @var EasyCodingStandardStyle
*/
protected $easyCodingStandardStyle;

/**
* @var EasyCodingStandardApplication
*/
Expand All @@ -37,10 +43,12 @@ abstract class AbstractCheckCommand extends Command
public function autowireAbstractCheckCommand(
Configuration $configuration,
EasyCodingStandardApplication $easyCodingStandardApplication,
EasyCodingStandardStyle $easyCodingStandardStyle,
OutputFormatterCollector $outputFormatterCollector
): void {
$this->configuration = $configuration;
$this->easyCodingStandardApplication = $easyCodingStandardApplication;
$this->easyCodingStandardStyle = $easyCodingStandardStyle;
$this->outputFormatterCollector = $outputFormatterCollector;
}

Expand Down

0 comments on commit f9ce665

Please sign in to comment.