Skip to content

Commit

Permalink
Merge pull request #99 from neos/feature/node-search-service
Browse files Browse the repository at this point in the history
FEATURE: Add migration for NodeSearchService
  • Loading branch information
mhsdesign authored Nov 28, 2024
2 parents 549f6f8 + e566989 commit 61c26bc
Show file tree
Hide file tree
Showing 7 changed files with 431 additions and 0 deletions.
6 changes: 6 additions & 0 deletions config/set/contentrepository-90.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
use Neos\Rector\ContentRepository90\Rules\NodeIsHiddenInIndexRector;
use Neos\Rector\ContentRepository90\Rules\NodeIsHiddenRector;
use Neos\Rector\ContentRepository90\Rules\NodeLabelGeneratorRector;
use Neos\Rector\ContentRepository90\Rules\NodeSearchServiceRector;
use Neos\Rector\ContentRepository90\Rules\NodeTypeAllowsGrandchildNodeTypeRector;
use Neos\Rector\ContentRepository90\Rules\NodeTypeGetAutoCreatedChildNodesRector;
use Neos\Rector\ContentRepository90\Rules\NodeTypeGetNameRector;
Expand Down Expand Up @@ -403,6 +404,11 @@
// ContentDimensionCombinator::getAllAllowedCombinations
$rectorConfig->rule(ContentDimensionCombinatorGetAllAllowedCombinationsRector::class);

/**
* Neos\Neos\Domain\Service\NodeSearchService
*/
$rectorConfig->rule(NodeSearchServiceRector::class);


/**
* Neos\ContentRepository\Domain\Factory\NodeFactory
Expand Down
131 changes: 131 additions & 0 deletions docs/rules_overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -1794,6 +1794,52 @@ return static function (RectorConfig $rectorConfig): void {

<br>

## NodeSearchServiceRector

`"NodeSearchService::findDescendantNodes()"` will be rewritten

- class: [`Neos\Rector\ContentRepository90\Rules\NodeSearchServiceRector`](../src/ContentRepository90/Rules/NodeSearchServiceRector.php)

```diff
<?php

namespace Neos\Rector\Test;

use Neos\ContentRepository\Domain\Model\Node;
use Neos\ContentRepository\Domain\Service\Context;

class SomeClass extends AnotherClass
{
/**
* @var \Neos\Neos\Domain\Service\NodeSearchService
*/
private $nodeSearchService;

public function startingPointNodeIsGiven(Node $node, Context $context)
{
$term = "term";
$searchNodeTypes = [];
- $nodes = $this->nodeSearchService->findByProperties($term, $searchNodeTypes, $context, $node);
+ // 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));
}

public function startingPointNodeIsNotGiven(Context $context)
{
$term = "term";
$searchNodeTypes = [];
- $nodes = $this->nodeSearchService->findByProperties($term, $searchNodeTypes, $context);
+ // 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));
}
}
```

<br>

## NodeTypeAllowsGrandchildNodeTypeRector

"$nodeType->allowsGrandchildNodeType($parentNodeName, `$nodeType)"` will be rewritten.
Expand Down Expand Up @@ -2078,6 +2124,91 @@ return static function (RectorConfig $rectorConfig): void {

<br>

## SignalSlotToWarningCommentRector

"Warning comments for various non-supported signals

:wrench: **configure it!**

- class: [`Neos\Rector\Generic\Rules\SignalSlotToWarningCommentRector`](../src/Generic/Rules/SignalSlotToWarningCommentRector.php)

```php
<?php

declare(strict_types=1);

use Neos\Rector\Generic\Rules\SignalSlotToWarningCommentRector;
use Neos\Rector\Generic\ValueObject\SignalSlotToWarningComment;
use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
$containerConfigurator->extension('rectorConfig', [
[
'class' => SignalSlotToWarningCommentRector::class,
'configuration' => [
new SignalSlotToWarningComment('PhpParser\Node', 'beforeMove', '!! This signal "beforeMove" on Node doesn\'t exist anymore'),
],
],
]);
};
```


```diff
<?php

use Neos\Flow\Core\Bootstrap;
use Neos\Flow\Package\Package as BasePackage;
use Neos\Flow\SignalSlot\Dispatcher;
use Neos\Rector\ContentRepository90\Legacy\NodeLegacyStub;

class Package extends BasePackage
{
public function boot(Bootstrap $bootstrap)
{
/** @var Dispatcher $dispatcher */
$dispatcher = $bootstrap->getSignalSlotDispatcher();
+ // TODO 9.0 migration: Signal "beforeMove" doesn't exist anymore

+
$dispatcher->connect(
NodeLegacyStub::class,
'beforeMove',
SomeOtherClass::class,
'someMethod'
);
+ // TODO 9.0 migration: Signal "afterMove" doesn't exist anymore
+

$dispatcher->connect(
'Neos\Rector\ContentRepository90\Legacy\NodeLegacyStub',
'afterMove',
SomeOtherClass::class,
'someMethod'
);

$dispatcher->connect(
NodeLegacyStub::class,
'otherMethod',
SomeOtherClass::class,
'someMethod'
);

$dispatcher->connect(
OtherClass::class,
'afterMove',
SomeOtherClass::class,
'someMethod'
);
}
}

?>
```

<br>

## ToStringToMethodCallOrPropertyFetchRector

Turns defined code uses of `"__toString()"` method to specific method calls or property fetches.
Expand Down
121 changes: 121 additions & 0 deletions src/ContentRepository90/Rules/NodeSearchServiceRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

declare (strict_types=1);

namespace Neos\Rector\ContentRepository90\Rules;

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 NodeSearchServiceRector extends AbstractRector
{
use AllTraits;

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

public function getRuleDefinition(): RuleDefinition
{
return CodeSampleLoader::fromFile('"NodeSearchService::findDescendantNodes()" will be rewritten', __CLASS__);
}

/**
* @return array<class-string<Node>>
*/
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\Neos\Domain\Service\NodeSearchService::class))) {
return null;
}
if (!$this->isName($node->name, 'findByProperties')) {
return null;
}

if (!isset($node->args[3])) {
$nodeExpr = self::assign('node', new \PhpParser\Node\Scalar\String_('we-need-a-node-here'));
$nodeNode = $nodeExpr->expr->var;

$this->nodesToAddCollector->addNodesBeforeNode(
[
self::withTodoComment('The replacement needs a node as starting point for the search. Please provide a node, to make this replacement working.', $nodeExpr),
$subgraphNode = self::assign('subgraph', $this->this_contentRepositoryRegistry_subgraphForNode($nodeNode)),
],
$node
);

} else {
$this->nodesToAddCollector->addNodesBeforeNode(
[
self::withTodoComment('This could be a suitable replacement. Please check if all your requirements are still fulfilled.',
$subgraphNode = self::assign('subgraph', $this->this_contentRepositoryRegistry_subgraphForNode($node->args[3]->value))
)
],
$node

);
$nodeNode = $node->args[3]->value;

}

return $this->nodeFactory->createMethodCall(
$subgraphNode->expr->var,
'findDescendantNodes',
[
$this->nodeFactory->createPropertyFetch(
$nodeNode,
'aggregateId'
),
$this->nodeFactory->createStaticCall(
\Neos\ContentRepository\Core\Projection\ContentGraph\Filter\FindDescendantNodesFilter::class,
'create',
[
'nodeTypes' => $this->nodeFactory->createStaticCall(
\Neos\ContentRepository\Core\Projection\ContentGraph\Filter\NodeType\NodeTypeCriteria::class,
'create',
[
$this->nodeFactory->createStaticCall(
\Neos\ContentRepository\Core\NodeType\NodeTypeNames::class,
'fromStringArray',
[
$node->args[1]->value,
]
),
$this->nodeFactory->createStaticCall(
\Neos\ContentRepository\Core\NodeType\NodeTypeNames::class,
'createEmpty',
),
]
),
'searchTerm' => $node->args[0]->value,
]
)
]
);
}
}

/**
* \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
* )
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Neos\Rector\Test;

use Neos\ContentRepository\Domain\Model\Node;
use Neos\ContentRepository\Domain\Service\Context;

class SomeClass extends AnotherClass
{
/**
* @var \Neos\Neos\Domain\Service\NodeSearchService
*/
private $nodeSearchService;

public function startingPointNodeIsGiven(Node $node, Context $context)
{
$term = "term";
$searchNodeTypes = [];
$nodes = $this->nodeSearchService->findByProperties($term, $searchNodeTypes, $context, $node);
}

public function startingPointNodeIsNotGiven(Context $context)
{
$term = "term";
$searchNodeTypes = [];
$nodes = $this->nodeSearchService->findByProperties($term, $searchNodeTypes, $context);
}
}

-----
<?php

namespace Neos\Rector\Test;

use Neos\ContentRepository\Domain\Model\Node;
use Neos\ContentRepository\Domain\Service\Context;

class SomeClass extends AnotherClass
{
/**
* @var \Neos\Neos\Domain\Service\NodeSearchService
*/
private $nodeSearchService;

public function startingPointNodeIsGiven(Node $node, Context $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));
}

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));
}
}

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\NodeGetChildNodesRector;

use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class NodeSearchServiceRectorTest 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,12 @@
<?php

declare (strict_types=1);

use Neos\Rector\ContentRepository90\Rules\NodeGetNodeTypeGetNameRector;
use Neos\Rector\ContentRepository90\Rules\NodeGetNodeTypeRector;
use Neos\Rector\ContentRepository90\Rules\NodeSearchServiceRector;
use Rector\Config\RectorConfig;

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

0 comments on commit 61c26bc

Please sign in to comment.