Skip to content

Commit f5c7bbd

Browse files
authored
Cleanup (#7731)
* extract LaravelClassName * perf checks PostFileProcessor * tidy up
1 parent 4b78d12 commit f5c7bbd

8 files changed

Lines changed: 48 additions & 32 deletions

File tree

rules/Privatization/Guard/LaravelModelGuard.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PhpParser\Node\Stmt\ClassMethod;
99
use PHPStan\Reflection\ClassReflection;
1010
use PHPStan\Type\ObjectType;
11+
use Rector\Enum\LaravelClassName;
1112
use Rector\NodeNameResolver\NodeNameResolver;
1213
use Rector\NodeTypeResolver\NodeTypeResolver;
1314
use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer;
@@ -39,7 +40,7 @@ public function __construct(
3940

4041
public function isProtectedMethod(ClassReflection $classReflection, ClassMethod $classMethod): bool
4142
{
42-
if (! $classReflection->is('Illuminate\Database\Eloquent\Model')) {
43+
if (! $classReflection->is(LaravelClassName::MODEL)) {
4344
return false;
4445
}
4546

@@ -63,7 +64,7 @@ private function isAttributeMethod(string $name, ClassMethod $classMethod): bool
6364

6465
return $this->nodeTypeResolver->isObjectType(
6566
$classMethod->returnType,
66-
new ObjectType('Illuminate\Database\Eloquent\Casts\Attribute')
67+
new ObjectType(LaravelClassName::CAST_ATTRIBUTE)
6768
);
6869
}
6970

@@ -73,9 +74,6 @@ private function isScopeMethod(string $name, ClassMethod $classMethod): bool
7374
return true;
7475
}
7576

76-
return $this->phpAttributeAnalyzer->hasPhpAttribute(
77-
$classMethod,
78-
'Illuminate\Database\Eloquent\Attributes\Scope'
79-
);
77+
return $this->phpAttributeAnalyzer->hasPhpAttribute($classMethod, LaravelClassName::ATTRIBUTES_SCOPE);
8078
}
8179
}

rules/Privatization/Guard/ParentClassMagicCallGuard.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
namespace Rector\Privatization\Guard;
66

7-
use PhpParser\PrettyPrinterAbstract;
87
use PhpParser\Node\Expr;
98
use PhpParser\Node\Expr\MethodCall;
109
use PhpParser\Node\Name;
1110
use PhpParser\Node\Stmt\Class_;
1211
use PhpParser\PrettyPrinter\Standard;
12+
use PhpParser\PrettyPrinterAbstract;
1313
use Rector\NodeNameResolver\NodeNameResolver;
1414
use Rector\PhpParser\AstResolver;
1515
use Rector\PhpParser\Node\BetterNodeFinder;

src/Enum/LaravelClassName.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Enum;
6+
7+
final class LaravelClassName
8+
{
9+
public const MODEL = 'Illuminate\Database\Eloquent\Model';
10+
11+
public const CAST_ATTRIBUTE = 'Illuminate\Database\Eloquent\Casts\Attribute';
12+
13+
public const ATTRIBUTES_SCOPE = 'Illuminate\Database\Eloquent\Attributes\Scope';
14+
}

src/PostRector/Application/PostFileProcessor.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,6 @@ private function getPostRectors(): array
9797

9898
$isRenamedClassEnabled = $this->renamedClassesDataCollector->getOldToNewClasses() !== [];
9999
$isNameImportingEnabled = SimpleParameterProvider::provideBoolParameter(Option::AUTO_IMPORT_NAMES);
100-
$isDocblockNameImportingEnabled = SimpleParameterProvider::provideBoolParameter(
101-
Option::AUTO_IMPORT_DOC_BLOCK_NAMES
102-
);
103100

104101
$isRemovingUnusedImportsEnabled = SimpleParameterProvider::provideBoolParameter(Option::REMOVE_UNUSED_IMPORTS);
105102

@@ -113,11 +110,11 @@ private function getPostRectors(): array
113110
// import names
114111
if ($isNameImportingEnabled) {
115112
$postRectors[] = $this->nameImportingPostRector;
116-
}
117113

118-
// import docblocks
119-
if ($isNameImportingEnabled && $isDocblockNameImportingEnabled) {
120-
$postRectors[] = $this->docblockNameImportingPostRector;
114+
// import docblocks
115+
if (SimpleParameterProvider::provideBoolParameter(Option::AUTO_IMPORT_DOC_BLOCK_NAMES)) {
116+
$postRectors[] = $this->docblockNameImportingPostRector;
117+
}
121118
}
122119

123120
$postRectors[] = $this->useAddingPostRector;

src/Testing/TestingParser/TestingParser.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,7 @@ public function __construct(
2727

2828
public function parseFilePathToFile(string $filePath): File
2929
{
30-
// needed for PHPStan reflection, as it caches the last processed file
31-
$this->dynamicSourceLocatorProvider->setFilePath($filePath);
32-
33-
$fileContent = FileSystem::read($filePath);
34-
$file = new File($filePath, $fileContent);
35-
$stmts = $this->rectorParser->parseString($fileContent);
36-
37-
$stmts = $this->nodeScopeAndMetadataDecorator->decorateNodesFromFile($filePath, $stmts);
38-
39-
$file->hydrateStmtsAndTokens($stmts, $stmts, []);
40-
$this->currentFileProvider->setFile($file);
30+
[$file, $stmts] = $this->parseToFileAndStmts($filePath);
4131

4232
return $file;
4333
}
@@ -46,19 +36,29 @@ public function parseFilePathToFile(string $filePath): File
4636
* @return Node[]
4737
*/
4838
public function parseFileToDecoratedNodes(string $filePath): array
39+
{
40+
[$file, $stmts] = $this->parseToFileAndStmts($filePath);
41+
42+
return $stmts;
43+
}
44+
45+
/**
46+
* @return array{0: File, 1: Node[]}
47+
*/
48+
private function parseToFileAndStmts(string $filePath): array
4949
{
5050
// needed for PHPStan reflection, as it caches the last processed file
5151
$this->dynamicSourceLocatorProvider->setFilePath($filePath);
5252

5353
$fileContent = FileSystem::read($filePath);
54-
$stmts = $this->rectorParser->parseString($fileContent);
5554
$file = new File($filePath, $fileContent);
5655

56+
$stmts = $this->rectorParser->parseString($fileContent);
5757
$stmts = $this->nodeScopeAndMetadataDecorator->decorateNodesFromFile($filePath, $stmts);
58-
$file->hydrateStmtsAndTokens($stmts, $stmts, []);
5958

59+
$file->hydrateStmtsAndTokens($stmts, $stmts, []);
6060
$this->currentFileProvider->setFile($file);
6161

62-
return $stmts;
62+
return [$file, $stmts];
6363
}
6464
}

tests/Comments/CommentRemover/Fixture/another_comment.php.inc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Rector\Tests\Comments\CommentRemover\Fixture;
44

5-
$values = new class
5+
final class AnotherComment
66
{
77
public function run($value)
88
{
@@ -13,13 +13,13 @@ $values = new class
1313
return 'https://some_very_long_link.cz'; /* here too */
1414
}
1515
}
16-
};
16+
}
1717

1818
?>
1919
-----
2020
namespace Rector\Tests\Comments\CommentRemover\Fixture;
2121

22-
$values = new class
22+
final class AnotherComment
2323
{
2424
public function run($value)
2525
{
@@ -28,4 +28,4 @@ $values = new class
2828
return 'https://some_very_long_link.cz';
2929
}
3030
}
31-
};
31+
}

tests/Issues/AnnotationToAttributeRenameAutoImport/config/configured_rule.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Rector\Renaming\Rector\Name\RenameClassRector;
99

1010
return static function (RectorConfig $rectorConfig): void {
11+
/** triggers @see \Rector\NodeTypeResolver\PhpDocNodeVisitor\NameImportingPhpDocNodeVisitor */
1112
$rectorConfig->importNames();
1213

1314
$rectorConfig->ruleWithConfiguration(AnnotationToAttributeRector::class, [

tests/Issues/AutoImport/config/configured_rule.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,23 @@
1212
use Rector\Symfony\Symfony44\Rector\ClassMethod\ConsoleExecuteReturnIntRector;
1313

1414
return static function (RectorConfig $rectorConfig): void {
15+
/** enables @see \Rector\PostRector\Rector\NameImportingPostRector */
1516
$rectorConfig->importNames();
17+
1618
$rectorConfig->ruleWithConfiguration(RenameClassRector::class, [
1719
'Some\Exception' => 'Some\Target\Exception',
1820
'DateTime' => 'DateTimeInterface',
1921
'Phalcon\Logger' => 'Phalcon\Logger\Logger',
2022
]);
23+
2124
$rectorConfig->rule(TernaryToNullCoalescingRector::class);
25+
2226
$rectorConfig->ruleWithConfiguration(AnnotationToAttributeRector::class, [
2327
new AnnotationToAttribute('Doctrine\ORM\Mapping\Entity'),
2428
]);
29+
2530
$rectorConfig->rules([ConsoleExecuteReturnIntRector::class, RemoveUnusedPrivatePropertyRector::class]);
31+
2632
$rectorConfig->ruleWithConfiguration(RenameFunctionRector::class, [
2733
'split' => 'explode',
2834
]);

0 commit comments

Comments
 (0)