From 41132c5fe6b9ec495c0abbe4957c78605c866a45 Mon Sep 17 00:00:00 2001 From: Denny Lubitz Date: Thu, 19 Dec 2024 17:44:11 +0100 Subject: [PATCH] FEATURE: Rewrite code also for NodeSearchServiceInterface --- .../Rules/NodeSearchServiceRector.php | 5 +- .../node-search-service-interface.php.inc | 64 +++++++++++++++++++ ...ss.php.inc => node-search-service.php.inc} | 0 .../Fixture/node-search-service.php.inc | 30 +++++++-- 4 files changed, 93 insertions(+), 6 deletions(-) create mode 100644 tests/ContentRepository90/Rules/NodeSearchServiceRector/Fixture/node-search-service-interface.php.inc rename tests/ContentRepository90/Rules/NodeSearchServiceRector/Fixture/{some_class.php.inc => node-search-service.php.inc} (100%) diff --git a/src/ContentRepository90/Rules/NodeSearchServiceRector.php b/src/ContentRepository90/Rules/NodeSearchServiceRector.php index 5f08ce9..ae00cbe 100644 --- a/src/ContentRepository90/Rules/NodeSearchServiceRector.php +++ b/src/ContentRepository90/Rules/NodeSearchServiceRector.php @@ -40,7 +40,10 @@ public function refactor(Node $node): ?Node { assert($node instanceof Node\Expr\MethodCall); - if (!$this->isObjectType($node->var, new ObjectType(\Neos\Neos\Domain\Service\NodeSearchService::class))) { + if ( + !$this->isObjectType($node->var, new ObjectType(\Neos\Neos\Domain\Service\NodeSearchService::class)) + && !$this->isObjectType($node->var, new ObjectType(\Neos\Neos\Domain\Service\NodeSearchServiceInterface::class)) + ) { return null; } if (!$this->isName($node->name, 'findByProperties')) { diff --git a/tests/ContentRepository90/Rules/NodeSearchServiceRector/Fixture/node-search-service-interface.php.inc b/tests/ContentRepository90/Rules/NodeSearchServiceRector/Fixture/node-search-service-interface.php.inc new file mode 100644 index 0000000..b3fcb2f --- /dev/null +++ b/tests/ContentRepository90/Rules/NodeSearchServiceRector/Fixture/node-search-service-interface.php.inc @@ -0,0 +1,64 @@ +nodeSearchServiceInterface->findByProperties($term, $searchNodeTypes, $context, $node); + } + + public function startingPointNodeIsNotGiven(Context $context) + { + $term = "term"; + $searchNodeTypes = []; + $nodes = $this->nodeSearchServiceInterface->findByProperties($term, $searchNodeTypes, $context); + } +} + +----- +contentRepositoryRegistry->subgraphForNode($node); + $nodes = $subgraph->findDescendantNodes($node->aggregateId, \Neos\ContentRepository\Core\Projection\ContentGraph\Filter\FindDescendantNodesFilter::create(nodeTypes: \Neos\ContentRepository\Core\Projection\ContentGraph\Filter\NodeType\NodeTypeCriteria::create(\Neos\ContentRepository\Core\NodeType\NodeTypeNames::fromStringArray($searchNodeTypes), \Neos\ContentRepository\Core\NodeType\NodeTypeNames::createEmpty()), searchTerm: $term)); + } + + public function startingPointNodeIsNotGiven(Context $context) + { + $term = "term"; + $searchNodeTypes = []; + // TODO 9.0 migration: The replacement needs a node as starting point for the search. Please provide a node, to make this replacement working. + $node = 'we-need-a-node-here'; + $subgraph = $this->contentRepositoryRegistry->subgraphForNode($node); + $nodes = $subgraph->findDescendantNodes($node->aggregateId, \Neos\ContentRepository\Core\Projection\ContentGraph\Filter\FindDescendantNodesFilter::create(nodeTypes: \Neos\ContentRepository\Core\Projection\ContentGraph\Filter\NodeType\NodeTypeCriteria::create(\Neos\ContentRepository\Core\NodeType\NodeTypeNames::fromStringArray($searchNodeTypes), \Neos\ContentRepository\Core\NodeType\NodeTypeNames::createEmpty()), searchTerm: $term)); + } +} + diff --git a/tests/ContentRepository90/Rules/NodeSearchServiceRector/Fixture/some_class.php.inc b/tests/ContentRepository90/Rules/NodeSearchServiceRector/Fixture/node-search-service.php.inc similarity index 100% rename from tests/ContentRepository90/Rules/NodeSearchServiceRector/Fixture/some_class.php.inc rename to tests/ContentRepository90/Rules/NodeSearchServiceRector/Fixture/node-search-service.php.inc diff --git a/tests/Sets/ContentRepository90/Fixture/node-search-service.php.inc b/tests/Sets/ContentRepository90/Fixture/node-search-service.php.inc index e995dd5..8b21742 100644 --- a/tests/Sets/ContentRepository90/Fixture/node-search-service.php.inc +++ b/tests/Sets/ContentRepository90/Fixture/node-search-service.php.inc @@ -2,16 +2,24 @@ namespace Neos\Rector\Test; +use Neos\Flow\Annotations as Flow; use Neos\ContentRepository\Domain\Model\Node; use Neos\ContentRepository\Domain\Service\Context; class SomeClass extends AnotherClass { /** + * @Flow\Inject * @var \Neos\Neos\Domain\Service\NodeSearchService */ private $nodeSearchService; + /** + * @Flow\Inject + * @var \Neos\Neos\Domain\Service\NodeSearchServiceInterface + */ + private $nodeSearchServiceInterface; + public function startingPointNodeIsGiven(Node $node, Context $context) { $term = "term"; @@ -25,6 +33,13 @@ class SomeClass extends AnotherClass $searchNodeTypes = []; $nodes = $this->nodeSearchService->findByProperties($term, $searchNodeTypes, $context); } + + public function startingPointNodeIsGivenUsingInterface(Node $node, Context $context) + { + $term = "term"; + $searchNodeTypes = []; + $nodes = $this->nodeSearchServiceInterface->findByProperties($term, $searchNodeTypes, $context, $node); + } } ----- @@ -32,18 +47,14 @@ class SomeClass extends AnotherClass namespace Neos\Rector\Test; +use Neos\Flow\Annotations as Flow; use Neos\ContentRepository\Domain\Model\Node; use Neos\ContentRepository\Domain\Service\Context; class SomeClass extends AnotherClass { - /** - * @var \Neos\Neos\Domain\Service\NodeSearchService - */ - private $nodeSearchService; #[\Neos\Flow\Annotations\Inject] protected \Neos\ContentRepositoryRegistry\ContentRepositoryRegistry $contentRepositoryRegistry; - public function startingPointNodeIsGiven(\Neos\ContentRepository\Core\Projection\ContentGraph\Node $node, \Neos\Rector\ContentRepository90\Legacy\LegacyContextStub $context) { $term = "term"; @@ -62,5 +73,14 @@ class SomeClass extends AnotherClass $subgraph = $this->contentRepositoryRegistry->subgraphForNode($node); $nodes = $subgraph->findDescendantNodes($node->aggregateId, \Neos\ContentRepository\Core\Projection\ContentGraph\Filter\FindDescendantNodesFilter::create(nodeTypes: \Neos\ContentRepository\Core\Projection\ContentGraph\Filter\NodeType\NodeTypeCriteria::create(\Neos\ContentRepository\Core\NodeType\NodeTypeNames::fromStringArray($searchNodeTypes), \Neos\ContentRepository\Core\NodeType\NodeTypeNames::createEmpty()), searchTerm: $term)); } + + public function startingPointNodeIsGivenUsingInterface(\Neos\ContentRepository\Core\Projection\ContentGraph\Node $node, \Neos\Rector\ContentRepository90\Legacy\LegacyContextStub $context) + { + $term = "term"; + $searchNodeTypes = []; + // TODO 9.0 migration: This could be a suitable replacement. Please check if all your requirements are still fulfilled. + $subgraph = $this->contentRepositoryRegistry->subgraphForNode($node); + $nodes = $subgraph->findDescendantNodes($node->aggregateId, \Neos\ContentRepository\Core\Projection\ContentGraph\Filter\FindDescendantNodesFilter::create(nodeTypes: \Neos\ContentRepository\Core\Projection\ContentGraph\Filter\NodeType\NodeTypeCriteria::create(\Neos\ContentRepository\Core\NodeType\NodeTypeNames::fromStringArray($searchNodeTypes), \Neos\ContentRepository\Core\NodeType\NodeTypeNames::createEmpty()), searchTerm: $term)); + } }