From 3ac63a2aa8ce8adefc06d551585243d00c6c88be Mon Sep 17 00:00:00 2001 From: Denny Lubitz Date: Fri, 29 Sep 2023 18:55:31 +0200 Subject: [PATCH 1/3] FEATURE: Rewrite NodeType::getName() --- .../Rules/FusionNodeTypeNameRector.php | 33 ++++++++++++ .../Rules/NodeTypeGetNameRector.php | 53 +++++++++++++++++++ .../Fixture/some_file.fusion.inc | 36 +++++++++++++ .../FusionNodeTypeNameRectorTest.php | 31 +++++++++++ .../config/configured_rule.php | 19 +++++++ .../NodeTypeGetNameRector/Fixture/all.php.inc | 27 ++++++++++ .../NodeTypeGetNameRectorTest.php | 31 +++++++++++ .../config/configured_rule.php | 11 ++++ 8 files changed, 241 insertions(+) create mode 100644 src/ContentRepository90/Rules/FusionNodeTypeNameRector.php create mode 100644 src/ContentRepository90/Rules/NodeTypeGetNameRector.php create mode 100644 tests/ContentRepository90/Rules/FusionNodeTypeNameRector/Fixture/some_file.fusion.inc create mode 100644 tests/ContentRepository90/Rules/FusionNodeTypeNameRector/FusionNodeTypeNameRectorTest.php create mode 100644 tests/ContentRepository90/Rules/FusionNodeTypeNameRector/config/configured_rule.php create mode 100644 tests/Rules/NodeTypeGetNameRector/Fixture/all.php.inc create mode 100644 tests/Rules/NodeTypeGetNameRector/NodeTypeGetNameRectorTest.php create mode 100644 tests/Rules/NodeTypeGetNameRector/config/configured_rule.php diff --git a/src/ContentRepository90/Rules/FusionNodeTypeNameRector.php b/src/ContentRepository90/Rules/FusionNodeTypeNameRector.php new file mode 100644 index 0000000..bb98cce --- /dev/null +++ b/src/ContentRepository90/Rules/FusionNodeTypeNameRector.php @@ -0,0 +1,33 @@ +process(fn(string $eelExpression) => preg_replace( + '/(node|documentNode|site)\.nodeType\.name/', + '$1.nodeTypeName.value', + $eelExpression + )) + ->addCommentsIfRegexMatches( + '/\.nodeType.name/', + '// TODO 9.0 migration: Line %LINE: You may need to rewrite "VARIABLE.nodeType.name" to VARIABLE.nodeTypeName.value. We did not auto-apply this migration because we cannot be sure whether the variable is a Node.' + )->getProcessedContent(); + } +} diff --git a/src/ContentRepository90/Rules/NodeTypeGetNameRector.php b/src/ContentRepository90/Rules/NodeTypeGetNameRector.php new file mode 100644 index 0000000..af6650b --- /dev/null +++ b/src/ContentRepository90/Rules/NodeTypeGetNameRector.php @@ -0,0 +1,53 @@ +> + */ + public function getNodeTypes(): array + { + return [\PhpParser\Node\Expr\MethodCall::class]; + } + + /** + * @param \PhpParser\Node\Expr\MethodCall $node + */ + public function refactor(Node $node): ?Node + { + assert($node instanceof Node\Expr\MethodCall); + + if (!$this->isObjectType($node->var, new ObjectType('Neos\ContentRepository\Core\NodeType\NodeType'))) { + return null; + } + if (!$this->isName($node->name, 'getName')) { + return null; + } + + $propertyFetchAggregateId = $this->nodeFactory->createPropertyFetch($node->var, 'name'); + return $this->nodeFactory->createPropertyFetch($propertyFetchAggregateId, 'value'); + } +} diff --git a/tests/ContentRepository90/Rules/FusionNodeTypeNameRector/Fixture/some_file.fusion.inc b/tests/ContentRepository90/Rules/FusionNodeTypeNameRector/Fixture/some_file.fusion.inc new file mode 100644 index 0000000..f414eb5 --- /dev/null +++ b/tests/ContentRepository90/Rules/FusionNodeTypeNameRector/Fixture/some_file.fusion.inc @@ -0,0 +1,36 @@ +prototype(Neos.Fusion.Form:Checkbox) < prototype(Neos.Fusion.Form:Component.Field) { + +renderer = Neos.Fusion:Component { + +# +# pass down props +# +attributes = ${node.nodeType.name || documentNode.nodeType.name} +renderer = afx` + +` +} +} +----- +// TODO 9.0 migration: Line 13: You may need to rewrite "VARIABLE.nodeType.name" to VARIABLE.nodeTypeName.value. We did not auto-apply this migration because we cannot be sure whether the variable is a Node. +prototype(Neos.Fusion.Form:Checkbox) < prototype(Neos.Fusion.Form:Component.Field) { + +renderer = Neos.Fusion:Component { + +# +# pass down props +# +attributes = ${node.nodeTypeName.value || documentNode.nodeTypeName.value} +renderer = afx` + +` +} +} diff --git a/tests/ContentRepository90/Rules/FusionNodeTypeNameRector/FusionNodeTypeNameRectorTest.php b/tests/ContentRepository90/Rules/FusionNodeTypeNameRector/FusionNodeTypeNameRectorTest.php new file mode 100644 index 0000000..c5a7d79 --- /dev/null +++ b/tests/ContentRepository90/Rules/FusionNodeTypeNameRector/FusionNodeTypeNameRectorTest.php @@ -0,0 +1,31 @@ +doTestFile($fileInfo); + } + + /** + * @return \Iterator + */ + public function provideData(): \Iterator + { + return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture', '*.fusion.inc'); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/configured_rule.php'; + } +} diff --git a/tests/ContentRepository90/Rules/FusionNodeTypeNameRector/config/configured_rule.php b/tests/ContentRepository90/Rules/FusionNodeTypeNameRector/config/configured_rule.php new file mode 100644 index 0000000..6453e6b --- /dev/null +++ b/tests/ContentRepository90/Rules/FusionNodeTypeNameRector/config/configured_rule.php @@ -0,0 +1,19 @@ +services(); + $services->defaults() + ->public() + ->autowire() + ->autoconfigure(); + $services->set(FusionFileProcessor::class); + $rectorConfig->disableParallel(); // does not work for fusion files - see https://github.com/rectorphp/rector-src/pull/2597#issuecomment-1190120688 + + $rectorConfig->rule(FusionNodeTypeNameRector::class); +}; diff --git a/tests/Rules/NodeTypeGetNameRector/Fixture/all.php.inc b/tests/Rules/NodeTypeGetNameRector/Fixture/all.php.inc new file mode 100644 index 0000000..b51824e --- /dev/null +++ b/tests/Rules/NodeTypeGetNameRector/Fixture/all.php.inc @@ -0,0 +1,27 @@ +getName(); + } +} + +?> +----- +name->value; + } +} + +?> diff --git a/tests/Rules/NodeTypeGetNameRector/NodeTypeGetNameRectorTest.php b/tests/Rules/NodeTypeGetNameRector/NodeTypeGetNameRectorTest.php new file mode 100644 index 0000000..f56bda0 --- /dev/null +++ b/tests/Rules/NodeTypeGetNameRector/NodeTypeGetNameRectorTest.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/Rules/NodeTypeGetNameRector/config/configured_rule.php b/tests/Rules/NodeTypeGetNameRector/config/configured_rule.php new file mode 100644 index 0000000..e36b0ea --- /dev/null +++ b/tests/Rules/NodeTypeGetNameRector/config/configured_rule.php @@ -0,0 +1,11 @@ +rule(NodeTypeGetNameRector::class); +}; From 1c8cb1c78c4e39becaba2ea224701f87c2c97bbf Mon Sep 17 00:00:00 2001 From: Denny Lubitz Date: Fri, 29 Sep 2023 19:00:55 +0200 Subject: [PATCH 2/3] FEATURE: Rewrite NodeType::getName() --- src/ContentRepository90/Rules/NodeTypeGetNameRector.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ContentRepository90/Rules/NodeTypeGetNameRector.php b/src/ContentRepository90/Rules/NodeTypeGetNameRector.php index af6650b..0deac54 100644 --- a/src/ContentRepository90/Rules/NodeTypeGetNameRector.php +++ b/src/ContentRepository90/Rules/NodeTypeGetNameRector.php @@ -22,7 +22,7 @@ public function __construct( public function getRuleDefinition(): RuleDefinition { - return CodeSampleLoader::fromFile('"NodeInterface::getChildNodes()" will be rewritten', __CLASS__); + return CodeSampleLoader::fromFile('"NodeType::getName()" will be rewritten', __CLASS__); } /** From 7597bc9cbf1f4bd9e510ffa2399b5c164b462ec2 Mon Sep 17 00:00:00 2001 From: Denny Lubitz Date: Fri, 29 Sep 2023 20:29:45 +0200 Subject: [PATCH 3/3] FEATURE: Rewrite NodeType::getName() --- config/set/contentrepository-90.php | 9 +++++++++ .../Rules/FusionNodeTypeNameRector.php | 2 +- .../Fixture/some_file.fusion.inc | 2 +- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/config/set/contentrepository-90.php b/config/set/contentrepository-90.php index 5025602..7d73fad 100644 --- a/config/set/contentrepository-90.php +++ b/config/set/contentrepository-90.php @@ -45,6 +45,8 @@ use Rector\Transform\ValueObject\MethodCallToPropertyFetch; use Neos\Rector\ContentRepository90\Rules\WorkspaceGetNameRector; use Neos\Rector\ContentRepository90\Rules\NodeGetIdentifierRector; +use Neos\Rector\ContentRepository90\Rules\FusionNodeTypeNameRector; +use Neos\Rector\ContentRepository90\Rules\NodeTypeGetNameRector; return static function (RectorConfig $rectorConfig): void { // Register FusionFileProcessor. All Fusion Rectors will be auto-registered at this processor. @@ -215,6 +217,13 @@ // hasProperty() ** (included/compatible in old NodeInterface) // getLabel() ** (included/compatible in old NodeInterface) + /** + * Neos\ContentRepository\Core\NodeType\NodeType + */ + // getName() + $rectorConfig->rule(rectorClass: FusionNodeTypeNameRector::class); + $rectorConfig->rule(rectorClass: NodeTypeGetNameRector::class); + /** * Neos\ContentRepository\Domain\Projection\Content\TraversableNodeInterface */ diff --git a/src/ContentRepository90/Rules/FusionNodeTypeNameRector.php b/src/ContentRepository90/Rules/FusionNodeTypeNameRector.php index bb98cce..11bf397 100644 --- a/src/ContentRepository90/Rules/FusionNodeTypeNameRector.php +++ b/src/ContentRepository90/Rules/FusionNodeTypeNameRector.php @@ -27,7 +27,7 @@ public function refactorFileContent(string $fileContent): string )) ->addCommentsIfRegexMatches( '/\.nodeType.name/', - '// TODO 9.0 migration: Line %LINE: You may need to rewrite "VARIABLE.nodeType.name" to VARIABLE.nodeTypeName.value. We did not auto-apply this migration because we cannot be sure whether the variable is a Node.' + '// TODO 9.0 migration: Line %LINE: You may need to rewrite "VARIABLE.nodeType.name" to "VARIABLE.nodeTypeName.value". We did not auto-apply this migration because we cannot be sure whether the variable is a Node.' )->getProcessedContent(); } } diff --git a/tests/ContentRepository90/Rules/FusionNodeTypeNameRector/Fixture/some_file.fusion.inc b/tests/ContentRepository90/Rules/FusionNodeTypeNameRector/Fixture/some_file.fusion.inc index f414eb5..23010be 100644 --- a/tests/ContentRepository90/Rules/FusionNodeTypeNameRector/Fixture/some_file.fusion.inc +++ b/tests/ContentRepository90/Rules/FusionNodeTypeNameRector/Fixture/some_file.fusion.inc @@ -16,7 +16,7 @@ renderer = afx` } } ----- -// TODO 9.0 migration: Line 13: You may need to rewrite "VARIABLE.nodeType.name" to VARIABLE.nodeTypeName.value. We did not auto-apply this migration because we cannot be sure whether the variable is a Node. +// TODO 9.0 migration: Line 13: You may need to rewrite "VARIABLE.nodeType.name" to "VARIABLE.nodeTypeName.value". We did not auto-apply this migration because we cannot be sure whether the variable is a Node. prototype(Neos.Fusion.Form:Checkbox) < prototype(Neos.Fusion.Form:Component.Field) { renderer = Neos.Fusion:Component {