Skip to content

Commit

Permalink
[DI] Autowire annotation to attribute mapper via tags (#4190)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jun 12, 2023
1 parent 7a61135 commit 21f8014
Show file tree
Hide file tree
Showing 23 changed files with 26 additions and 570 deletions.
5 changes: 5 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@
use Rector\NodeTypeResolver\Reflection\BetterReflection\SourceLocator\IntermediateSourceLocator;
use Rector\NodeTypeResolver\Reflection\BetterReflection\SourceLocatorProvider\DynamicSourceLocatorProvider;
use Rector\Parallel\WorkerRunner;
use Rector\PhpAttribute\AnnotationToAttributeMapper;
use Rector\PhpAttribute\Contract\AnnotationToAttributeMapperInterface;
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
use Rector\PhpDocParser\PhpParser\SmartPhpParser;
use Rector\PhpDocParser\PhpParser\SmartPhpParserFactory;
Expand Down Expand Up @@ -325,4 +327,7 @@

$services->set(WorkerRunner::class)
->arg('$fileProcessors', tagged_iterator(FileProcessorInterface::class));

$services->set(AnnotationToAttributeMapper::class)
->arg('$annotationToAttributeMappers', tagged_iterator(AnnotationToAttributeMapperInterface::class));
};
12 changes: 2 additions & 10 deletions easy-ci.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,33 @@
declare(strict_types=1);

use Rector\BetterPhpDocParser\Contract\PhpDocParser\PhpDocNodeDecoratorInterface;
use Rector\BetterPhpDocParser\PhpDoc\ArrayItemNode;
use Rector\CodingStyle\Contract\ClassNameImport\ClassNameImportSkipVoterInterface;
use Rector\Core\Contract\Console\OutputStyleInterface;
use Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface;
use Rector\Core\Contract\PHPStan\Reflection\TypeToCallReflectionResolver\TypeToCallReflectionResolverInterface;
use Rector\Core\Contract\Processor\FileProcessorInterface;
use Rector\Core\Contract\Rector\RectorInterface;
use Rector\Core\NodeManipulator\MethodCallManipulator;
use Rector\FileSystemRector\Parser\FileInfoParser;
use Rector\NodeNameResolver\Contract\NodeNameResolverInterface;
use Rector\NodeTypeResolver\Contract\NodeTypeResolverInterface;
use Rector\NodeTypeResolver\DependencyInjection\PHPStanServicesFactory;
use Rector\NodeTypeResolver\PHPStan\Scope\Contract\NodeVisitor\ScopeResolverNodeVisitorInterface;
use Rector\NodeTypeResolver\Reflection\BetterReflection\RectorBetterReflectionSourceLocatorFactory;
use Rector\Php80\Contract\AttributeDecoratorInterface;
use Rector\PhpAttribute\Contract\AnnotationToAttributeMapperInterface;
use Rector\PhpDocParser\PhpDocParser\PhpDocNodeVisitor\AbstractPhpDocNodeVisitor;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
use Rector\ReadWrite\Contract\ParentNodeReadAnalyzerInterface;
use Rector\ReadWrite\Contract\ReadNodeAnalyzerInterface;
use Rector\Set\Contract\SetListInterface;
use Rector\StaticTypeMapper\Contract\PhpDocParser\PhpDocTypeMapperInterface;
use Rector\StaticTypeMapper\Contract\PhpParser\PhpParserNodeMapperInterface;
use Rector\Testing\PHPUnit\AbstractTestCase;
use Rector\TypeDeclaration\Contract\PHPStan\TypeWithClassTypeSpecifierInterface;
use Symfony\Component\Console\Application;
use Symplify\EasyCI\Config\EasyCIConfig;

return static function (EasyCIConfig $easyCiConfig): void {
$easyCiConfig->typesToSkip([
AttributeDecoratorInterface::class,
ArrayItemNode::class,
AnnotationToAttributeMapperInterface::class,
PhpDocNodeDecoratorInterface::class,
Application::class,
RectorInterface::class,
TypeToCallReflectionResolverInterface::class,
FileProcessorInterface::class,
Expand All @@ -46,13 +40,11 @@
AbstractPhpDocNodeVisitor::class,
NodeNameResolverInterface::class,
NodeTypeResolverInterface::class,
ReadNodeAnalyzerInterface::class,
SetListInterface::class,
RectorBetterReflectionSourceLocatorFactory::class,
AbstractTestCase::class,
PHPStanServicesFactory::class,
OutputStyleInterface::class,
MethodCallManipulator::class,
// fix later - rector-symfony
// used in tests
FileInfoParser::class,
Expand Down

This file was deleted.

36 changes: 7 additions & 29 deletions packages/PhpAttribute/AnnotationToAttributeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,9 @@
use Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode;
use Rector\BetterPhpDocParser\PhpDoc\StringNode;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpAttribute\AnnotationToAttributeMapper\ArrayAnnotationToAttributeMapper;
use Rector\PhpAttribute\AnnotationToAttributeMapper\ArrayItemNodeAnnotationToAttributeMapper;
use Rector\PhpAttribute\AnnotationToAttributeMapper\ClassConstFetchAnnotationToAttributeMapper;
use Rector\PhpAttribute\AnnotationToAttributeMapper\ConstExprNodeAnnotationToAttributeMapper;
use Rector\PhpAttribute\AnnotationToAttributeMapper\CurlyListNodeAnnotationToAttributeMapper;
use Rector\PhpAttribute\AnnotationToAttributeMapper\DoctrineAnnotationAnnotationToAttributeMapper;
use Rector\PhpAttribute\AnnotationToAttributeMapper\StringAnnotationToAttributeMapper;
use Rector\PhpAttribute\AnnotationToAttributeMapper\StringNodeAnnotationToAttributeMapper;
use Rector\PhpAttribute\Contract\AnnotationToAttributeMapperInterface;
use Rector\PhpAttribute\Enum\DocTagNodeState;
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;

/**
* @see \Rector\Tests\PhpAttribute\AnnotationToAttributeMapper\AnnotationToAttributeMapperTest
Expand All @@ -32,27 +25,12 @@ final class AnnotationToAttributeMapper
*/
private array $annotationToAttributeMappers = [];

public function __construct(
// private readonly array $annotationToAttributeMappers,
ArrayAnnotationToAttributeMapper $arrayAnnotationToAttributeMapper,
ArrayItemNodeAnnotationToAttributeMapper $arrayItemNodeAnnotationToAttributeMapper,
ClassConstFetchAnnotationToAttributeMapper $classConstFetchAnnotationToAttributeMapper,
ConstExprNodeAnnotationToAttributeMapper $constExprNodeAnnotationToAttributeMapper,
CurlyListNodeAnnotationToAttributeMapper $curlyListNodeAnnotationToAttributeMapper,
DoctrineAnnotationAnnotationToAttributeMapper $doctrineAnnotationAnnotationToAttributeMapper,
StringAnnotationToAttributeMapper $stringAnnotationToAttributeMapper,
StringNodeAnnotationToAttributeMapper $stringNodeAnnotationToAttributeMapper,
) {
$this->annotationToAttributeMappers = [
$arrayAnnotationToAttributeMapper,
$arrayItemNodeAnnotationToAttributeMapper,
$classConstFetchAnnotationToAttributeMapper,
$constExprNodeAnnotationToAttributeMapper,
$curlyListNodeAnnotationToAttributeMapper,
$doctrineAnnotationAnnotationToAttributeMapper,
$stringAnnotationToAttributeMapper,
$stringNodeAnnotationToAttributeMapper,
];
/**
* @param RewindableGenerator<AnnotationToAttributeMapperInterface> $annotationToAttributeMappers
*/
public function __construct(iterable $annotationToAttributeMappers)
{
$this->annotationToAttributeMappers = iterator_to_array($annotationToAttributeMappers->getIterator());
}

/**
Expand Down
4 changes: 1 addition & 3 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,7 @@
__DIR__ . '/src/PhpParser/NodeTraverser/RectorNodeTraverser.php',
],

RenameVariableToMatchMethodCallReturnTypeRector::class => [
__DIR__ . '/packages/Config/RectorConfig.php',
],
RenameVariableToMatchMethodCallReturnTypeRector::class => [__DIR__ . '/packages/Config/RectorConfig.php'],

StringClassNameToClassConstantRector::class,
__DIR__ . '/bin/validate-phpstan-version.php',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
namespace Rector\Php80\AttributeDecorator;

use PhpParser\Node\Attribute;
use Rector\Php80\Contract\AttributeDecoratorInterface;

final class SensioParamConverterAttributeDecorator implements AttributeDecoratorInterface
final class SensioParamConverterAttributeDecorator
{
public function getAttributeName(): string
{
Expand Down
14 changes: 0 additions & 14 deletions rules/Php80/Contract/AttributeDecoratorInterface.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,12 @@
use PhpParser\Node\AttributeGroup;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Php80\AttributeDecorator\SensioParamConverterAttributeDecorator;
use Rector\Php80\Contract\AttributeDecoratorInterface;

final class AttributeGroupNamedArgumentManipulator
{
/**
* @var AttributeDecoratorInterface[]
*/
private array $attributeDecorators = [];

public function __construct(SensioParamConverterAttributeDecorator $sensioParamConverterAttributeDecorator)
{
$this->attributeDecorators[] = $sensioParamConverterAttributeDecorator;
public function __construct(
private readonly SensioParamConverterAttributeDecorator $sensioParamConverterAttributeDecorator
) {
}

/**
Expand All @@ -30,13 +24,11 @@ public function decorate(array $attributeGroups): void
foreach ($attributeGroup->attrs as $attr) {
$phpAttributeName = $attr->name->getAttribute(AttributeKey::PHP_ATTRIBUTE_NAME);

foreach ($this->attributeDecorators as $attributeDecorator) {
if ($attributeDecorator->getAttributeName() !== $phpAttributeName) {
continue;
}

$attributeDecorator->decorate($attr);
if ($this->sensioParamConverterAttributeDecorator->getAttributeName() !== $phpAttributeName) {
continue;
}

$this->sensioParamConverterAttributeDecorator->decorate($attr);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/Kernel/ContainerBuilderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Rector\NodeNameResolver\Contract\NodeNameResolverInterface;
use Rector\NodeTypeResolver\Contract\NodeTypeResolverInterface;
use Rector\NodeTypeResolver\PHPStan\Scope\Contract\NodeVisitor\ScopeResolverNodeVisitorInterface;
use Rector\PhpAttribute\Contract\AnnotationToAttributeMapperInterface;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
use Rector\StaticTypeMapper\Contract\PhpDocParser\PhpDocTypeMapperInterface;
use Rector\StaticTypeMapper\Contract\PhpParser\PhpParserNodeMapperInterface;
Expand Down Expand Up @@ -44,6 +45,7 @@ final class ContainerBuilderFactory
PhpRectorInterface::class,
NodeNameResolverInterface::class,
FileProcessorInterface::class,
AnnotationToAttributeMapperInterface::class,
];

public function __construct(
Expand All @@ -58,7 +60,6 @@ public function __construct(
public function create(array $configFiles, array $compilerPasses): ContainerBuilder
{
Assert::allIsAOf($compilerPasses, CompilerPassInterface::class);

Assert::allString($configFiles);

$containerBuilder = new ContainerBuilder();
Expand Down
2 changes: 1 addition & 1 deletion src/Kernel/RectorKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class RectorKernel
/**
* @var string
*/
private const CACHE_KEY = 'v107';
private const CACHE_KEY = 'v109';

private ContainerInterface|null $container = null;

Expand Down
77 changes: 0 additions & 77 deletions src/NodeManipulator/MethodCallManipulator.php

This file was deleted.

28 changes: 0 additions & 28 deletions tests/Issues/CovariantTrio/CovariantTrioTest.php

This file was deleted.

0 comments on commit 21f8014

Please sign in to comment.