Skip to content

Commit

Permalink
Merge pull request #89 from neos/task/node-label-generator
Browse files Browse the repository at this point in the history
TASK: Add migration for getLabel with NodeLabelGeneratorRector
  • Loading branch information
dlubitz authored Oct 18, 2024
2 parents 2722bae + 677df86 commit 8c8acdd
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 0 deletions.
4 changes: 4 additions & 0 deletions config/set/contentrepository-90.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -137,6 +139,7 @@
$fusionFlowQueryPropertyToComments[] = new FusionFlowQueryNodePropertyToWarningComment('_name', 'Line %LINE: !! You very likely need to rewrite "q(VARIABLE).property("_name")" to "VARIABLE.nodeName". 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)
Expand Down Expand Up @@ -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);
};
60 changes: 60 additions & 0 deletions src/ContentRepository90/Rules/NodeLabelGeneratorRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare (strict_types=1);
namespace Neos\Rector\ContentRepository90\Rules;

use Neos\Rector\ContentRepository90\Legacy\NodeLegacyStub;
use Neos\Rector\Utility\CodeSampleLoader;
use PhpParser\Node;
use PHPStan\Type\ObjectType;
use Rector\Core\Rector\AbstractRector;
use Rector\PostRector\Collector\NodesToAddCollector;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

final class NodeLabelGeneratorRector extends AbstractRector
{
use AllTraits;

public function __construct(
private readonly NodesToAddCollector $nodesToAddCollector
)
{
}

public function getRuleDefinition() : RuleDefinition
{
return CodeSampleLoader::fromFile('"$node->getLabel()" will be rewritten.', __CLASS__);
}

/**
* @return array<class-string<Node>>
*/
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]
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

use Neos\Rector\ContentRepository90\Legacy\NodeLegacyStub;

class SomeClass
{
public function run(NodeLegacyStub $node)
{
$label = $node->getLabel();
}
}

?>
-----
<?php

use Neos\Rector\ContentRepository90\Legacy\NodeLegacyStub;

class SomeClass
{
#[\Neos\Flow\Annotations\Inject]
protected \Neos\Neos\Domain\NodeLabel\NodeLabelGeneratorInterface $nodeLabelGenerator;
public function run(NodeLegacyStub $node)
{
$label = $this->nodeLabelGenerator->getLabel($node);
}
}

?>
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\NodeLabelGeneratorRector;

use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class NodeLabelGeneratorRectorTest 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');
}

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

declare (strict_types=1);

use Neos\Neos\Domain\NodeLabel\NodeLabelGeneratorInterface;
use Neos\Rector\ContentRepository90\Rules\NodeLabelGeneratorRector;
use Neos\Rector\Generic\Rules\InjectServiceIfNeededRector;
use Neos\Rector\Generic\ValueObject\AddInjection;
use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(NodeLabelGeneratorRector::class);

$rectorConfig->ruleWithConfiguration(InjectServiceIfNeededRector::class, [
new AddInjection('nodeLabelGenerator', NodeLabelGeneratorInterface::class),
]);
};

0 comments on commit 8c8acdd

Please sign in to comment.