diff --git a/packages-tests/ChangesReporting/Annotation/AppliedRectorsChangelogResolver/RectorsChangelogResolverTest.php b/packages-tests/ChangesReporting/Annotation/AppliedRectorsChangelogResolver/RectorsChangelogResolverTest.php index 843054e1687..6f819ff27ce 100644 --- a/packages-tests/ChangesReporting/Annotation/AppliedRectorsChangelogResolver/RectorsChangelogResolverTest.php +++ b/packages-tests/ChangesReporting/Annotation/AppliedRectorsChangelogResolver/RectorsChangelogResolverTest.php @@ -5,7 +5,7 @@ declare(strict_types=1); namespace Rector\Tests\ChangesReporting\Annotation\AppliedRectorsChangelogResolver; use Rector\ChangesReporting\Annotation\RectorsChangelogResolver; -use Rector\ChangesReporting\ValueObject\RectorWithFileAndLineChange; +use Rector\ChangesReporting\ValueObject\RectorWithLineChange; use Rector\Core\HttpKernel\RectorKernel; use Rector\Core\ValueObject\Reporting\FileDiff; use Rector\Tests\ChangesReporting\Annotation\AppliedRectorsChangelogResolver\Source\RectorWithChangelog; @@ -46,30 +46,12 @@ final class RectorsChangelogResolverTest extends AbstractKernelTestCase private function createFileDiff(): FileDiff { // This is by intention to test the array_unique functionality - $rectorWithFileAndLineChange1 = new RectorWithFileAndLineChange( - new RectorWithChangelog(), - __DIR__ . '/Source/RectorWithChangelog.php', - 1 - ); + $rectorWithLineChange = new RectorWithLineChange(new RectorWithChangelog(), 1); + $rectorWithLineChange2 = new RectorWithLineChange(new RectorWithChangelog(), 1); + $rectorWithLineChange3 = new RectorWithLineChange(new RectorWithOutChangelog(), 1); - $rectorWithFileAndLineChange2 = new RectorWithFileAndLineChange( - new RectorWithChangelog(), - __DIR__ . '/Source/RectorWithChangelog.php', - 1 - ); + $rectorWithLineChanges = [$rectorWithLineChange, $rectorWithLineChange2, $rectorWithLineChange3]; - $rectorWithFileAndLineChange3 = new RectorWithFileAndLineChange( - new RectorWithOutChangelog(), - __DIR__ . '/Source/RectorWithOutChangelog.php', - 1 - ); - - $rectorWithFileAndLineChanges = [ - $rectorWithFileAndLineChange1, - $rectorWithFileAndLineChange2, - $rectorWithFileAndLineChange3, - ]; - - return new FileDiff(new SmartFileInfo(__FILE__), 'foo', 'foo', $rectorWithFileAndLineChanges); + return new FileDiff(new SmartFileInfo(__FILE__), 'foo', 'foo', $rectorWithLineChanges); } } diff --git a/packages/ChangesReporting/Collector/RectorChangeCollector.php b/packages/ChangesReporting/Collector/RectorChangeCollector.php index 45c1c51fa28..a0f37e315bc 100644 --- a/packages/ChangesReporting/Collector/RectorChangeCollector.php +++ b/packages/ChangesReporting/Collector/RectorChangeCollector.php @@ -5,22 +5,15 @@ declare(strict_types=1); namespace Rector\ChangesReporting\Collector; use PhpParser\Node; -use Rector\ChangesReporting\ValueObject\RectorWithFileAndLineChange; use Rector\ChangesReporting\ValueObject\RectorWithLineChange; use Rector\Core\Contract\Rector\RectorInterface; use Rector\Core\Exception\ShouldNotHappenException; use Rector\Core\Logging\CurrentRectorProvider; use Rector\Core\Provider\CurrentFileProvider; use Rector\Core\ValueObject\Application\File; -use Symplify\SmartFileSystem\SmartFileInfo; final class RectorChangeCollector { - /** - * @var RectorWithFileAndLineChange[] - */ - private $rectorWithFileAndLineChanges = []; - /** * @var CurrentRectorProvider */ @@ -39,17 +32,10 @@ final class RectorChangeCollector $this->currentFileProvider = $currentFileProvider; } - /** - * @return RectorWithFileAndLineChange[] - */ - public function getRectorChangesByFileInfo(SmartFileInfo $smartFileInfo): array + public function notifyFileChange(File $file, Node $node, RectorInterface $rector): void { - return array_filter( - $this->rectorWithFileAndLineChanges, - function (RectorWithFileAndLineChange $rectorWithFileAndLineChange) use ($smartFileInfo): bool { - return $rectorWithFileAndLineChange->getRealPath() === $smartFileInfo->getRealPath(); - } - ); + $rectorWithLineChange = new RectorWithLineChange($rector, $node->getLine()); + $file->addRectorClassWithLine($rectorWithLineChange); } public function notifyNodeFileInfo(Node $node): void @@ -61,27 +47,12 @@ final class RectorChangeCollector return; } - $smartFileInfo = $file->getSmartFileInfo(); - $currentRector = $this->currentRectorProvider->getCurrentRector(); if (! $currentRector instanceof RectorInterface) { throw new ShouldNotHappenException(); } - // @old - $this->addRectorClassWithLine($currentRector, $smartFileInfo, $node->getLine()); - - // @new $rectorWithLineChange = new RectorWithLineChange($currentRector, $node->getLine()); $file->addRectorClassWithLine($rectorWithLineChange); } - - private function addRectorClassWithLine(RectorInterface $rector, SmartFileInfo $smartFileInfo, int $line): void - { - $this->rectorWithFileAndLineChanges[] = new RectorWithFileAndLineChange( - $rector, - $smartFileInfo->getRealPath(), - $line - ); - } } diff --git a/packages/ChangesReporting/ValueObject/RectorWithFileAndLineChange.php b/packages/ChangesReporting/ValueObject/RectorWithFileAndLineChange.php deleted file mode 100644 index 9aceabb04fc..00000000000 --- a/packages/ChangesReporting/ValueObject/RectorWithFileAndLineChange.php +++ /dev/null @@ -1,56 +0,0 @@ -rector = $rector; - $this->line = $line; - $this->realPath = $realPath; - } - - public function getRectorDefinitionsDescription(): string - { - $ruleDefinition = $this->rector->getRuleDefinition(); - return $ruleDefinition->getDescription(); - } - - /** - * @return class-string - */ - public function getRectorClass(): string - { - return get_class($this->rector); - } - - public function getLine(): int - { - return $this->line; - } - - public function getRealPath(): string - { - return $this->realPath; - } -} diff --git a/packages/ChangesReporting/ValueObjectFactory/FileDiffFactory.php b/packages/ChangesReporting/ValueObjectFactory/FileDiffFactory.php index 48ce27714d2..e712b0284ca 100644 --- a/packages/ChangesReporting/ValueObjectFactory/FileDiffFactory.php +++ b/packages/ChangesReporting/ValueObjectFactory/FileDiffFactory.php @@ -4,7 +4,6 @@ declare(strict_types=1); namespace Rector\ChangesReporting\ValueObjectFactory; -use Rector\ChangesReporting\Collector\RectorChangeCollector; use Rector\Core\Differ\DefaultDiffer; use Rector\Core\ValueObject\Application\File; use Rector\Core\ValueObject\Reporting\FileDiff; @@ -12,11 +11,6 @@ use Symplify\ConsoleColorDiff\Console\Output\ConsoleDiffer; final class FileDiffFactory { - /** - * @var RectorChangeCollector - */ - private $rectorChangeCollector; - /** * @var DefaultDiffer */ @@ -27,27 +21,20 @@ final class FileDiffFactory */ private $consoleDiffer; - public function __construct( - RectorChangeCollector $rectorChangeCollector, - DefaultDiffer $defaultDiffer, - ConsoleDiffer $consoleDiffer - ) { - $this->rectorChangeCollector = $rectorChangeCollector; + public function __construct(DefaultDiffer $defaultDiffer, ConsoleDiffer $consoleDiffer) + { $this->defaultDiffer = $defaultDiffer; $this->consoleDiffer = $consoleDiffer; } public function createFileDiff(File $file, string $oldContent, string $newContent): FileDiff { - $smartFileInfo = $file->getSmartFileInfo(); - $rectorChanges = $this->rectorChangeCollector->getRectorChangesByFileInfo($smartFileInfo); - // always keep the most recent diff return new FileDiff( - $smartFileInfo, + $file->getSmartFileInfo(), $this->defaultDiffer->diff($oldContent, $newContent), $this->consoleDiffer->diff($oldContent, $newContent), - $rectorChanges + $file->getRectorWithLineChanges() ); } } diff --git a/phpstan.neon b/phpstan.neon index 5227c104610..b99fbf9d91d 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -526,3 +526,7 @@ parameters: - '#Cannot call method getSmartFileInfo\(\) on Rector\\Core\\ValueObject\\Application\\File\|null#' - '#Class with base "Parser" name is already used in "Hoa\\Compiler\\Llk\\Parser", "Rector\\Core\\PhpParser\\Parser\\Parser"\. Use unique name to make classes easy to recognize#' - '#Parameter \#1 \$file of method Rector\\CodingStyle\\ClassNameImport\\ShortNameResolver\:\:resolveForNode\(\) expects Rector\\Core\\ValueObject\\Application\\File, Rector\\Core\\ValueObject\\Application\\File\|null given#' + + - + message: '#\$this as argument is not allowed\. Refactor method to service composition#' + path: rules/TypeDeclaration/Rector/ClassMethod/ParamTypeFromStrictTypedPropertyRector.php diff --git a/rules/Php80/Reflection/MethodReflectionClassMethodResolver.php b/rules/Php80/Reflection/MethodReflectionClassMethodResolver.php index 86d0b53881e..f1945fff973 100644 --- a/rules/Php80/Reflection/MethodReflectionClassMethodResolver.php +++ b/rules/Php80/Reflection/MethodReflectionClassMethodResolver.php @@ -36,6 +36,10 @@ final class MethodReflectionClassMethodResolver } $classReflection = $this->reflectionProvider->getClass($className); + if ($classReflection->isAnonymous()) { + return null; + } + if (! $classReflection->hasMethod($methodName)) { return null; } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeFromStrictTypedPropertyRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeFromStrictTypedPropertyRector.php index b50eafc3e0a..d041228f6ff 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeFromStrictTypedPropertyRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeFromStrictTypedPropertyRector.php @@ -134,7 +134,7 @@ CODE_SAMPLE return null; } - $this->rectorChangeCollector->notifyNodeFileInfo($node); + $this->rectorChangeCollector->notifyFileChange($this->file, $node, $this); $param->type = $singlePropertyTypeNode; return NodeTraverser::STOP_TRAVERSAL; diff --git a/src/Rector/AbstractRector.php b/src/Rector/AbstractRector.php index bcc60dc6f50..7ad5ccb8604 100644 --- a/src/Rector/AbstractRector.php +++ b/src/Rector/AbstractRector.php @@ -328,7 +328,8 @@ abstract class AbstractRector extends NodeVisitorAbstract implements PhpRectorIn // changed! if ($this->hasNodeChanged($originalNode, $node)) { $this->updateAttributes($node); - $this->rectorChangeCollector->notifyNodeFileInfo($node); + + $this->rectorChangeCollector->notifyFileChange($this->file, $node, $this); // update parents relations $this->connectParentNodes($node); diff --git a/src/ValueObject/Reporting/FileDiff.php b/src/ValueObject/Reporting/FileDiff.php index aa9180fd985..e8f334004a7 100644 --- a/src/ValueObject/Reporting/FileDiff.php +++ b/src/ValueObject/Reporting/FileDiff.php @@ -4,7 +4,7 @@ declare(strict_types=1); namespace Rector\Core\ValueObject\Reporting; -use Rector\ChangesReporting\ValueObject\RectorWithFileAndLineChange; +use Rector\ChangesReporting\ValueObject\RectorWithLineChange; use Rector\Core\Contract\Rector\RectorInterface; use Symplify\SmartFileSystem\SmartFileInfo; @@ -21,9 +21,9 @@ final class FileDiff private $diffConsoleFormatted; /** - * @var RectorWithFileAndLineChange[] + * @var RectorWithLineChange[] */ - private $rectorWithFileAndLineChanges = []; + private $rectorWithLineChanges = []; /** * @var SmartFileInfo @@ -31,17 +31,17 @@ final class FileDiff private $smartFileInfo; /** - * @param RectorWithFileAndLineChange[] $rectorWithFileAndLineChanges + * @param RectorWithLineChange[] $rectorWithLineChanges */ public function __construct( SmartFileInfo $smartFileInfo, string $diff, string $diffConsoleFormatted, - array $rectorWithFileAndLineChanges = [] + array $rectorWithLineChanges = [] ) { $this->smartFileInfo = $smartFileInfo; $this->diff = $diff; - $this->rectorWithFileAndLineChanges = $rectorWithFileAndLineChanges; + $this->rectorWithLineChanges = $rectorWithLineChanges; $this->diffConsoleFormatted = $diffConsoleFormatted; } @@ -66,11 +66,11 @@ final class FileDiff } /** - * @return RectorWithFileAndLineChange[] + * @return RectorWithLineChange[] */ public function getRectorChanges(): array { - return $this->rectorWithFileAndLineChanges; + return $this->rectorWithLineChanges; } /** @@ -79,8 +79,8 @@ final class FileDiff public function getRectorClasses(): array { $rectorClasses = []; - foreach ($this->rectorWithFileAndLineChanges as $rectorWithFileAndLineChange) { - $rectorClasses[] = $rectorWithFileAndLineChange->getRectorClass(); + foreach ($this->rectorWithLineChanges as $rectorWithLineChange) { + $rectorClasses[] = $rectorWithLineChange->getRectorClass(); } return $this->sortClasses($rectorClasses);