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

Commit

Permalink
[ECS] Decouple sub-package SnippetFormatter (#2174)
Browse files Browse the repository at this point in the history
* [ECS] Decouple sub-package SnippetFormatter

* [ECS] Refactor *SnippetFormatters to single SnippetFormatter

* [ECS] Decouple AbstractSnippetFormatterCommand

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
TomasVotruba and kodiakhq[bot] authored Sep 14, 2020
1 parent fab77ae commit 8e1961c
Show file tree
Hide file tree
Showing 34 changed files with 255 additions and 369 deletions.
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"Symplify\\EasyCodingStandard\\Configuration\\": "packages/easy-coding-standard/packages/configuration/src",
"Symplify\\EasyCodingStandard\\FixerRunner\\": "packages/easy-coding-standard/packages/fixer-runner/src",
"Symplify\\EasyCodingStandard\\SniffRunner\\": "packages/easy-coding-standard/packages/sniff-runner/src",
"Symplify\\EasyCodingStandard\\SnippetFormatter\\": "packages/easy-coding-standard/packages/snippet-formatter/src",
"Symplify\\EasyHydrator\\": "packages/easy-hydrator/src",
"Symplify\\EasyTesting\\": "packages/easy-testing/src",
"Symplify\\FlexLoader\\": "packages/flex-loader/src",
Expand Down Expand Up @@ -119,6 +120,7 @@
"Symplify\\EasyCodingStandard\\ChangedFilesDetector\\Tests\\": "packages/easy-coding-standard/packages/changed-files-detector/tests",
"Symplify\\EasyCodingStandard\\FixerRunner\\Tests\\": "packages/easy-coding-standard/packages/fixer-runner/tests",
"Symplify\\EasyCodingStandard\\SniffRunner\\Tests\\": "packages/easy-coding-standard/packages/sniff-runner/tests",
"Symplify\\EasyCodingStandard\\SnippetFormatter\\Tests\\": "packages/easy-coding-standard/packages/snippet-formatter/tests",
"Symplify\\EasyCodingStandard\\Tests\\": "packages/easy-coding-standard/tests",
"Symplify\\EasyHydrator\\Tests\\": "packages/easy-hydrator/tests",
"Symplify\\EasyTesting\\Tests\\": "packages/easy-testing/tests",
Expand Down
6 changes: 4 additions & 2 deletions packages/easy-coding-standard/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,17 @@
"Symplify\\EasyCodingStandard\\ChangedFilesDetector\\": "packages/changed-files-detector/src",
"Symplify\\EasyCodingStandard\\Configuration\\": "packages/configuration/src",
"Symplify\\EasyCodingStandard\\FixerRunner\\": "packages/fixer-runner/src",
"Symplify\\EasyCodingStandard\\SniffRunner\\": "packages/sniff-runner/src"
"Symplify\\EasyCodingStandard\\SniffRunner\\": "packages/sniff-runner/src",
"Symplify\\EasyCodingStandard\\SnippetFormatter\\": "packages/snippet-formatter/src"
}
},
"autoload-dev": {
"psr-4": {
"Symplify\\EasyCodingStandard\\ChangedFilesDetector\\Tests\\": "packages/changed-files-detector/tests",
"Symplify\\EasyCodingStandard\\FixerRunner\\Tests\\": "packages/fixer-runner/tests",
"Symplify\\EasyCodingStandard\\SniffRunner\\Tests\\": "packages/sniff-runner/tests",
"Symplify\\EasyCodingStandard\\Tests\\": "tests"
"Symplify\\EasyCodingStandard\\Tests\\": "tests",
"Symplify\\EasyCodingStandard\\SnippetFormatter\\Tests\\": "packages/snippet-formatter/tests"
}
},
"extra": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();

$services->defaults()
->public()
->autowire();

$services->load('Symplify\EasyCodingStandard\SnippetFormatter\\', __DIR__ . '/../src');
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?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);
$this->easyCodingStandardStyle->progressAdvance();
}

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

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

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

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

$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
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Symplify\EasyCodingStandard\SnippetFormatter\Command;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symplify\EasyCodingStandard\SnippetFormatter\ValueObject\SnippetPattern;
use Symplify\PackageBuilder\Console\Command\CommandNaming;

final class CheckHeredocNowdocCommand extends AbstractSnippetFormatterCommand
{
protected function configure(): void
{
$this->setName(CommandNaming::classToName(self::class));
$this->setDescription('Format Heredoc/Nowdoc PHP snippets in PHP files');

parent::configure();
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
return $this->doExecuteSnippetFormatterWithFileNamesAndSnippetPattern(
$input,
'*.php',
SnippetPattern::HERENOWDOC_SNIPPET_PATTERN
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Symplify\EasyCodingStandard\SnippetFormatter\Command;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symplify\EasyCodingStandard\SnippetFormatter\ValueObject\SnippetPattern;
use Symplify\PackageBuilder\Console\Command\CommandNaming;

final class CheckMarkdownCommand extends AbstractSnippetFormatterCommand
{
protected function configure(): void
{
$this->setName(CommandNaming::classToName(self::class));
$this->setDescription('Format Markdown PHP code');

parent::configure();
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
return $this->doExecuteSnippetFormatterWithFileNamesAndSnippetPattern(
$input,
'*.md',
SnippetPattern::HERENOWDOC_SNIPPET_PATTERN
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,36 @@

declare(strict_types=1);

namespace Symplify\EasyCodingStandard\Formatter;
namespace Symplify\EasyCodingStandard\SnippetFormatter\Formatter;

use Nette\Utils\Strings;
use Symplify\EasyCodingStandard\Configuration\Configuration;
use Symplify\EasyCodingStandard\Contract\RegexAwareFormatterInterface;
use Symplify\EasyCodingStandard\FixerRunner\Application\FixerFileProcessor;
use Symplify\EasyCodingStandard\Provider\CurrentParentFileInfoProvider;
use Symplify\EasyCodingStandard\SniffRunner\Application\SniffFileProcessor;
use Symplify\EasyCodingStandard\SnippetFormatter\Provider\CurrentParentFileInfoProvider;
use Symplify\SmartFileSystem\SmartFileInfo;
use Symplify\SmartFileSystem\SmartFileSystem;
use Throwable;

abstract class AbstractPHPFormatter implements RegexAwareFormatterInterface
/**
* @see \Symplify\EasyCodingStandard\SnippetFormatter\Tests\Markdown\MarkdownSnippetFormatterTest
* @see \Symplify\EasyCodingStandard\SnippetFormatter\Tests\HeredocNowdoc\HereNowDocSnippetFormatterTest
*/
final class SnippetFormatter
{
/**
* @var SmartFileSystem
*/
protected $smartFileSystem;
private $smartFileSystem;

/**
* @var FixerFileProcessor
*/
protected $fixerFileProcessor;
private $fixerFileProcessor;

/**
* @var SniffFileProcessor
*/
protected $sniffFileProcessor;

/**
* @var Configuration
*/
protected $configuration;
private $sniffFileProcessor;

/**
* @var CurrentParentFileInfoProvider
Expand All @@ -45,21 +42,19 @@ public function __construct(
SmartFileSystem $smartFileSystem,
FixerFileProcessor $fixerFileProcessor,
SniffFileProcessor $sniffFileProcessor,
Configuration $configuration,
CurrentParentFileInfoProvider $currentParentFileInfoProvider
) {
$this->smartFileSystem = $smartFileSystem;
$this->fixerFileProcessor = $fixerFileProcessor;
$this->sniffFileProcessor = $sniffFileProcessor;
$this->configuration = $configuration;
$this->currentParentFileInfoProvider = $currentParentFileInfoProvider;
}

public function format(SmartFileInfo $fileInfo): string
public function format(SmartFileInfo $fileInfo, string $snippetRegex): string
{
$this->currentParentFileInfoProvider->setParentFileInfo($fileInfo);

return (string) Strings::replace($fileInfo->getContents(), $this->provideRegex(), function ($match): string {
return (string) Strings::replace($fileInfo->getContents(), $snippetRegex, function ($match): string {
return $this->fixContentAndPreserveFormatting($match);
});
}
Expand All @@ -79,8 +74,8 @@ private function fixContent(string $content): string
$content = trim($content);
$key = md5($content);

/** @var string $temporaryFile */
$temporaryFile = sys_get_temp_dir() . '/ecs_temp/' . sprintf('php-code-%s.php', $key);
/** @var string $temporaryFilePath */
$temporaryFilePath = sys_get_temp_dir() . '/ecs_temp/' . sprintf('php-code-%s.php', $key);

$hasPreviouslyOpeningPHPTag = true;
if (! Strings::startsWith($content, '<?php')) {
Expand All @@ -90,8 +85,8 @@ private function fixContent(string $content): string

$fileContent = $content;

$this->smartFileSystem->dumpFile($temporaryFile, $fileContent);
$temporaryFileInfo = new SmartFileInfo($temporaryFile);
$this->smartFileSystem->dumpFile($temporaryFilePath, $fileContent);
$temporaryFileInfo = new SmartFileInfo($temporaryFilePath);

try {
$this->fixerFileProcessor->processFile($temporaryFileInfo);
Expand All @@ -102,7 +97,7 @@ private function fixContent(string $content): string
// Skipped parsed error when processing php temporaryFile
} finally {
// remove temporary temporaryFile
$this->smartFileSystem->remove($temporaryFile);
$this->smartFileSystem->remove($temporaryFilePath);
}

if (! $hasPreviouslyOpeningPHPTag) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Symplify\EasyCodingStandard\Provider;
namespace Symplify\EasyCodingStandard\SnippetFormatter\Provider;

use Symplify\SmartFileSystem\SmartFileInfo;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Symplify\EasyCodingStandard\SnippetFormatter\ValueObject;

final class SnippetPattern
{
/**
* @see https://regex101.com/r/4YUIu1/4
* @var string
*/
public const MARKDOWN_PHP_SNIPPET_PATTERN = '#(?<opening>\`\`\`php\s+)(?<content>[^\`\`\`|^\-\-\-\-\-]+\n)(?<closing>(\s+)?\`\`\`)#ms';

/**
* @see https://regex101.com/r/SZr0X5/12
* @var string
*/
public const HERENOWDOC_SNIPPET_PATTERN = '#(?<opening><<<(\'?([A-Z]+)\'?|\"?([A-Z]+)\"?)\s+)(?<content>[^\3|\4]+)(?<closing>(\s+)?\3|\4)#msU';
}
Loading

0 comments on commit 8e1961c

Please sign in to comment.