Skip to content

Commit

Permalink
TASK: Migrate ${node.nodeType.name} and ${node.nodeType}
Browse files Browse the repository at this point in the history
Solves partially #57

Also migrates `q(node).property('_nodeType.name')` as we actually used the syntax once as well:
neos/neos-development-collection#3641
  • Loading branch information
mhsdesign committed Jun 27, 2024
1 parent efcb725 commit 5f1846c
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 1 deletion.
5 changes: 4 additions & 1 deletion config/set/contentrepository-90.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Neos\Rector\ContentRepository90\Rules\FusionNodeDepthRector;
use Neos\Rector\ContentRepository90\Rules\FusionNodeHiddenInIndexRector;
use Neos\Rector\ContentRepository90\Rules\FusionNodeIdentifierRector;
use Neos\Rector\ContentRepository90\Rules\FusionNodeNodeTypeRector;
use Neos\Rector\ContentRepository90\Rules\FusionNodeParentRector;
use Neos\Rector\ContentRepository90\Rules\FusionNodePathRector;
use Neos\Rector\ContentRepository90\Rules\FusionNodeTypeNameRector;
Expand Down Expand Up @@ -152,7 +153,9 @@
// setNodeType
// getNodeType: NodeType
$methodCallToPropertyFetches[] = new MethodCallToPropertyFetch(NodeLegacyStub::class, 'getNodeType', 'nodeType');
$fusionFlowQueryPropertyToComments[] = new FusionFlowQueryNodePropertyToWarningComment('_nodeType', 'Line %LINE: !! You very likely need to rewrite "q(VARIABLE).property("_nodeType")" to "VARIABLE.nodeType". We did not auto-apply this migration because we cannot be sure whether the variable is a Node.');
// Fusion: node.nodeType -> Neos.Node.getNodeType(node)
// Fusion: node.nodeType.name -> q(node).nodeTypeName()
$rectorConfig->rule(FusionNodeNodeTypeRector::class);
// setHidden
// isHidden
$rectorConfig->rule(NodeIsHiddenRector::class);
Expand Down
48 changes: 48 additions & 0 deletions src/ContentRepository90/Rules/FusionNodeNodeTypeRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace Neos\Rector\ContentRepository90\Rules;

use Neos\Rector\Core\FusionProcessing\EelExpressionTransformer;
use Neos\Rector\Core\FusionProcessing\FusionRectorInterface;
use Neos\Rector\Utility\CodeSampleLoader;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

class FusionNodeNodeTypeRector implements FusionRectorInterface
{
public function getRuleDefinition(): RuleDefinition
{
return CodeSampleLoader::fromFile('Fusion: Rewrite "node.nodeType" and "q(node).property(\'_nodeType\')" to "Neos.Node.getNodeType(node)"', __CLASS__);
}

public function refactorFileContent(string $fileContent): string
{
return EelExpressionTransformer::parse($fileContent)
->process(fn(string $eelExpression) => preg_replace(
'/(node|documentNode|site)\.nodeType\.name/',
'q($1).nodeTypeName()',
$eelExpression
))
->process(fn(string $eelExpression) => preg_replace(
'/(node|documentNode|site)\.nodeType/',
'Neos.Node.getNodeType($1)',
$eelExpression
))
->addCommentsIfRegexMatches(
'/\.nodeType\b(?!\()/',
'// TODO 9.0 migration: Line %LINE: You very likely need to rewrite "VARIABLE.nodeType" to "Neos.Node.getNodeType(VARIABLE)". We did not auto-apply this migration because we cannot be sure whether the variable is a Node.'
)
->process(fn(string $eelExpression) => preg_replace(
'/\.property\\((\'|")_nodeType.name(\'|")\\)/',
'.nodeTypeName()',
$eelExpression
))
->process(fn(string $eelExpression) => preg_replace(
'/q\(([^)]+)\)\.property\\([\'"]_nodeType(\.[^\'"]*)?[\'"]\\)/',
'Neos.Node.getNodeType($1)$2',
$eelExpression
))
->getProcessedContent();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
prototype(Neos.Rector:Test) < prototype(Neos.Fusion:Value) {
node = ${q(node).property('_nodeType') || q(documentNode).property("_nodeType") || q(site).property("_nodeType")}
otherVariable = ${q(someOtherVariable).property('_nodeType')}
nested = ${q(someOtherVariable).property('_nodeType.properties')}
deepNested = ${q(someOtherVariable).property('_nodeType.options.myOption')}
inAfx = afx`<Neos.Fusion:Value value={q(node).property('_nodeType')}/>`
}
-----
prototype(Neos.Rector:Test) < prototype(Neos.Fusion:Value) {
node = ${Neos.Node.getNodeType(node) || Neos.Node.getNodeType(documentNode) || Neos.Node.getNodeType(site)}
otherVariable = ${Neos.Node.getNodeType(someOtherVariable)}
nested = ${Neos.Node.getNodeType(someOtherVariable).properties}
deepNested = ${Neos.Node.getNodeType(someOtherVariable).options.myOption}
inAfx = afx`<Neos.Fusion:Value value={Neos.Node.getNodeType(node)}/>`
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
prototype(Neos.Rector:Test) < prototype(Neos.Fusion:Value) {
node = ${node.nodeType || documentNode.nodeType || site.nodeType}
otherVariable = ${someOtherVariable.nodeType}
inAfx = afx`<Neos.Fusion:Value value={node.nodeType}/>`
}
-----
// TODO 9.0 migration: Line 4: You very likely need to rewrite "VARIABLE.nodeType" to "Neos.Node.getNodeType(VARIABLE)". We did not auto-apply this migration because we cannot be sure whether the variable is a Node.
prototype(Neos.Rector:Test) < prototype(Neos.Fusion:Value) {
node = ${Neos.Node.getNodeType(node) || Neos.Node.getNodeType(documentNode) || Neos.Node.getNodeType(site)}
otherVariable = ${someOtherVariable.nodeType}
inAfx = afx`<Neos.Fusion:Value value={Neos.Node.getNodeType(node)}/>`
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
prototype(Neos.Rector:Test) < prototype(Neos.Fusion:Value) {
node = ${q(node).property('_nodeType.name') || q(documentNode).property("_nodeType.name") || q(site).property("_nodeType.name")}
otherVariable = ${q(someOtherVariable).property('_nodeType.name')}
inAfx = afx`<Neos.Fusion:Value value={q(node).property('_nodeType.name')}/>`
}
-----
prototype(Neos.Rector:Test) < prototype(Neos.Fusion:Value) {
node = ${q(node).nodeTypeName() || q(documentNode).nodeTypeName() || q(site).nodeTypeName()}
otherVariable = ${q(someOtherVariable).nodeTypeName()}
inAfx = afx`<Neos.Fusion:Value value={q(node).nodeTypeName()}/>`
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
prototype(Neos.Rector:Test) < prototype(Neos.Fusion:Value) {
node = ${node.nodeType.name || documentNode.nodeType.name || site.nodeType.name}
otherVariable = ${someOtherVariable.nodeType.name}
inAfx = afx`<Neos.Fusion:Value value={node.nodeType.name}/>`
}
-----
// TODO 9.0 migration: Line 4: You very likely need to rewrite "VARIABLE.nodeType" to "Neos.Node.getNodeType(VARIABLE)". We did not auto-apply this migration because we cannot be sure whether the variable is a Node.
prototype(Neos.Rector:Test) < prototype(Neos.Fusion:Value) {
node = ${q(node).nodeTypeName() || q(documentNode).nodeTypeName() || q(site).nodeTypeName()}
otherVariable = ${someOtherVariable.nodeType.name}
inAfx = afx`<Neos.Fusion:Value value={q(node).nodeTypeName()}/>`
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Neos\Rector\Tests\ContentRepository90\Rules\FusionNodeNodeTypeRector;

use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class FusionNodeNodeTypeRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(string $fileInfo): void
{
$this->doTestFile($fileInfo);
}

/**
* @return \Iterator<string>
*/
public function provideData(): \Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture', '*.fusion.inc');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare (strict_types=1);

use Neos\Rector\ContentRepository90\Rules\FusionNodeNodeTypeRector;
use Neos\Rector\Core\FusionProcessing\FusionFileProcessor;
use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig) : void {
$services = $rectorConfig->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(FusionNodeNodeTypeRector::class);
};

0 comments on commit 5f1846c

Please sign in to comment.