Skip to content

Commit

Permalink
[DX] Deprecate NonPhpRectorInterface, the only rule and its file proc…
Browse files Browse the repository at this point in the history
…essor, to make Rector handle exlusively PHP (#4761)
  • Loading branch information
TomasVotruba committed Aug 10, 2023
1 parent cbe3255 commit 1659ca2
Show file tree
Hide file tree
Showing 39 changed files with 37 additions and 543 deletions.
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@
],
"files": [
"tests/debug_functions.php",
"stubs/Directory.php",
"rules-tests/Transform/Rector/FuncCall/FuncCallToMethodCallRector/Source/some_view_function.php",
"rules-tests/TypeDeclaration/Rector/ClassMethod/ParamTypeByMethodCallTypeRector/Source/FunctionTyped.php"
]
Expand Down
5 changes: 0 additions & 5 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@
use Rector\Core\Console\Style\RectorStyle;
use Rector\Core\Console\Style\SymfonyStyleFactory;
use Rector\Core\Contract\Processor\FileProcessorInterface;
use Rector\Core\Contract\Rector\NonPhpRectorInterface;
use Rector\Core\Contract\Rector\PhpRectorInterface;
use Rector\Core\Contract\Rector\RectorInterface;
use Rector\Core\NonPhpFile\NonPhpFileProcessor;
use Rector\Core\PhpParser\NodeTraverser\RectorNodeTraverser;
use Rector\Core\ValueObjectFactory\Application\FileFactory;
use Rector\NodeNameResolver\Contract\NodeNameResolverInterface;
Expand Down Expand Up @@ -301,9 +299,6 @@
$services->set(OutputFormatterCollector::class)
->arg('$outputFormatters', tagged_iterator(OutputFormatterInterface::class));

$services->set(NonPhpFileProcessor::class)
->arg('$nonPhpRectors', tagged_iterator(NonPhpRectorInterface::class));

$services->set(RectorNodeTraverser::class)
->arg('$phpRectors', tagged_iterator(PhpRectorInterface::class));

Expand Down
10 changes: 0 additions & 10 deletions packages/ChangesReporting/ValueObjectFactory/FileDiffFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,6 @@ public function __construct(
) {
}

public function createFileDiff(File $file, string $oldContent, string $newContent): FileDiff
{
return $this->createFileDiffWithLineChanges(
$file,
$oldContent,
$newContent,
$file->getRectorWithLineChanges()
);
}

/**
* @param RectorWithLineChange[] $rectorsWithLineChanges
*/
Expand Down
9 changes: 7 additions & 2 deletions packages/Config/LazyRectorConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ private function resolveDuplicatedValues(array $values): array
}

/**
* @param class-string<RectorInterface|PhpRectorInterface|NonPhpRectorInterface> $rectorClass
* @param class-string<RectorInterface|PhpRectorInterface> $rectorClass
*/
private function tagRectorService(string $rectorClass): void
{
Expand All @@ -283,7 +283,12 @@ private function tagRectorService(string $rectorClass): void
if (is_a($rectorClass, PhpRectorInterface::class, true)) {
$this->tag($rectorClass, PhpRectorInterface::class);
} elseif (is_a($rectorClass, NonPhpRectorInterface::class, true)) {
$this->tag($rectorClass, NonPhpRectorInterface::class);
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,
$rectorClass,
));
exit();
}
}
}
9 changes: 7 additions & 2 deletions packages/Config/RectorConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ private function getServices(): ServicesConfigurator
}

/**
* @param class-string<RectorInterface|PhpRectorInterface|NonPhpRectorInterface> $rectorClass
* @param class-string<RectorInterface|PhpRectorInterface> $rectorClass
*/
private function tagRectorService(ServiceConfigurator $rectorServiceConfigurator, string $rectorClass): void
{
Expand All @@ -348,7 +348,12 @@ private function tagRectorService(ServiceConfigurator $rectorServiceConfigurator
if (is_a($rectorClass, PhpRectorInterface::class, true)) {
$rectorServiceConfigurator->tag(PhpRectorInterface::class);
} elseif (is_a($rectorClass, NonPhpRectorInterface::class, true)) {
$rectorServiceConfigurator->tag(NonPhpRectorInterface::class);
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,
$rectorClass,
));
exit();
}
}
}
17 changes: 6 additions & 11 deletions packages/PostRector/Application/PostFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
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\UnusedImportRemovingPostRector;
use Rector\PostRector\Rector\UseAddingPostRector;
use Rector\Renaming\Rector\Name\RenameClassRector;
use Rector\Skipper\Skipper\Skipper;

final class PostFileProcessor
Expand Down Expand Up @@ -79,15 +79,10 @@ private function shouldSkipPostRector(PostRectorInterface $postRector): bool
return true;
}

if ($postRector instanceof PostRectorDependencyInterface) {
$dependencies = $postRector->getRectorDependencies();
foreach ($dependencies as $dependency) {
if ($this->skipper->shouldSkipElementAndFilePath($dependency, $filePath)) {
return true;
}
}
}

return false;
// skip renaming if rename class rector is skipped
return $postRector instanceof ClassRenamingPostRector && $this->skipper->shouldSkipElementAndFilePath(
RenameClassRector::class,
$filePath
);
}
}

This file was deleted.

14 changes: 1 addition & 13 deletions packages/PostRector/Rector/ClassRenamingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,13 @@
use Rector\Core\Configuration\Option;
use Rector\Core\Configuration\Parameter\SimpleParameterProvider;
use Rector\Core\Configuration\RenamedClassesDataCollector;
use Rector\Core\Contract\Rector\RectorInterface;
use Rector\Core\NonPhpFile\Rector\RenameClassNonPhpRector;
use Rector\Core\PhpParser\Node\CustomNode\FileWithoutNamespace;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PostRector\Contract\Rector\PostRectorDependencyInterface;
use Rector\Renaming\NodeManipulator\ClassRenamer;
use Rector\Renaming\Rector\Name\RenameClassRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

final class ClassRenamingPostRector extends AbstractPostRector implements PostRectorDependencyInterface
final class ClassRenamingPostRector extends AbstractPostRector
{
private FileWithoutNamespace|Namespace_|null $rootNode = null;

Expand Down Expand Up @@ -55,14 +51,6 @@ public function beforeTraverse(array $nodes): array
return $nodes;
}

/**
* @return class-string<RectorInterface>[]
*/
public function getRectorDependencies(): array
{
return [RenameClassRector::class, RenameClassNonPhpRector::class];
}

public function enterNode(Node $node): ?Node
{
// cannot be renamed
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 Illuminate\Container\Container;
use PHPUnit\Framework\TestCase;
use Rector\Core\Contract\Rector\NonPhpRectorInterface;
use Rector\Core\Contract\Rector\PhpRectorInterface;
use Rector\Core\Contract\Rector\RectorInterface;
use Rector\Core\DependencyInjection\LazyContainerFactory;
Expand Down Expand Up @@ -72,7 +71,6 @@ protected function forgetRectorsRules(): void
$privatesAccessor->propertyClosure($container, 'tags', static function (array $tags): array {
unset($tags[RectorInterface::class]);
unset($tags[PhpRectorInterface::class]);
unset($tags[NonPhpRectorInterface::class]);
return $tags;
});

Expand Down
5 changes: 5 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -687,3 +687,8 @@ parameters:

# not relevant
- '#Do not use static property#'

# deprecated
-
message: '#Fetching class constant class of deprecated class Rector\\Core\\Contract\\Rector\\NonPhpRectorInterface#'
path: packages/Config/*

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

28 changes: 0 additions & 28 deletions rules-tests/Renaming/Rector/Name/RenameClassRector/NonPhpTest.php

This file was deleted.

This file was deleted.

3 changes: 3 additions & 0 deletions src/Contract/Processor/FileProcessorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
use Rector\Core\ValueObject\Error\SystemError;
use Rector\Core\ValueObject\Reporting\FileDiff;

/**
* @internal
*/
interface FileProcessorInterface
{
public function supports(File $file, Configuration $configuration): bool;
Expand Down
3 changes: 3 additions & 0 deletions src/Contract/Rector/NonPhpRectorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace Rector\Core\Contract\Rector;

/**
* @deprecated Rector should never handle anything outside PHP files, as oustide it's scope - use custom tool instead.
*/
interface NonPhpRectorInterface extends RectorInterface
{
public function refactorFileContent(string $fileContent): string;
Expand Down
7 changes: 0 additions & 7 deletions src/DependencyInjection/LazyContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,11 @@
use Rector\Core\Console\Style\RectorStyle;
use Rector\Core\Console\Style\SymfonyStyleFactory;
use Rector\Core\Contract\Processor\FileProcessorInterface;
use Rector\Core\Contract\Rector\NonPhpRectorInterface;
use Rector\Core\Contract\Rector\PhpRectorInterface;
use Rector\Core\Contract\Rector\RectorInterface;
use Rector\Core\Logging\CurrentRectorProvider;
use Rector\Core\Logging\RectorOutput;
use Rector\Core\NodeDecorator\CreatedByRuleDecorator;
use Rector\Core\NonPhpFile\NonPhpFileProcessor;
use Rector\Core\PhpParser\Comparing\NodeComparator;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Core\PhpParser\Node\NodeFactory;
Expand Down Expand Up @@ -425,7 +423,6 @@ public function create(): LazyRectorConfig
->giveTagged(Command::class);

$lazyRectorConfig->tag(PhpFileProcessor::class, FileProcessorInterface::class);
$lazyRectorConfig->tag(NonPhpFileProcessor::class, FileProcessorInterface::class);

$lazyRectorConfig->tag(ProcessCommand::class, Command::class);
$lazyRectorConfig->tag(WorkerCommand::class, Command::class);
Expand All @@ -440,10 +437,6 @@ public function create(): LazyRectorConfig
$lazyRectorConfig->tag(MissingInSetCommand::class, Command::class);
$lazyRectorConfig->tag(OutsideAnySetCommand::class, Command::class);

$lazyRectorConfig->when(NonPhpFileProcessor::class)
->needs('$nonPhpRectors')
->giveTagged(NonPhpRectorInterface::class);

$lazyRectorConfig->when(ApplicationFileProcessor::class)
->needs('$fileProcessors')
->giveTagged(FileProcessorInterface::class);
Expand Down
2 changes: 0 additions & 2 deletions src/Kernel/ContainerBuilderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Rector\CodingStyle\Contract\ClassNameImport\ClassNameImportSkipVoterInterface;
use Rector\Core\Config\Loader\ConfigureCallMergingLoaderFactory;
use Rector\Core\Contract\Processor\FileProcessorInterface;
use Rector\Core\Contract\Rector\NonPhpRectorInterface;
use Rector\Core\Contract\Rector\PhpRectorInterface;
use Rector\Core\Contract\Rector\RectorInterface;
use Rector\NodeNameResolver\Contract\NodeNameResolverInterface;
Expand Down Expand Up @@ -43,7 +42,6 @@ final class ContainerBuilderFactory
Command::class,
RectorInterface::class,
OutputFormatterInterface::class,
NonPhpRectorInterface::class,
PhpRectorInterface::class,
NodeNameResolverInterface::class,
FileProcessorInterface::class,
Expand Down

0 comments on commit 1659ca2

Please sign in to comment.