diff --git a/packages/PhpAttribute/AnnotationToAttributeMapper.php b/packages/PhpAttribute/AnnotationToAttributeMapper.php index b8245ec72cb..b2416663336 100644 --- a/packages/PhpAttribute/AnnotationToAttributeMapper.php +++ b/packages/PhpAttribute/AnnotationToAttributeMapper.php @@ -11,6 +11,14 @@ 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; @@ -20,11 +28,31 @@ final class AnnotationToAttributeMapper { /** - * @param AnnotationToAttributeMapperInterface[] $annotationToAttributeMappers + * @var AnnotationToAttributeMapperInterface[] */ + private array $annotationToAttributeMappers = []; + public function __construct( - private readonly array $annotationToAttributeMappers + // 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, + ]; } /** diff --git a/packages/PostRector/Application/PostFileProcessor.php b/packages/PostRector/Application/PostFileProcessor.php index 0d23a4dc026..a29416df4d7 100644 --- a/packages/PostRector/Application/PostFileProcessor.php +++ b/packages/PostRector/Application/PostFileProcessor.php @@ -6,12 +6,16 @@ use PhpParser\Node\Stmt; use PhpParser\NodeTraverser; -use Rector\Core\Exception\ShouldNotHappenException; use Rector\Core\Logging\CurrentRectorProvider; use Rector\Core\Provider\CurrentFileProvider; use Rector\Core\ValueObject\Application\File; use Rector\PostRector\Contract\Rector\PostRectorDependencyInterface; use Rector\PostRector\Contract\Rector\PostRectorInterface; +use Rector\PostRector\Rector\ClassRenamingPostRector; +use Rector\PostRector\Rector\NameImportingPostRector; +use Rector\PostRector\Rector\PropertyAddingPostRector; +use Rector\PostRector\Rector\UnusedImportRemovingPostRector; +use Rector\PostRector\Rector\UseAddingPostRector; use Rector\Skipper\Skipper\Skipper; final class PostFileProcessor @@ -21,16 +25,29 @@ final class PostFileProcessor */ private array $postRectors = []; - /** - * @param PostRectorInterface[] $postRectors - */ public function __construct( private readonly Skipper $skipper, private readonly CurrentFileProvider $currentFileProvider, private readonly CurrentRectorProvider $currentRectorProvider, - array $postRectors + // set order here + UseAddingPostRector $useAddingPostRector, + NameImportingPostRector $nameImportingPostRector, + PropertyAddingPostRector $propertyAddingPostRector, + ClassRenamingPostRector $classRenamingPostRector, + UnusedImportRemovingPostRector $unusedImportRemovingPostRector, ) { - $this->postRectors = $this->sortByPriority($postRectors); + $this->postRectors = [ + // priority: 900 + $propertyAddingPostRector, + // priority: 650 + $classRenamingPostRector, + // priority: 600 + $nameImportingPostRector, + // priority: 500 + $useAddingPostRector, + // priority: 100 + $unusedImportRemovingPostRector, + ]; } /** @@ -54,32 +71,6 @@ public function traverse(array $stmts): array return $stmts; } - /** - * @param PostRectorInterface[] $postRectors - * @return PostRectorInterface[] - */ - private function sortByPriority(array $postRectors): array - { - $postRectorsByPriority = []; - - foreach ($postRectors as $postRector) { - if (isset($postRectorsByPriority[$postRector->getPriority()])) { - $errorMessage = sprintf( - 'There are multiple post rectors with the same priority: %d. Use different one for your new PostRector', - $postRector->getPriority() - ); - - throw new ShouldNotHappenException($errorMessage); - } - - $postRectorsByPriority[$postRector->getPriority()] = $postRector; - } - - krsort($postRectorsByPriority); - - return $postRectorsByPriority; - } - private function shouldSkipPostRector(PostRectorInterface $postRector): bool { $file = $this->currentFileProvider->getFile(); diff --git a/packages/PostRector/Collector/PropertyToAddCollector.php b/packages/PostRector/Collector/PropertyToAddCollector.php index 205a952550e..3423f515946 100644 --- a/packages/PostRector/Collector/PropertyToAddCollector.php +++ b/packages/PostRector/Collector/PropertyToAddCollector.php @@ -9,6 +9,9 @@ use Rector\PostRector\Contract\Collector\NodeCollectorInterface; use Rector\PostRector\ValueObject\PropertyMetadata; +/** + * @deprecated Use directly in the class + */ final class PropertyToAddCollector implements NodeCollectorInterface { /** diff --git a/packages/PostRector/Contract/Rector/PostRectorInterface.php b/packages/PostRector/Contract/Rector/PostRectorInterface.php index b9351f7d7c8..2a0a18f9494 100644 --- a/packages/PostRector/Contract/Rector/PostRectorInterface.php +++ b/packages/PostRector/Contract/Rector/PostRectorInterface.php @@ -9,8 +9,4 @@ interface PostRectorInterface extends NodeVisitor, RectorInterface { - /** - * Higher values are executed first - */ - public function getPriority(): int; } diff --git a/packages/PostRector/Rector/ClassRenamingPostRector.php b/packages/PostRector/Rector/ClassRenamingPostRector.php index 7b30af5af56..690cb052fe8 100644 --- a/packages/PostRector/Rector/ClassRenamingPostRector.php +++ b/packages/PostRector/Rector/ClassRenamingPostRector.php @@ -33,12 +33,6 @@ public function __construct( ) { } - public function getPriority(): int - { - // must be run before name importing, so new names are imported - return 650; - } - /** * @param Stmt[] $nodes * @return Stmt[] diff --git a/packages/PostRector/Rector/NameImportingPostRector.php b/packages/PostRector/Rector/NameImportingPostRector.php index f5846d7faab..ea945a70543 100644 --- a/packages/PostRector/Rector/NameImportingPostRector.php +++ b/packages/PostRector/Rector/NameImportingPostRector.php @@ -72,12 +72,6 @@ public function enterNode(Node $node): ?Node return $node; } - public function getPriority(): int - { - // this must run after NodeRemovingPostRector, sine renamed use imports can block next import - return 600; - } - public function getRuleDefinition(): RuleDefinition { return new RuleDefinition('Imports fully qualified names', [ diff --git a/packages/PostRector/Rector/PropertyAddingPostRector.php b/packages/PostRector/Rector/PropertyAddingPostRector.php index abeb21ded73..3bdc88b5e9f 100644 --- a/packages/PostRector/Rector/PropertyAddingPostRector.php +++ b/packages/PostRector/Rector/PropertyAddingPostRector.php @@ -24,11 +24,6 @@ public function __construct( ) { } - public function getPriority(): int - { - return 900; - } - public function enterNode(Node $node): ?Node { if (! $node instanceof Class_) { diff --git a/packages/PostRector/Rector/UnusedImportRemovingPostRector.php b/packages/PostRector/Rector/UnusedImportRemovingPostRector.php index 7cae8b5e44b..71f73296007 100644 --- a/packages/PostRector/Rector/UnusedImportRemovingPostRector.php +++ b/packages/PostRector/Rector/UnusedImportRemovingPostRector.php @@ -74,15 +74,6 @@ public function enterNode(Node $node): ?Node return $node; } - /** - * The higher, the later - */ - public function getPriority(): int - { - // run this last - return 100; - } - public function getRuleDefinition(): RuleDefinition { return new RuleDefinition('Removes unused import names', [ diff --git a/packages/PostRector/Rector/UseAddingPostRector.php b/packages/PostRector/Rector/UseAddingPostRector.php index 3a31970fb05..b2cb4192ed3 100644 --- a/packages/PostRector/Rector/UseAddingPostRector.php +++ b/packages/PostRector/Rector/UseAddingPostRector.php @@ -68,12 +68,6 @@ public function beforeTraverse(array $nodes): array return $this->resolveNodesWithImportedUses($nodes, $useImportTypes, $functionUseImportTypes, $namespace); } - public function getPriority(): int - { - // must be after name importing - return 500; - } - public function getRuleDefinition(): RuleDefinition { return new RuleDefinition( diff --git a/phpstan.neon b/phpstan.neon index dc839bdbde7..6ee93abca07 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -715,5 +715,6 @@ parameters: # handle later - '#Fetching class constant class of deprecated class Rector\\(.*?)#' + - '#has typehint with deprecated class Rector\\PostRector\\Collector\\PropertyToAddCollector#' - '#Function "class_exists\(\)" cannot be used/left in the code\: use ReflectionProvider\->has\*\(\) instead#' diff --git a/rules/Naming/Naming/VariableNaming.php b/rules/Naming/Naming/VariableNaming.php index 2eed75bca57..9d166cf1fe8 100644 --- a/rules/Naming/Naming/VariableNaming.php +++ b/rules/Naming/Naming/VariableNaming.php @@ -20,6 +20,8 @@ use PHPStan\Analyser\Scope; use PHPStan\Type\ThisType; use PHPStan\Type\Type; +use Rector\Naming\AssignVariableNameResolver\NewAssignVariableNameResolver; +use Rector\Naming\AssignVariableNameResolver\PropertyFetchAssignVariableNameResolver; use Rector\Naming\Contract\AssignVariableNameResolverInterface; use Rector\NodeNameResolver\NodeNameResolver; use Rector\NodeTypeResolver\NodeTypeResolver; @@ -28,13 +30,20 @@ final class VariableNaming { /** - * @param AssignVariableNameResolverInterface[] $assignVariableNameResolvers + * @var AssignVariableNameResolverInterface[] */ + private array $assignVariableNameResolvers = []; + public function __construct( private readonly NodeNameResolver $nodeNameResolver, private readonly NodeTypeResolver $nodeTypeResolver, - private readonly array $assignVariableNameResolvers + PropertyFetchAssignVariableNameResolver $propertyFetchAssignVariableNameResolver, + NewAssignVariableNameResolver $newAssignVariableNameResolver, ) { + $this->assignVariableNameResolvers = [ + $propertyFetchAssignVariableNameResolver, + $newAssignVariableNameResolver, + ]; } /** diff --git a/src/Kernel/RectorKernel.php b/src/Kernel/RectorKernel.php index 4eae5f0abdf..84a536a5137 100644 --- a/src/Kernel/RectorKernel.php +++ b/src/Kernel/RectorKernel.php @@ -17,7 +17,7 @@ final class RectorKernel /** * @var string */ - private const CACHE_KEY = 'v72'; + private const CACHE_KEY = 'v73'; private ContainerInterface|null $container = null;