Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,9 @@ public function traverseNodes(array $nodes): array

return $nodes;
}

public function getPriority(): int
{
return 600;
}
}
5 changes: 5 additions & 0 deletions packages/CodingStyle/src/Application/UseAddingCommander.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,11 @@ public function canImportBeAdded(Node $node, FullyQualifiedObjectType $fullyQual
return true;
}

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

/**
* @return FullyQualifiedObjectType[]
*/
Expand Down
50 changes: 50 additions & 0 deletions src/Commander/CommanderCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php declare(strict_types=1);

namespace Rector\Commander;

use Rector\Contract\PhpParser\Node\CommanderInterface;
use Rector\Exception\ShouldNotHappenException;

final class CommanderCollector
{
/**
* @var CommanderInterface[]
*/
private $commanders = [];

/**
* @param CommanderInterface[] $commanders
*/
public function __construct(array $commanders)
{
$this->sortAndSetCommanders($commanders);
}

/**
* @return CommanderInterface[]
*/
public function provide(): array
{
return $this->commanders;
}

/**
* @param CommanderInterface[] $commanders
*/
private function sortAndSetCommanders(array $commanders): void
{
$commandersByPriority = [];

foreach ($commanders as $commander) {
if (isset($commandersByPriority[$commander->getPriority()])) {
throw new ShouldNotHappenException();
}

$commandersByPriority[$commander->getPriority()] = $commander;
}

krsort($commandersByPriority);

$this->commanders = $commandersByPriority;
}
}
5 changes: 5 additions & 0 deletions src/Contract/PhpParser/Node/CommanderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

interface CommanderInterface
{
/**
* Higher values are executed first
*/
public function getPriority(): int;

public function isActive(): bool;

/**
Expand Down
5 changes: 5 additions & 0 deletions src/PhpParser/Node/Commander/NodeAddingCommander.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ public function isActive(): bool
return count($this->nodesToAdd) > 0 || count($this->nodesToAddBefore) > 0;
}

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

private function resolveNearestExpressionPosition(Node $node): string
{
if ($node instanceof Expression) {
Expand Down
5 changes: 5 additions & 0 deletions src/PhpParser/Node/Commander/NodeRemovingCommander.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ public function getNodesToRemove(): array
return $this->nodesToRemove;
}

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

private function ensureIsNotPartOfChainMethodCall(Node $node): void
{
if (! $node instanceof MethodCall) {
Expand Down
5 changes: 5 additions & 0 deletions src/PhpParser/Node/Commander/PropertyAddingCommander.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public function isActive(): bool
return count($this->propertiesByClass) > 0;
}

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

private function createNodeVisitor(): NodeVisitor
{
return new class($this->classManipulator, $this->propertiesByClass) extends NodeVisitorAbstract {
Expand Down
42 changes: 11 additions & 31 deletions src/Rector/AbstractRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Expression;
use PhpParser\NodeVisitorAbstract;
use Rector\Commander\CommanderCollector;
use Rector\Contract\PhpParser\Node\CommanderInterface;
use Rector\Contract\Rector\PhpRectorInterface;
use Rector\NodeTypeResolver\Node\AttributeKey;
Expand Down Expand Up @@ -38,9 +39,9 @@ abstract class AbstractRector extends NodeVisitorAbstract implements PhpRectorIn
private $phpVersionProvider;

/**
* @var CommanderInterface[]
* @var CommanderCollector
*/
private $commanders = [];
private $commanderCollector;

/**
* Run once in the every end of one processed file
Expand All @@ -51,18 +52,17 @@ protected function tearDown(): void

/**
* @required
* @param CommanderInterface[] $symfonyStyle
*/
public function autowireAbstractRectorDependencies(
SymfonyStyle $symfonyStyle,
PhpVersionProvider $phpVersionProvider,
BuilderFactory $builderFactory
// array $commanders = []
BuilderFactory $builderFactory,
CommanderCollector $commanderCollector
): void {
$this->symfonyStyle = $symfonyStyle;
$this->phpVersionProvider = $phpVersionProvider;
$this->builderFactory = $builderFactory;
// $this->commanders = $commanders;
$this->commanderCollector = $commanderCollector;
}

/**
Expand Down Expand Up @@ -123,32 +123,12 @@ final public function enterNode(Node $node)
*/
public function afterTraverse(array $nodes): array
{
// foreach ($this->commanders as $commander) {
// if (! $commander->isActive()) {
// continue;
// }
//
// $nodes = $commander->traverseNodes($nodes);
// }
if ($this->nodeAddingCommander->isActive()) {
$nodes = $this->nodeAddingCommander->traverseNodes($nodes);
}

if ($this->propertyAddingCommander->isActive()) {
$nodes = $this->propertyAddingCommander->traverseNodes($nodes);
}

if ($this->nodeRemovingCommander->isActive()) {
$nodes = $this->nodeRemovingCommander->traverseNodes($nodes);
}

// this must run before use imports, since it adds them
if ($this->nameImportingCommander->isActive()) {
$nodes = $this->nameImportingCommander->traverseNodes($nodes);
}
foreach ($this->commanderCollector->provide() as $commander) {
if (! $commander->isActive()) {
continue;
}

if ($this->useAddingCommander->isActive()) {
$nodes = $this->useAddingCommander->traverseNodes($nodes);
$nodes = $commander->traverseNodes($nodes);
}

$this->tearDown();
Expand Down