diff --git a/rules/Privatization/Guard/LaravelModelGuard.php b/rules/Privatization/Guard/LaravelModelGuard.php index e51610dc48a..bd0ec7eef56 100644 --- a/rules/Privatization/Guard/LaravelModelGuard.php +++ b/rules/Privatization/Guard/LaravelModelGuard.php @@ -8,6 +8,7 @@ use PhpParser\Node\Stmt\ClassMethod; use PHPStan\Reflection\ClassReflection; use PHPStan\Type\ObjectType; +use Rector\Enum\LaravelClassName; use Rector\NodeNameResolver\NodeNameResolver; use Rector\NodeTypeResolver\NodeTypeResolver; use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer; @@ -39,7 +40,7 @@ public function __construct( public function isProtectedMethod(ClassReflection $classReflection, ClassMethod $classMethod): bool { - if (! $classReflection->is('Illuminate\Database\Eloquent\Model')) { + if (! $classReflection->is(LaravelClassName::MODEL)) { return false; } @@ -63,7 +64,7 @@ private function isAttributeMethod(string $name, ClassMethod $classMethod): bool return $this->nodeTypeResolver->isObjectType( $classMethod->returnType, - new ObjectType('Illuminate\Database\Eloquent\Casts\Attribute') + new ObjectType(LaravelClassName::CAST_ATTRIBUTE) ); } @@ -73,9 +74,6 @@ private function isScopeMethod(string $name, ClassMethod $classMethod): bool return true; } - return $this->phpAttributeAnalyzer->hasPhpAttribute( - $classMethod, - 'Illuminate\Database\Eloquent\Attributes\Scope' - ); + return $this->phpAttributeAnalyzer->hasPhpAttribute($classMethod, LaravelClassName::ATTRIBUTES_SCOPE); } } diff --git a/rules/Privatization/Guard/ParentClassMagicCallGuard.php b/rules/Privatization/Guard/ParentClassMagicCallGuard.php index b3fb3eb1930..083b11504e8 100644 --- a/rules/Privatization/Guard/ParentClassMagicCallGuard.php +++ b/rules/Privatization/Guard/ParentClassMagicCallGuard.php @@ -4,12 +4,12 @@ namespace Rector\Privatization\Guard; -use PhpParser\PrettyPrinterAbstract; use PhpParser\Node\Expr; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Name; use PhpParser\Node\Stmt\Class_; use PhpParser\PrettyPrinter\Standard; +use PhpParser\PrettyPrinterAbstract; use Rector\NodeNameResolver\NodeNameResolver; use Rector\PhpParser\AstResolver; use Rector\PhpParser\Node\BetterNodeFinder; diff --git a/src/Enum/LaravelClassName.php b/src/Enum/LaravelClassName.php new file mode 100644 index 00000000000..70eedaaac4a --- /dev/null +++ b/src/Enum/LaravelClassName.php @@ -0,0 +1,14 @@ +renamedClassesDataCollector->getOldToNewClasses() !== []; $isNameImportingEnabled = SimpleParameterProvider::provideBoolParameter(Option::AUTO_IMPORT_NAMES); - $isDocblockNameImportingEnabled = SimpleParameterProvider::provideBoolParameter( - Option::AUTO_IMPORT_DOC_BLOCK_NAMES - ); $isRemovingUnusedImportsEnabled = SimpleParameterProvider::provideBoolParameter(Option::REMOVE_UNUSED_IMPORTS); @@ -113,11 +110,11 @@ private function getPostRectors(): array // import names if ($isNameImportingEnabled) { $postRectors[] = $this->nameImportingPostRector; - } - // import docblocks - if ($isNameImportingEnabled && $isDocblockNameImportingEnabled) { - $postRectors[] = $this->docblockNameImportingPostRector; + // import docblocks + if (SimpleParameterProvider::provideBoolParameter(Option::AUTO_IMPORT_DOC_BLOCK_NAMES)) { + $postRectors[] = $this->docblockNameImportingPostRector; + } } $postRectors[] = $this->useAddingPostRector; diff --git a/src/Testing/TestingParser/TestingParser.php b/src/Testing/TestingParser/TestingParser.php index 14fe9d9b70e..01f7d8d1650 100644 --- a/src/Testing/TestingParser/TestingParser.php +++ b/src/Testing/TestingParser/TestingParser.php @@ -27,17 +27,7 @@ public function __construct( public function parseFilePathToFile(string $filePath): File { - // needed for PHPStan reflection, as it caches the last processed file - $this->dynamicSourceLocatorProvider->setFilePath($filePath); - - $fileContent = FileSystem::read($filePath); - $file = new File($filePath, $fileContent); - $stmts = $this->rectorParser->parseString($fileContent); - - $stmts = $this->nodeScopeAndMetadataDecorator->decorateNodesFromFile($filePath, $stmts); - - $file->hydrateStmtsAndTokens($stmts, $stmts, []); - $this->currentFileProvider->setFile($file); + [$file, $stmts] = $this->parseToFileAndStmts($filePath); return $file; } @@ -46,19 +36,29 @@ public function parseFilePathToFile(string $filePath): File * @return Node[] */ public function parseFileToDecoratedNodes(string $filePath): array + { + [$file, $stmts] = $this->parseToFileAndStmts($filePath); + + return $stmts; + } + + /** + * @return array{0: File, 1: Node[]} + */ + private function parseToFileAndStmts(string $filePath): array { // needed for PHPStan reflection, as it caches the last processed file $this->dynamicSourceLocatorProvider->setFilePath($filePath); $fileContent = FileSystem::read($filePath); - $stmts = $this->rectorParser->parseString($fileContent); $file = new File($filePath, $fileContent); + $stmts = $this->rectorParser->parseString($fileContent); $stmts = $this->nodeScopeAndMetadataDecorator->decorateNodesFromFile($filePath, $stmts); - $file->hydrateStmtsAndTokens($stmts, $stmts, []); + $file->hydrateStmtsAndTokens($stmts, $stmts, []); $this->currentFileProvider->setFile($file); - return $stmts; + return [$file, $stmts]; } } diff --git a/tests/Comments/CommentRemover/Fixture/another_comment.php.inc b/tests/Comments/CommentRemover/Fixture/another_comment.php.inc index 62e5d0fa065..4c0b367cba9 100644 --- a/tests/Comments/CommentRemover/Fixture/another_comment.php.inc +++ b/tests/Comments/CommentRemover/Fixture/another_comment.php.inc @@ -2,7 +2,7 @@ namespace Rector\Tests\Comments\CommentRemover\Fixture; -$values = new class +final class AnotherComment { public function run($value) { @@ -13,13 +13,13 @@ $values = new class return 'https://some_very_long_link.cz'; /* here too */ } } -}; +} ?> ----- namespace Rector\Tests\Comments\CommentRemover\Fixture; -$values = new class +final class AnotherComment { public function run($value) { @@ -28,4 +28,4 @@ $values = new class return 'https://some_very_long_link.cz'; } } -}; +} diff --git a/tests/Issues/AnnotationToAttributeRenameAutoImport/config/configured_rule.php b/tests/Issues/AnnotationToAttributeRenameAutoImport/config/configured_rule.php index 96d7d0c4d9a..c634e1a6ed9 100644 --- a/tests/Issues/AnnotationToAttributeRenameAutoImport/config/configured_rule.php +++ b/tests/Issues/AnnotationToAttributeRenameAutoImport/config/configured_rule.php @@ -8,6 +8,7 @@ use Rector\Renaming\Rector\Name\RenameClassRector; return static function (RectorConfig $rectorConfig): void { + /** triggers @see \Rector\NodeTypeResolver\PhpDocNodeVisitor\NameImportingPhpDocNodeVisitor */ $rectorConfig->importNames(); $rectorConfig->ruleWithConfiguration(AnnotationToAttributeRector::class, [ diff --git a/tests/Issues/AutoImport/config/configured_rule.php b/tests/Issues/AutoImport/config/configured_rule.php index 2210b737b82..ee387b4091d 100644 --- a/tests/Issues/AutoImport/config/configured_rule.php +++ b/tests/Issues/AutoImport/config/configured_rule.php @@ -12,17 +12,23 @@ use Rector\Symfony\Symfony44\Rector\ClassMethod\ConsoleExecuteReturnIntRector; return static function (RectorConfig $rectorConfig): void { + /** enables @see \Rector\PostRector\Rector\NameImportingPostRector */ $rectorConfig->importNames(); + $rectorConfig->ruleWithConfiguration(RenameClassRector::class, [ 'Some\Exception' => 'Some\Target\Exception', 'DateTime' => 'DateTimeInterface', 'Phalcon\Logger' => 'Phalcon\Logger\Logger', ]); + $rectorConfig->rule(TernaryToNullCoalescingRector::class); + $rectorConfig->ruleWithConfiguration(AnnotationToAttributeRector::class, [ new AnnotationToAttribute('Doctrine\ORM\Mapping\Entity'), ]); + $rectorConfig->rules([ConsoleExecuteReturnIntRector::class, RemoveUnusedPrivatePropertyRector::class]); + $rectorConfig->ruleWithConfiguration(RenameFunctionRector::class, [ 'split' => 'explode', ]);