add github action check

This commit is contained in:
TomasVotruba 2020-04-04 22:25:06 +02:00
parent d29c4ede57
commit 6291b39ffa
8 changed files with 108 additions and 43 deletions

View 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

View File

@ -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

View File

@ -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));
}

View File

@ -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');

View File

@ -10,5 +10,5 @@ interface ClassSyncerInterface
public function getTargetFilePath(): string;
public function sync(): void;
public function sync(bool $isDryRun): bool;
}

View File

@ -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;
}
}

View File

@ -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

View File

@ -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