From 677df861ec88dcb6ca231bab4ee2a8741bf66d97 Mon Sep 17 00:00:00 2001 From: Denny Lubitz Date: Fri, 18 Oct 2024 19:24:27 +0200 Subject: [PATCH] TASK: Add migration for getLabel with NodeLabelGeneratorRector --- config/set/contentrepository-90.php | 4 ++ .../Rules/NodeLabelGeneratorRector.php | 60 +++++++++++++++++++ .../Fixture/some_class.php.inc | 29 +++++++++ .../NodeLabelGeneratorRectorTest.php | 31 ++++++++++ .../config/configured_rule.php | 17 ++++++ 5 files changed, 141 insertions(+) create mode 100644 src/ContentRepository90/Rules/NodeLabelGeneratorRector.php create mode 100644 tests/ContentRepository90/Rules/NodeLabelGeneratorRector/Fixture/some_class.php.inc create mode 100644 tests/ContentRepository90/Rules/NodeLabelGeneratorRector/NodeLabelGeneratorRectorTest.php create mode 100644 tests/ContentRepository90/Rules/NodeLabelGeneratorRector/config/configured_rule.php diff --git a/config/set/contentrepository-90.php b/config/set/contentrepository-90.php index cb5ace8..2d18cd8 100644 --- a/config/set/contentrepository-90.php +++ b/config/set/contentrepository-90.php @@ -4,6 +4,7 @@ use Neos\ContentRepository\Core\NodeType\NodeType; use Neos\ContentRepository\Core\Projection\ContentGraph\Node; use Neos\ContentRepositoryRegistry\ContentRepositoryRegistry; +use Neos\Neos\Domain\NodeLabel\NodeLabelGeneratorInterface; use Neos\Neos\Domain\Service\RenderingModeService; use Neos\Rector\ContentRepository90\Legacy\LegacyContextStub; use Neos\Rector\ContentRepository90\Legacy\NodeLegacyStub; @@ -46,6 +47,7 @@ use Neos\Rector\ContentRepository90\Rules\NodeGetPathRector; use Neos\Rector\ContentRepository90\Rules\NodeIsHiddenInIndexRector; use Neos\Rector\ContentRepository90\Rules\NodeIsHiddenRector; +use Neos\Rector\ContentRepository90\Rules\NodeLabelGeneratorRector; use Neos\Rector\ContentRepository90\Rules\NodeTypeAllowsGrandchildNodeTypeRector; use Neos\Rector\ContentRepository90\Rules\NodeTypeGetAutoCreatedChildNodesRector; use Neos\Rector\ContentRepository90\Rules\NodeTypeGetNameRector; @@ -137,6 +139,7 @@ $fusionFlowQueryPropertyToComments[] = new FusionFlowQueryNodePropertyToWarningComment('_name', 'Line %LINE: !! You very likely need to rewrite "q(VARIABLE).property("_name")" to "VARIABLE.nodeName.value". We did not auto-apply this migration because we cannot be sure whether the variable is a Node.'); // getLabel $rectorConfig->rule(FusionNodeLabelRector::class); + $rectorConfig->rule(NodeLabelGeneratorRector::class); // setProperty // hasProperty -> compatible with ES CR Node (nothing to do) // getProperty -> compatible with ES CR Node (nothing to do) @@ -469,6 +472,7 @@ $rectorConfig->ruleWithConfiguration(InjectServiceIfNeededRector::class, [ new AddInjection('contentRepositoryRegistry', ContentRepositoryRegistry::class), new AddInjection('renderingModeService', RenderingModeService::class), + new AddInjection('nodeLabelGenerator', NodeLabelGeneratorInterface::class), ]); // TODO: does not fully seem to work.$rectorConfig->rule(RemoveDuplicateCommentRector::class); }; diff --git a/src/ContentRepository90/Rules/NodeLabelGeneratorRector.php b/src/ContentRepository90/Rules/NodeLabelGeneratorRector.php new file mode 100644 index 0000000..d5e627c --- /dev/null +++ b/src/ContentRepository90/Rules/NodeLabelGeneratorRector.php @@ -0,0 +1,60 @@ +getLabel()" will be rewritten.', __CLASS__); + } + + /** + * @return array> + */ + public function getNodeTypes() : array + { + return [Node\Expr\MethodCall::class]; + } + /** + * @param Node\Expr\MethodCall $node + */ + public function refactor(Node $node) : ?Node + { + assert($node instanceof Node\Expr\MethodCall); + + if (!$this->isObjectType($node->var, new ObjectType(NodeLegacyStub::class))) { + return null; + } + + if (!$this->isName($node->name, 'getLabel')) { + return null; + } + + return $this->nodeFactory->createMethodCall( + $this->nodeFactory->createPropertyFetch( + 'this', + 'nodeLabelGenerator' + ), + 'getLabel', + [$node->var] + ); + } +} diff --git a/tests/ContentRepository90/Rules/NodeLabelGeneratorRector/Fixture/some_class.php.inc b/tests/ContentRepository90/Rules/NodeLabelGeneratorRector/Fixture/some_class.php.inc new file mode 100644 index 0000000..1b1e69d --- /dev/null +++ b/tests/ContentRepository90/Rules/NodeLabelGeneratorRector/Fixture/some_class.php.inc @@ -0,0 +1,29 @@ +getLabel(); + } +} + +?> +----- +nodeLabelGenerator->getLabel($node); + } +} + +?> diff --git a/tests/ContentRepository90/Rules/NodeLabelGeneratorRector/NodeLabelGeneratorRectorTest.php b/tests/ContentRepository90/Rules/NodeLabelGeneratorRector/NodeLabelGeneratorRectorTest.php new file mode 100644 index 0000000..1f03fe9 --- /dev/null +++ b/tests/ContentRepository90/Rules/NodeLabelGeneratorRector/NodeLabelGeneratorRectorTest.php @@ -0,0 +1,31 @@ +doTestFile($fileInfo); + } + + /** + * @return \Iterator + */ + public function provideData(): \Iterator + { + return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/configured_rule.php'; + } +} diff --git a/tests/ContentRepository90/Rules/NodeLabelGeneratorRector/config/configured_rule.php b/tests/ContentRepository90/Rules/NodeLabelGeneratorRector/config/configured_rule.php new file mode 100644 index 0000000..4b1c2ce --- /dev/null +++ b/tests/ContentRepository90/Rules/NodeLabelGeneratorRector/config/configured_rule.php @@ -0,0 +1,17 @@ +rule(NodeLabelGeneratorRector::class); + + $rectorConfig->ruleWithConfiguration(InjectServiceIfNeededRector::class, [ + new AddInjection('nodeLabelGenerator', NodeLabelGeneratorInterface::class), + ]); +}; \ No newline at end of file