This repository has been archived by the owner on Dec 3, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ECS] Decouple sub-package SnippetFormatter (#2174)
* [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
1 parent
fab77ae
commit 8e1961c
Showing
34 changed files
with
255 additions
and
369 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
packages/easy-coding-standard/packages/snippet-formatter/config/services.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}; |
102 changes: 102 additions & 0 deletions
102
...oding-standard/packages/snippet-formatter/src/Command/AbstractSnippetFormatterCommand.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...easy-coding-standard/packages/snippet-formatter/src/Command/CheckHeredocNowdocCommand.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...ages/easy-coding-standard/packages/snippet-formatter/src/Command/CheckMarkdownCommand.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
packages/easy-coding-standard/packages/snippet-formatter/src/ValueObject/SnippetPattern.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.