-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from neos/task/nodetype
TASK: Refactor nodetype
- Loading branch information
Showing
20 changed files
with
720 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
src/ContentRepository90/Rules/NodeTypeAllowsGrandchildNodeTypeRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?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; | ||
use Neos\ContentRepository\Core\NodeType\NodeType; | ||
use PhpParser\NodeDumper; | ||
|
||
final class NodeTypeAllowsGrandchildNodeTypeRector extends AbstractRector | ||
{ | ||
use AllTraits; | ||
|
||
public function __construct( | ||
private readonly NodesToAddCollector $nodesToAddCollector | ||
) { | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return CodeSampleLoader::fromFile('"$nodeType->allowsGrandchildNodeType($parentNodeName, $nodeType)" 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(NodeType::class))) { | ||
return null; | ||
} | ||
|
||
if (!$this->isName($node->name, 'allowsGrandchildNodeType')) { | ||
return null; | ||
} | ||
|
||
$this->nodesToAddCollector->addNodesBeforeNode( | ||
[ | ||
self::withTodoComment( | ||
'Make this code aware of multiple Content Repositories.', | ||
self::assign('contentRepository', $this->this_contentRepositoryRegistry_get($this->contentRepositoryId_fromString('default'))), | ||
) | ||
], | ||
$node | ||
); | ||
|
||
return $this->nodeFactory->createMethodCall( | ||
$this->contentRepository_getNodeTypeManager(), | ||
'isNodeTypeAllowedAsChildToTetheredNode', | ||
[ | ||
$node->var, | ||
$this->nodeFactory->createStaticCall( | ||
\Neos\ContentRepository\Core\SharedModel\Node\NodeName::class, | ||
'fromString', | ||
[ | ||
$node->args[0] | ||
] | ||
), | ||
$node->args[1] | ||
] | ||
); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
src/ContentRepository90/Rules/NodeTypeGetAutoCreatedChildNodesRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?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; | ||
use Neos\ContentRepository\Core\NodeType\NodeType; | ||
|
||
final class NodeTypeGetAutoCreatedChildNodesRector extends AbstractRector | ||
{ | ||
use AllTraits; | ||
|
||
public function __construct( | ||
private readonly NodesToAddCollector $nodesToAddCollector | ||
) | ||
{ | ||
} | ||
|
||
public function getRuleDefinition() : RuleDefinition | ||
{ | ||
return CodeSampleLoader::fromFile('"$nodeType->getAutoCreatedChildNodes()" 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(NodeType::class))) { | ||
return null; | ||
} | ||
|
||
if (!$this->isName($node->name, 'getAutoCreatedChildNodes')) { | ||
return null; | ||
} | ||
|
||
$this->nodesToAddCollector->addNodesBeforeNode( | ||
[ | ||
self::withTodoComment( | ||
'Make this code aware of multiple Content Repositories.', | ||
self::assign('contentRepository', $this->this_contentRepositoryRegistry_get($this->contentRepositoryId_fromString('default'))), | ||
) | ||
], | ||
$node | ||
); | ||
|
||
return $this->nodeFactory->createMethodCall( | ||
$this->contentRepository_getNodeTypeManager(), | ||
'getTetheredNodesConfigurationForNodeType', | ||
[$node->var] | ||
); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
src/ContentRepository90/Rules/NodeTypeGetTypeOfAutoCreatedChildNodeRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?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; | ||
use Neos\ContentRepository\Core\NodeType\NodeType; | ||
use PhpParser\NodeDumper; | ||
|
||
final class NodeTypeGetTypeOfAutoCreatedChildNodeRector extends AbstractRector | ||
{ | ||
use AllTraits; | ||
|
||
public function __construct( | ||
private readonly NodesToAddCollector $nodesToAddCollector | ||
) | ||
{ | ||
} | ||
|
||
public function getRuleDefinition() : RuleDefinition | ||
{ | ||
return CodeSampleLoader::fromFile('"$nodeType->getTypeOfAutoCreatedChildNode($nodeName)" 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(NodeType::class))) { | ||
return null; | ||
} | ||
|
||
if (!$this->isName($node->name, 'getTypeOfAutoCreatedChildNode')) { | ||
return null; | ||
} | ||
|
||
$this->nodesToAddCollector->addNodesBeforeNode( | ||
[ | ||
self::withTodoComment( | ||
'Make this code aware of multiple Content Repositories.', | ||
self::assign('contentRepository', $this->this_contentRepositoryRegistry_get($this->contentRepositoryId_fromString('default'))), | ||
) | ||
], | ||
$node | ||
); | ||
|
||
return $this->nodeFactory->createMethodCall( | ||
$this->contentRepository_getNodeTypeManager(), | ||
'getTypeOfTetheredNode', | ||
[ | ||
$node->var, | ||
$node->args[0] | ||
] | ||
); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
tests/ContentRepository90/Rules/NodeTypeAllowsGrandchildNodeType/Fixture/some_class.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
use Neos\Rector\ContentRepository90\Legacy\NodeLegacyStub; | ||
use Neos\ContentRepository\Core\SharedModel\Node\NodeName; | ||
|
||
class SomeClass | ||
{ | ||
public function run(NodeLegacyStub $node) | ||
{ | ||
$parentNodeName = 'name'; | ||
$nodeType = $node->getNodeType(); | ||
$grandParentsNodeType = $node->getParent()->getParent()->getNodeType(); | ||
|
||
$grandParentsNodeType->allowsGrandchildNodeType($parentNodeName, $nodeType); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
use Neos\Rector\ContentRepository90\Legacy\NodeLegacyStub; | ||
use Neos\ContentRepository\Core\SharedModel\Node\NodeName; | ||
|
||
class SomeClass | ||
{ | ||
#[\Neos\Flow\Annotations\Inject] | ||
protected \Neos\ContentRepositoryRegistry\ContentRepositoryRegistry $contentRepositoryRegistry; | ||
public function run(NodeLegacyStub $node) | ||
{ | ||
$parentNodeName = 'name'; | ||
$nodeType = $node->getNodeType(); | ||
$grandParentsNodeType = $node->getParent()->getParent()->getNodeType(); | ||
// TODO 9.0 migration: Make this code aware of multiple Content Repositories. | ||
$contentRepository = $this->contentRepositoryRegistry->get(\Neos\ContentRepository\Core\Factory\ContentRepositoryId::fromString('default')); | ||
|
||
$contentRepository->getNodeTypeManager()->isNodeTypeAllowedAsChildToTetheredNode($grandParentsNodeType, \Neos\ContentRepository\Core\SharedModel\Node\NodeName::fromString($parentNodeName), $nodeType); | ||
} | ||
} | ||
|
||
?> |
Oops, something went wrong.