diff --git a/packages/file-system-rector/src/Rector/AbstractFileSystemRector.php b/packages/file-system-rector/src/Rector/AbstractFileSystemRector.php index 4af87d80348..3006764fc09 100644 --- a/packages/file-system-rector/src/Rector/AbstractFileSystemRector.php +++ b/packages/file-system-rector/src/Rector/AbstractFileSystemRector.php @@ -9,7 +9,6 @@ use PhpParser\Lexer; use PhpParser\Node; use PhpParser\ParserFactory; use Rector\Autodiscovery\ValueObject\NodesWithFileDestinationValueObject; -use Rector\Core\Application\FileSystem\RemovedAndAddedFilesCollector; use Rector\Core\Configuration\Configuration; use Rector\Core\PhpParser\Parser\Parser; use Rector\Core\PhpParser\Printer\BetterStandardPrinter; @@ -61,11 +60,6 @@ abstract class AbstractFileSystemRector implements FileSystemRectorInterface */ private $nodeScopeAndMetadataDecorator; - /** - * @var RemovedAndAddedFilesCollector - */ - private $removedAndAddedFilesCollector; - /** * @var ParserFactory */ @@ -85,7 +79,6 @@ abstract class AbstractFileSystemRector implements FileSystemRectorInterface Lexer $lexer, FormatPerservingPrinter $formatPerservingPrinter, NodeScopeAndMetadataDecorator $nodeScopeAndMetadataDecorator, - RemovedAndAddedFilesCollector $removedAndAddedFilesCollector, Configuration $configuration, BetterStandardPrinter $betterStandardPrinter, ParameterProvider $parameterProvider, @@ -96,7 +89,6 @@ abstract class AbstractFileSystemRector implements FileSystemRectorInterface $this->lexer = $lexer; $this->formatPerservingPrinter = $formatPerservingPrinter; $this->nodeScopeAndMetadataDecorator = $nodeScopeAndMetadataDecorator; - $this->removedAndAddedFilesCollector = $removedAndAddedFilesCollector; $this->configuration = $configuration; $this->betterStandardPrinter = $betterStandardPrinter; $this->parameterProvider = $parameterProvider; @@ -197,21 +189,6 @@ abstract class AbstractFileSystemRector implements FileSystemRectorInterface ); } - protected function moveFile(SmartFileInfo $oldFileInfo, string $newFileLocation, ?string $fileContent = null): void - { - $this->removedAndAddedFilesCollector->addMovedFile($oldFileInfo, $newFileLocation, $fileContent); - } - - protected function removeFile(SmartFileInfo $smartFileInfo): void - { - $this->removedAndAddedFilesCollector->removeFile($smartFileInfo); - } - - private function addFile(string $filePath, string $content): void - { - $this->removedAndAddedFilesCollector->addFileWithContent($filePath, $content); - } - /** * Also without FQN "\" that are added by basic printer */ diff --git a/rules/nette-kdyby/src/Rector/MethodCall/ReplaceMagicPropertyEventWithEventClassRector.php b/rules/nette-kdyby/src/Rector/MethodCall/ReplaceMagicPropertyEventWithEventClassRector.php index 4769a3cf128..aadf81757b8 100644 --- a/rules/nette-kdyby/src/Rector/MethodCall/ReplaceMagicPropertyEventWithEventClassRector.php +++ b/rules/nette-kdyby/src/Rector/MethodCall/ReplaceMagicPropertyEventWithEventClassRector.php @@ -16,7 +16,6 @@ use PhpParser\Node\Name\FullyQualified; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\Property; use Rector\CodingStyle\Naming\ClassNaming; -use Rector\Core\Application\FileSystem\RemovedAndAddedFilesCollector; use Rector\Core\Rector\AbstractRector; use Rector\Core\RectorDefinition\CodeSample; use Rector\Core\RectorDefinition\RectorDefinition; @@ -46,21 +45,14 @@ final class ReplaceMagicPropertyEventWithEventClassRector extends AbstractRector */ private $classNaming; - /** - * @var RemovedAndAddedFilesCollector - */ - private $removedAndAddedFilesCollector; - public function __construct( EventClassNaming $eventClassNaming, CustomEventFactory $customEventFactory, - ClassNaming $classNaming, - RemovedAndAddedFilesCollector $removedAndAddedFilesCollector + ClassNaming $classNaming ) { $this->eventClassNaming = $eventClassNaming; $this->customEventFactory = $customEventFactory; $this->classNaming = $classNaming; - $this->removedAndAddedFilesCollector = $removedAndAddedFilesCollector; } public function getDefinition(): RectorDefinition @@ -123,9 +115,8 @@ PHP $eventFileLocation = $this->eventClassNaming->resolveEventFileLocationFromMethodCall($node); // 3. create new event class with args - $eventClass = $this->customEventFactory->create($eventClassName, (array) $node->args); - $eventContent = $this->printFile($eventClass); - $this->removedAndAddedFilesCollector->addFileWithContent($eventFileLocation, $eventContent); + $eventClassInNamespace = $this->customEventFactory->create($eventClassName, (array) $node->args); + $this->printNodesToFilePath($eventClassInNamespace, $eventFileLocation); // 4. ad disatch method call $dispatchMethodCall = $this->createDispatchMethodCall($eventClassName); diff --git a/rules/nette-kdyby/tests/Rector/MethodCall/ReplaceEventManagerWithEventSubscriberRector/Source/ExpectedSomeClassCopyEvent.php b/rules/nette-kdyby/tests/Rector/MethodCall/ReplaceEventManagerWithEventSubscriberRector/Source/ExpectedSomeClassCopyEvent.php new file mode 100644 index 00000000000..83e55196880 --- /dev/null +++ b/rules/nette-kdyby/tests/Rector/MethodCall/ReplaceEventManagerWithEventSubscriberRector/Source/ExpectedSomeClassCopyEvent.php @@ -0,0 +1,28 @@ +this = $this; + $this->key = $key; + } + public function getThis(): static + { + return $this->this; + } + public function getKey(): string + { + return $this->key; + } +} diff --git a/src/Rector/AbstractRector/RemovedAndAddedFilesTrait.php b/src/Rector/AbstractRector/RemovedAndAddedFilesTrait.php index 9ec59271d74..26398af1701 100644 --- a/src/Rector/AbstractRector/RemovedAndAddedFilesTrait.php +++ b/src/Rector/AbstractRector/RemovedAndAddedFilesTrait.php @@ -7,6 +7,7 @@ namespace Rector\Core\Rector\AbstractRector; use PhpParser\Node; use Rector\Core\Application\FileSystem\RemovedAndAddedFilesCollector; use Rector\Core\PhpParser\Printer\BetterStandardPrinter; +use Symplify\SmartFileSystem\SmartFileInfo; /** * This could be part of @see AbstractRector, but decopuling to trait @@ -38,4 +39,19 @@ trait RemovedAndAddedFilesTrait $eventContent = $this->betterStandardPrinter->prettyPrintFile($node); $this->removedAndAddedFilesCollector->addFileWithContent($fileLocation, $eventContent); } + + protected function moveFile(SmartFileInfo $oldFileInfo, string $newFileLocation, ?string $fileContent = null): void + { + $this->removedAndAddedFilesCollector->addMovedFile($oldFileInfo, $newFileLocation, $fileContent); + } + + protected function removeFile(SmartFileInfo $smartFileInfo): void + { + $this->removedAndAddedFilesCollector->removeFile($smartFileInfo); + } + + private function addFile(string $filePath, string $content): void + { + $this->removedAndAddedFilesCollector->addFileWithContent($filePath, $content); + } }