Skip to content
Merged

Cleanup #7731

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
10 changes: 4 additions & 6 deletions rules/Privatization/Guard/LaravelModelGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand All @@ -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)
);
}

Expand All @@ -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);
}
}
2 changes: 1 addition & 1 deletion rules/Privatization/Guard/ParentClassMagicCallGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 14 additions & 0 deletions src/Enum/LaravelClassName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Rector\Enum;

final class LaravelClassName
{
public const MODEL = 'Illuminate\Database\Eloquent\Model';

public const CAST_ATTRIBUTE = 'Illuminate\Database\Eloquent\Casts\Attribute';

public const ATTRIBUTES_SCOPE = 'Illuminate\Database\Eloquent\Attributes\Scope';
}
11 changes: 4 additions & 7 deletions src/PostRector/Application/PostFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,6 @@ private function getPostRectors(): array

$isRenamedClassEnabled = $this->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);

Expand All @@ -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;
Expand Down
28 changes: 14 additions & 14 deletions src/Testing/TestingParser/TestingParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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];
}
}
8 changes: 4 additions & 4 deletions tests/Comments/CommentRemover/Fixture/another_comment.php.inc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Rector\Tests\Comments\CommentRemover\Fixture;

$values = new class
final class AnotherComment
{
public function run($value)
{
Expand All @@ -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)
{
Expand All @@ -28,4 +28,4 @@ $values = new class
return 'https://some_very_long_link.cz';
}
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -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, [
Expand Down
6 changes: 6 additions & 0 deletions tests/Issues/AutoImport/config/configured_rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
]);
Expand Down