Skip to content

Commit

Permalink
[DX] Merge PhpRectorInterface to RectorInterface as identical contrac…
Browse files Browse the repository at this point in the history
…t and separation no longer needed (#4812)
  • Loading branch information
TomasVotruba committed Aug 18, 2023
1 parent 00f736f commit 216108b
Show file tree
Hide file tree
Showing 20 changed files with 71 additions and 176 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,26 @@

namespace Rector\Tests\ChangesReporting\Annotation\AppliedRectorsChangelogResolver\Source;

use Rector\Core\Contract\Rector\RectorInterface;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @changelog https://github.com/rectorphp/rector/blob/main/docs/rector_rules_overview.md
*/
final class RectorWithChangelog implements RectorInterface
final class RectorWithChangelog extends AbstractRector
{

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Foo', []);
}

public function getNodeTypes(): array
{
return [];
}

public function refactor(\PhpParser\Node $node)
{

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,25 @@
namespace Rector\Tests\ChangesReporting\Annotation\AppliedRectorsChangelogResolver\Source;

use Rector\Core\Contract\Rector\RectorInterface;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

final class RectorWithOutChangelog implements RectorInterface
final class RectorWithOutChangelog extends AbstractRector
{

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Foo', []);
}


public function getNodeTypes(): array
{
return [];
}

public function refactor(\PhpParser\Node $node)
{

}

}
7 changes: 2 additions & 5 deletions packages/Config/RectorConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Rector\Core\Configuration\Parameter\SimpleParameterProvider;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Contract\Rector\NonPhpRectorInterface;
use Rector\Core\Contract\Rector\PhpRectorInterface;
use Rector\Core\Contract\Rector\RectorInterface;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\NodeAnalyzer\ScopeAnalyzer;
Expand Down Expand Up @@ -350,15 +349,13 @@ private function resolveDuplicatedValues(array $values): array
}

/**
* @param class-string<RectorInterface|PhpRectorInterface> $rectorClass
* @param class-string<RectorInterface> $rectorClass
*/
private function tagRectorService(string $rectorClass): void
{
$this->tag($rectorClass, RectorInterface::class);

if (is_a($rectorClass, PhpRectorInterface::class, true)) {
$this->tag($rectorClass, PhpRectorInterface::class);
} elseif (is_a($rectorClass, NonPhpRectorInterface::class, true)) {
if (is_a($rectorClass, NonPhpRectorInterface::class, true)) {
trigger_error(sprintf(
'The "%s" interface of "%s" rule is deprecated. Rector will only PHP code, as designed to with AST. For another file format, use custom tooling.',
NonPhpRectorInterface::class,
Expand Down
3 changes: 1 addition & 2 deletions packages/PostRector/Contract/Rector/PostRectorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
namespace Rector\PostRector\Contract\Rector;

use PhpParser\NodeVisitor;
use Rector\Core\Contract\Rector\RectorInterface;

interface PostRectorInterface extends NodeVisitor, RectorInterface
interface PostRectorInterface extends NodeVisitor
{
}
21 changes: 0 additions & 21 deletions packages/PostRector/Rector/ClassRenamingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
use Rector\Core\PhpParser\Node\CustomNode\FileWithoutNamespace;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Renaming\NodeManipulator\ClassRenamer;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

final class ClassRenamingPostRector extends AbstractPostRector
{
Expand Down Expand Up @@ -80,23 +78,4 @@ public function enterNode(Node $node): ?Node

return $result;
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Rename references for classes that were renamed during Rector run', [
new CodeSample(
<<<'CODE_SAMPLE'
function (OriginalClass $someClass)
{
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
function (RenamedClass $someClass)
{
}
CODE_SAMPLE
),
]);
}
}
29 changes: 0 additions & 29 deletions packages/PostRector/Rector/NameImportingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
use Rector\Naming\Naming\UseImportsResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockNameImporter;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

final class NameImportingPostRector extends AbstractPostRector
{
Expand Down Expand Up @@ -76,33 +74,6 @@ public function enterNode(Node $node): ?Node
return null;
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Imports fully qualified names', [
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function run(App\AnotherClass $anotherClass)
{
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use App\AnotherClass;
class SomeClass
{
public function run(AnotherClass $anotherClass)
{
}
}
CODE_SAMPLE
),
]);
}

private function processNodeName(Name $name, File $file): ?Node
{
if ($name->isSpecialClassName()) {
Expand Down
27 changes: 0 additions & 27 deletions packages/PostRector/Rector/UnusedImportRemovingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
use Rector\Core\PhpParser\Node\CustomNode\FileWithoutNamespace;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

final class UnusedImportRemovingPostRector extends AbstractPostRector
{
Expand Down Expand Up @@ -73,31 +71,6 @@ public function enterNode(Node $node): ?Node
return $node;
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Removes unused import names', [
new CodeSample(
<<<'CODE_SAMPLE'
namespace App;
use App\SomeUnusedClass;
class SomeClass
{
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
namespace App;
class SomeClass
{
}
CODE_SAMPLE
),
]);
}

/**
* @return string[]
*/
Expand Down
31 changes: 0 additions & 31 deletions packages/PostRector/Rector/UseAddingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory;
use Rector\PostRector\Collector\UseNodesToAddCollector;
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

final class UseAddingPostRector extends AbstractPostRector
{
Expand Down Expand Up @@ -79,35 +77,6 @@ public function beforeTraverse(array $nodes): array
);
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Add unique use imports collected during Rector run',
[
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function run(AnotherClass $anotherClass)
{
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use App\AnotherClass;
class SomeClass
{
public function run(AnotherClass $anotherClass)
{
}
}
CODE_SAMPLE
), ]
);
}

/**
* @param Stmt[] $nodes
* @param FullyQualifiedObjectType[] $useImportTypes
Expand Down
2 changes: 0 additions & 2 deletions packages/Testing/PHPUnit/AbstractLazyTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use PHPUnit\Framework\TestCase;
use Rector\Config\RectorConfig;
use Rector\Core\Contract\Rector\PhpRectorInterface;
use Rector\Core\Contract\Rector\RectorInterface;
use Rector\Core\DependencyInjection\LazyContainerFactory;
use Rector\Core\Rector\AbstractRector;
Expand Down Expand Up @@ -63,7 +62,6 @@ protected function forgetRectorsRules(): void
$privatesAccessor = new PrivatesAccessor();
$privatesAccessor->propertyClosure($rectorConfig, 'tags', static function (array $tags): array {
unset($tags[RectorInterface::class]);
unset($tags[PhpRectorInterface::class]);
return $tags;
});

Expand Down
8 changes: 3 additions & 5 deletions packages/Testing/PHPUnit/AbstractRectorTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Rector\Core\Configuration\Option;
use Rector\Core\Configuration\Parameter\SimpleParameterProvider;
use Rector\Core\Configuration\RenamedClassesDataCollector;
use Rector\Core\Contract\Rector\PhpRectorInterface;
use Rector\Core\Contract\Rector\RectorInterface;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\PhpParser\NodeTraverser\RectorNodeTraverser;
Expand Down Expand Up @@ -77,14 +76,13 @@ protected function setUp(): void

// this has to be always empty, so we can add new rules with their configuration
$this->assertEmpty($rectorConfig->tagged(RectorInterface::class));
$this->assertEmpty($rectorConfig->tagged(PhpRectorInterface::class));

$this->bootFromConfigFiles([$configFile]);

$phpRectorsGenerator = $rectorConfig->tagged(PhpRectorInterface::class);
$rectorsGenerator = $rectorConfig->tagged(RectorInterface::class);

if ($phpRectorsGenerator instanceof RewindableGenerator) {
$phpRectors = iterator_to_array($phpRectorsGenerator->getIterator());
if ($rectorsGenerator instanceof RewindableGenerator) {
$phpRectors = iterator_to_array($rectorsGenerator->getIterator());
} else {
// no rules at all, e.g. in case of only post rector run
$phpRectors = [];
Expand Down
6 changes: 3 additions & 3 deletions packages/VersionBonding/PhpVersionedFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Rector\VersionBonding;

use Rector\Core\Contract\Rector\PhpRectorInterface;
use Rector\Core\Contract\Rector\RectorInterface;
use Rector\Core\Php\PhpVersionProvider;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;

Expand All @@ -16,8 +16,8 @@ public function __construct(
}

/**
* @param array<PhpRectorInterface> $rectors
* @return array<PhpRectorInterface>
* @param array<RectorInterface> $rectors
* @return array<RectorInterface>
*/
public function filter(iterable $rectors): array
{
Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ parameters:
- '#Dynamic call to static method PHPUnit\\Framework\\Assert\:\:(.*?)\(\)#'

# specific generics
- '#Parameter \#1 \$node (.*?) of method Rector\\(.*?)Rector\:\:refactor\(\) should be contravariant with parameter \$node \(PhpParser\\Node\) of method Rector\\Core\\Contract\\Rector\\PhpRectorInterface\:\:refactor\(\)#'
- '#Parameter \#1 \$node (.*?) of method Rector\\(.*?)Rector\:\:(refactor|refactorWithScope)\(\) should be contravariant with parameter \$node \(PhpParser\\Node\) of method Rector\\Core\\Contract\\Rector#'

# generics
- '#Parameter \#1 \$stmts of class Rector\\Core\\PhpParser\\Node\\CustomNode\\FileWithoutNamespace constructor expects array<PhpParser\\Node\\Stmt\>, array<TNode of PhpParser\\Node\> given#'
Expand Down
26 changes: 0 additions & 26 deletions src/Contract/Rector/PhpRectorInterface.php

This file was deleted.

18 changes: 17 additions & 1 deletion src/Contract/Rector/RectorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,24 @@

namespace Rector\Core\Contract\Rector;

use PhpParser\Node;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor;
use Symplify\RuleDocGenerator\Contract\DocumentedRuleInterface;

interface RectorInterface extends DocumentedRuleInterface
interface RectorInterface extends NodeVisitor, DocumentedRuleInterface
{
/**
* List of nodes this class checks, classes that implements \PhpParser\Node
* See beautiful map of all nodes https://github.com/rectorphp/php-parser-nodes-docs#node-overview
*
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array;

/**
* Process Node of matched type
* @return Node|Node[]|null|NodeTraverser::*
*/
public function refactor(Node $node);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use PhpParser\NodeTraverser;
use PHPStan\Analyser\Scope;

interface ScopeAwarePhpRectorInterface extends PhpRectorInterface
interface ScopeAwareRectorInterface extends RectorInterface
{
/**
* Process Node of matched type with its PHPStan scope
Expand Down
Loading

0 comments on commit 216108b

Please sign in to comment.