Skip to content

Commit

Permalink
From magic array autowire to explicit autowire (#4122)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jun 8, 2023
1 parent ce31e60 commit d003424
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 73 deletions.
32 changes: 30 additions & 2 deletions packages/PhpAttribute/AnnotationToAttributeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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,
];
}

/**
Expand Down
55 changes: 23 additions & 32 deletions packages/PostRector/Application/PostFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
];
}

/**
Expand All @@ -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();
Expand Down
3 changes: 3 additions & 0 deletions packages/PostRector/Collector/PropertyToAddCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
/**
Expand Down
4 changes: 0 additions & 4 deletions packages/PostRector/Contract/Rector/PostRectorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,4 @@

interface PostRectorInterface extends NodeVisitor, RectorInterface
{
/**
* Higher values are executed first
*/
public function getPriority(): int;
}
6 changes: 0 additions & 6 deletions packages/PostRector/Rector/ClassRenamingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand Down
6 changes: 0 additions & 6 deletions packages/PostRector/Rector/NameImportingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', [
Expand Down
5 changes: 0 additions & 5 deletions packages/PostRector/Rector/PropertyAddingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ public function __construct(
) {
}

public function getPriority(): int
{
return 900;
}

public function enterNode(Node $node): ?Node
{
if (! $node instanceof Class_) {
Expand Down
9 changes: 0 additions & 9 deletions packages/PostRector/Rector/UnusedImportRemovingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', [
Expand Down
6 changes: 0 additions & 6 deletions packages/PostRector/Rector/UseAddingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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#'
13 changes: 11 additions & 2 deletions rules/Naming/Naming/VariableNaming.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
];
}

/**
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 = 'v72';
private const CACHE_KEY = 'v73';

private ContainerInterface|null $container = null;

Expand Down

0 comments on commit d003424

Please sign in to comment.