mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-19 14:27:14 +01:00
add github action check
This commit is contained in:
parent
d29c4ede57
commit
6291b39ffa
22
.github/workflows/validate_doctrine_annotation.yaml
vendored
Normal file
22
.github/workflows/validate_doctrine_annotation.yaml
vendored
Normal file
@ -0,0 +1,22 @@
|
||||
# related PR: https://github.com/rectorphp/rector/pull/3134
|
||||
name: Validate Doctine Annotation
|
||||
|
||||
on:
|
||||
pull_request: null
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
|
||||
jobs:
|
||||
validate_doctrine_annotation:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: shivammathur/setup-php@v1
|
||||
with:
|
||||
php-version: 7.4
|
||||
coverage: none # disable xdebug, pcov
|
||||
extensions: "intl"
|
||||
- run: composer install --no-progress
|
||||
- run: |
|
||||
bin/rector sync-annotation-parser --dry-run
|
@ -26,7 +26,12 @@ final class AssertTypeTagValueNode extends AbstractConstraintTagValueNode implem
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
return '(' . $this->type . ')';
|
||||
$contentItems = [];
|
||||
$contentItems['type'] = $this->type;
|
||||
|
||||
$contentItems = $this->appendGroups($contentItems);
|
||||
|
||||
return $this->printContentItems($contentItems);
|
||||
}
|
||||
|
||||
public function getShortName(): string
|
||||
|
@ -28,7 +28,9 @@ abstract class AbstractConstraintTagValueNode extends AbstractTagValueNode
|
||||
}
|
||||
|
||||
if (count($this->groups) === 1) {
|
||||
$contentItems['groups'] = sprintf('groups=%s', $this->groups[0]);
|
||||
if ($this->groups !== ['Default']) {
|
||||
$contentItems['groups'] = sprintf('groups=%s', $this->groups[0]);
|
||||
}
|
||||
} else {
|
||||
$contentItems['groups'] = sprintf('groups=%s', $this->printArrayItem($this->groups));
|
||||
}
|
||||
|
@ -4,10 +4,11 @@ declare(strict_types=1);
|
||||
|
||||
namespace Rector\Utils\DoctrineAnnotationParserSyncer\Command;
|
||||
|
||||
use Rector\Utils\DoctrineAnnotationParserSyncer\FileSyncer\AnnotationReaderClassSyncer;
|
||||
use Rector\Utils\DoctrineAnnotationParserSyncer\FileSyncer\DocParserClassSyncer;
|
||||
use Rector\Core\Configuration\Option;
|
||||
use Rector\Utils\DoctrineAnnotationParserSyncer\Contract\ClassSyncerInterface;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
use Symplify\PackageBuilder\Console\Command\CommandNaming;
|
||||
@ -21,37 +22,55 @@ final class SyncAnnotationParserCommand extends Command
|
||||
private $symfonyStyle;
|
||||
|
||||
/**
|
||||
* @var DocParserClassSyncer
|
||||
* @var ClassSyncerInterface[]
|
||||
*/
|
||||
private $docParserClassSyncer;
|
||||
private $classSyncers = [];
|
||||
|
||||
/**
|
||||
* @var AnnotationReaderClassSyncer
|
||||
* @param ClassSyncerInterface[] $classSyncers
|
||||
*/
|
||||
private $annotationReaderClassSyncer;
|
||||
|
||||
public function __construct(
|
||||
SymfonyStyle $symfonyStyle,
|
||||
DocParserClassSyncer $docParserClassSyncer,
|
||||
AnnotationReaderClassSyncer $annotationReaderClassSyncer
|
||||
) {
|
||||
public function __construct(array $classSyncers, SymfonyStyle $symfonyStyle)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$this->symfonyStyle = $symfonyStyle;
|
||||
$this->docParserClassSyncer = $docParserClassSyncer;
|
||||
$this->annotationReaderClassSyncer = $annotationReaderClassSyncer;
|
||||
$this->classSyncers = $classSyncers;
|
||||
}
|
||||
|
||||
protected function configure(): void
|
||||
{
|
||||
$this->setName(CommandNaming::classToName(self::class));
|
||||
$this->setDescription('[DOC] Generate value-preserving DocParser from doctrine/annotation');
|
||||
|
||||
$this->addOption(
|
||||
Option::OPTION_DRY_RUN,
|
||||
'n',
|
||||
InputOption::VALUE_NONE,
|
||||
'See diff of changes, do not save them to files.'
|
||||
);
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$this->docParserClassSyncer->sync();
|
||||
$this->annotationReaderClassSyncer->sync();
|
||||
$dryRun = (bool) $input->getOption(Option::OPTION_DRY_RUN);
|
||||
|
||||
foreach ($this->classSyncers as $classSyncer) {
|
||||
$isSuccess = $classSyncer->sync($dryRun);
|
||||
if (! $isSuccess) {
|
||||
$message = sprintf('File "%s" has changed, regenerate it: ', $classSyncer->getTargetFilePath());
|
||||
$this->symfonyStyle->error($message);
|
||||
|
||||
return ShellCode::ERROR;
|
||||
}
|
||||
|
||||
$message = sprintf(
|
||||
'Original "%s" was changed and refactored to "%s"',
|
||||
$classSyncer->getSourceFilePath(),
|
||||
$classSyncer->getTargetFilePath()
|
||||
);
|
||||
|
||||
$this->symfonyStyle->note($message);
|
||||
}
|
||||
|
||||
$this->symfonyStyle->success('Done');
|
||||
|
||||
|
@ -10,5 +10,5 @@ interface ClassSyncerInterface
|
||||
|
||||
public function getTargetFilePath(): string;
|
||||
|
||||
public function sync(): void;
|
||||
public function sync(bool $isDryRun): bool;
|
||||
}
|
||||
|
@ -9,16 +9,10 @@ use PhpParser\Node;
|
||||
use Rector\Core\PhpParser\Printer\BetterStandardPrinter;
|
||||
use Rector\FileSystemRector\Parser\FileInfoParser;
|
||||
use Rector\Utils\DoctrineAnnotationParserSyncer\Contract\ClassSyncerInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
abstract class AbstractClassSyncer implements ClassSyncerInterface
|
||||
{
|
||||
/**
|
||||
* @var SymfonyStyle
|
||||
*/
|
||||
protected $symfonyStyle;
|
||||
|
||||
/**
|
||||
* @var BetterStandardPrinter
|
||||
*/
|
||||
@ -34,12 +28,10 @@ abstract class AbstractClassSyncer implements ClassSyncerInterface
|
||||
*/
|
||||
public function autowireAbstractClassSyncer(
|
||||
BetterStandardPrinter $betterStandardPrinter,
|
||||
FileInfoParser $fileInfoParser,
|
||||
SymfonyStyle $symfonyStyle
|
||||
FileInfoParser $fileInfoParser
|
||||
): void {
|
||||
$this->betterStandardPrinter = $betterStandardPrinter;
|
||||
$this->fileInfoParser = $fileInfoParser;
|
||||
$this->symfonyStyle = $symfonyStyle;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -57,19 +49,36 @@ abstract class AbstractClassSyncer implements ClassSyncerInterface
|
||||
*/
|
||||
protected function printNodesToPath(array $nodes): void
|
||||
{
|
||||
$printedContent = $this->betterStandardPrinter->prettyPrint($nodes);
|
||||
$printedContent = '<?php' . PHP_EOL . PHP_EOL . $printedContent;
|
||||
$printedContent = $this->printNodesToString($nodes);
|
||||
|
||||
FileSystem::write($this->getTargetFilePath(), $printedContent);
|
||||
}
|
||||
|
||||
protected function reportChange(): void
|
||||
/**
|
||||
* @param Node[] $nodes
|
||||
*/
|
||||
protected function printNodesToString(array $nodes): string
|
||||
{
|
||||
$message = sprintf(
|
||||
'Original "%s" was changed and refactored to "%s"',
|
||||
$this->getSourceFilePath(),
|
||||
$this->getTargetFilePath()
|
||||
);
|
||||
$this->symfonyStyle->note($message);
|
||||
$printedContent = $this->betterStandardPrinter->prettyPrint($nodes);
|
||||
|
||||
return '<?php' . PHP_EOL . PHP_EOL . $printedContent;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Node[] $nodes
|
||||
*/
|
||||
protected function hasContentChanged(array $nodes): bool
|
||||
{
|
||||
$finalContent = $this->printNodesToString($nodes);
|
||||
|
||||
// nothing to validate agains
|
||||
if (! file_exists($this->getTargetFilePath())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$currentContent = FileSystem::read($this->getTargetFilePath());
|
||||
|
||||
// has content changed
|
||||
return $finalContent !== $currentContent;
|
||||
}
|
||||
}
|
||||
|
@ -30,13 +30,17 @@ final class AnnotationReaderClassSyncer extends AbstractClassSyncer
|
||||
$this->nodeTraverser->addVisitor($assignNewDocParserRector);
|
||||
}
|
||||
|
||||
public function sync(): void
|
||||
public function sync(bool $isDryRun): bool
|
||||
{
|
||||
$nodes = $this->getFileNodes();
|
||||
$changedNodes = $this->nodeTraverser->traverse($nodes);
|
||||
$this->printNodesToPath($changedNodes);
|
||||
|
||||
$this->reportChange();
|
||||
if ($isDryRun) {
|
||||
return ! $this->hasContentChanged($nodes);
|
||||
}
|
||||
|
||||
$this->printNodesToPath($changedNodes);
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getSourceFilePath(): string
|
||||
|
@ -27,13 +27,17 @@ final class DocParserClassSyncer extends AbstractClassSyncer
|
||||
$this->nodeTraverser->addVisitor($replaceDirWithRealPathRector);
|
||||
}
|
||||
|
||||
public function sync(): void
|
||||
public function sync(bool $isDryRun): bool
|
||||
{
|
||||
$nodes = $this->getFileNodes();
|
||||
$changedNodes = $this->nodeTraverser->traverse($nodes);
|
||||
$this->printNodesToPath($changedNodes);
|
||||
|
||||
$this->reportChange();
|
||||
if ($isDryRun) {
|
||||
return ! $this->hasContentChanged($nodes);
|
||||
}
|
||||
|
||||
$this->printNodesToPath($changedNodes);
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getSourceFilePath(): string
|
||||
|
Loading…
x
Reference in New Issue
Block a user