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 AbstractSnippetFormatterCommand
- Loading branch information
1 parent
c111f1b
commit f9ce665
Showing
4 changed files
with
118 additions
and
185 deletions.
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
100
...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,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; | ||
} | ||
} |
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
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