Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TASK: Migrate ${node.nodeType.name} and ${node.nodeType} #73

Merged
merged 2 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions config/set/contentrepository-90.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Neos\Rector\ContentRepository90\Legacy\LegacyContextStub;
use Neos\Rector\ContentRepository90\Legacy\NodeLegacyStub;
use Neos\Rector\ContentRepository90\Rules\ContentDimensionCombinatorGetAllAllowedCombinationsRector;
use Neos\Rector\ContentRepository90\Rules\ContentRepositoryUtilityRenderValidNodeNameRector;
use Neos\Rector\ContentRepository90\Rules\ContextFactoryToLegacyContextStubRector;
use Neos\Rector\ContentRepository90\Rules\ContextGetCurrentRenderingModeRector;
use Neos\Rector\ContentRepository90\Rules\ContextGetFirstLevelNodeCacheRector;
Expand All @@ -23,9 +24,12 @@
use Neos\Rector\ContentRepository90\Rules\FusionNodeAutoCreatedRector;
use Neos\Rector\ContentRepository90\Rules\FusionNodeContextPathRector;
use Neos\Rector\ContentRepository90\Rules\FusionNodeDepthRector;
use Neos\Rector\ContentRepository90\Rules\FusionNodeHiddenAfterDateTimeRector;
use Neos\Rector\ContentRepository90\Rules\FusionNodeHiddenBeforeDateTimeRector;
use Neos\Rector\ContentRepository90\Rules\FusionNodeHiddenInIndexRector;
use Neos\Rector\ContentRepository90\Rules\FusionNodeIdentifierRector;
use Neos\Rector\ContentRepository90\Rules\FusionNodeLabelRector;
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 All @@ -50,30 +54,25 @@
use Neos\Rector\ContentRepository90\Rules\YamlDimensionConfigRector;
use Neos\Rector\Generic\Rules\FusionNodePropertyPathToWarningCommentRector;
use Neos\Rector\Generic\Rules\FusionPrototypeNameAddCommentRector;
use Neos\Rector\Generic\Rules\FusionReplacePrototypeNameRector;
use Neos\Rector\Generic\Rules\InjectServiceIfNeededRector;
use Neos\Rector\Generic\Rules\MethodCallToWarningCommentRector;
use Neos\Rector\Generic\Rules\RemoveInjectionsRector;
use Neos\Rector\Generic\Rules\ToStringToMethodCallOrPropertyFetchRector;
use Neos\Rector\Generic\ValueObject\AddInjection;
use Neos\Rector\Generic\ValueObject\FusionFlowQueryNodePropertyToWarningComment;
use Neos\Rector\Generic\ValueObject\FusionNodePropertyPathToWarningComment;
use Neos\Rector\Generic\ValueObject\FusionPrototypeNameAddComment;
use Neos\Rector\Generic\ValueObject\FusionPrototypeNameReplacement;
use Neos\Rector\Generic\ValueObject\MethodCallToWarningComment;
use Neos\Rector\Generic\ValueObject\RemoveInjection;
use Neos\Rector\Generic\ValueObject\RemoveParentClass;
use Rector\Config\RectorConfig;
use Rector\Renaming\Rector\MethodCall\RenameMethodRector;
use Rector\Renaming\Rector\Name\RenameClassRector;
use Rector\Renaming\Rector\StaticCall\RenameStaticMethodRector;
use Rector\Renaming\ValueObject\MethodCallRename;
use Rector\Renaming\ValueObject\RenameStaticMethod;
use Rector\Transform\Rector\MethodCall\MethodCallToPropertyFetchRector;
use Rector\Transform\ValueObject\MethodCallToPropertyFetch;
use Neos\Rector\ContentRepository90\Rules\FusionNodeHiddenAfterDateTimeRector;
use Neos\Rector\ContentRepository90\Rules\FusionNodeHiddenBeforeDateTimeRector;
use Neos\Rector\Generic\Rules\FusionReplacePrototypeNameRector;
use Neos\Rector\Generic\ValueObject\FusionPrototypeNameReplacement;
use Neos\Rector\Generic\ValueObject\FusionPrototypeNameAddComment;
use Neos\Rector\ContentRepository90\Rules\ContentRepositoryUtilityRenderValidNodeNameRector;

return static function (RectorConfig $rectorConfig): void {
// Register FusionFileProcessor. All Fusion Rectors will be auto-registered at this processor.
Expand Down Expand Up @@ -153,7 +152,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);
};
Loading