From 3cf9eab82387b1eac40017b941c231d3ad3a4eb9 Mon Sep 17 00:00:00 2001 From: Denny Lubitz Date: Thu, 19 Dec 2024 17:17:39 +0100 Subject: [PATCH] FEATURE: Add todo warnings for all NodeTemplate::* methods --- config/set/contentrepository-90.php | 24 ++++++ ...ectInstantiationToWarningCommentRector.php | 83 +++++++++++++++++++ .../ObjectInstantiationToWarningComment.php | 13 +++ .../Fixture/new-object-fqcn.php.inc | 29 +++++++ .../Fixture/new-object-nested.php.inc | 31 +++++++ .../Fixture/new-object.php.inc | 31 +++++++ ...nstantiationToWarningCommentRectorTest.php | 31 +++++++ .../config/configured_rule.php | 13 +++ .../Fixture/node-template.php.inc | 45 ++++++++++ 9 files changed, 300 insertions(+) create mode 100644 src/Generic/Rules/ObjectInstantiationToWarningCommentRector.php create mode 100644 src/Generic/ValueObject/ObjectInstantiationToWarningComment.php create mode 100644 tests/Generic/Rules/ObjectInstantiationToWarningCommentRector/Fixture/new-object-fqcn.php.inc create mode 100644 tests/Generic/Rules/ObjectInstantiationToWarningCommentRector/Fixture/new-object-nested.php.inc create mode 100644 tests/Generic/Rules/ObjectInstantiationToWarningCommentRector/Fixture/new-object.php.inc create mode 100644 tests/Generic/Rules/ObjectInstantiationToWarningCommentRector/ObjectInstantiationToWarningCommentRectorTest.php create mode 100644 tests/Generic/Rules/ObjectInstantiationToWarningCommentRector/config/configured_rule.php create mode 100644 tests/Sets/ContentRepository90/Fixture/node-template.php.inc diff --git a/config/set/contentrepository-90.php b/config/set/contentrepository-90.php index 798c236..1d639ae 100644 --- a/config/set/contentrepository-90.php +++ b/config/set/contentrepository-90.php @@ -70,6 +70,7 @@ use Neos\Rector\Generic\Rules\FusionReplacePrototypeNameRector; use Neos\Rector\Generic\Rules\InjectServiceIfNeededRector; use Neos\Rector\Generic\Rules\MethodCallToWarningCommentRector; +use Neos\Rector\Generic\Rules\ObjectInstantiationToWarningCommentRector; use Neos\Rector\Generic\Rules\RemoveInjectionsRector; use Neos\Rector\Generic\Rules\SignalSlotToWarningCommentRector; use Neos\Rector\Generic\Rules\ToStringToMethodCallOrPropertyFetchRector; @@ -79,6 +80,7 @@ use Neos\Rector\Generic\ValueObject\FusionPrototypeNameAddComment; use Neos\Rector\Generic\ValueObject\FusionPrototypeNameReplacement; use Neos\Rector\Generic\ValueObject\MethodCallToWarningComment; +use Neos\Rector\Generic\ValueObject\ObjectInstantiationToWarningComment; use Neos\Rector\Generic\ValueObject\RemoveInjection; use Neos\Rector\Generic\ValueObject\RemoveParentClass; use Neos\Rector\Generic\ValueObject\SignalSlotToWarningComment; @@ -435,6 +437,28 @@ */ $rectorConfig->rule(WorkspaceGetNameRector::class); + /** + * NodeTemplate + */ + $nodeTemplateWarningMessage = '!! NodeTemplate::%2$s is removed in Neos 9.0. Use the "CreateNodeAggregateWithNode" command to create new nodes or "CreateNodeVariant" command to create variants of an existing node in other dimensions.'; + // getIdentifier(): string + $methodCallToWarningComments[] = new MethodCallToWarningComment(\Neos\ContentRepository\Domain\Model\NodeTemplate::class, 'getIdentifier', $nodeTemplateWarningMessage); + // getName(): string + $methodCallToWarningComments[] = new MethodCallToWarningComment(\Neos\ContentRepository\Domain\Model\NodeTemplate::class, 'getName', $nodeTemplateWarningMessage); + // getWorkspace(): void + $methodCallToWarningComments[] = new MethodCallToWarningComment(\Neos\ContentRepository\Domain\Model\NodeTemplate::class, 'getWorkspace', $nodeTemplateWarningMessage); + // setIdentifier(identifier: string): void + $methodCallToWarningComments[] = new MethodCallToWarningComment(\Neos\ContentRepository\Domain\Model\NodeTemplate::class, 'setIdentifier', $nodeTemplateWarningMessage); + // setName(newName: string): void + $methodCallToWarningComments[] = new MethodCallToWarningComment(\Neos\ContentRepository\Domain\Model\NodeTemplate::class, 'setName', $nodeTemplateWarningMessage); + + /** + * ObjectInstantiationToWarningComment + */ + $rectorConfig->ruleWithConfiguration(ObjectInstantiationToWarningCommentRector::class, [ + new ObjectInstantiationToWarningComment(\Neos\ContentRepository\Domain\Model\NodeTemplate::class, '!! NodeTemplate is removed in Neos 9.0. Use the "CreateNodeAggregateWithNode" command to create new nodes or "CreateNodeVariant" command to create variants of an existing node in other dimensions.')] + ); + /** * Signals and Slots * https://docs.neos.io/api/upgrade-instructions/9/signals-and-slots diff --git a/src/Generic/Rules/ObjectInstantiationToWarningCommentRector.php b/src/Generic/Rules/ObjectInstantiationToWarningCommentRector.php new file mode 100644 index 0000000..32c7f61 --- /dev/null +++ b/src/Generic/Rules/ObjectInstantiationToWarningCommentRector.php @@ -0,0 +1,83 @@ +> + */ + public function getNodeTypes(): array + { + return [Name::class]; + } + + /** + * @param \PhpParser\Node\Name $node + */ + public function refactor(Node $node): ?Node + { + assert($node instanceof Name); + + if ($node->getAttribute(AttributeKey::PARENT_NODE) instanceof Node\Stmt\UseUse) { + return null; + } + + foreach ($this->objectInstantiationToWarningComments as $objectInstantiationToWarningComment) { + if (!$this->isObjectType($node, new ObjectType($objectInstantiationToWarningComment->className))) { + continue; + } + + $this->nodesToAddCollector->addNodesBeforeNode( + [ + self::todoComment(sprintf($objectInstantiationToWarningComment->warningMessage, $objectInstantiationToWarningComment->className)) + ], + $node + ); + } + return $node; + } + + + /** + * @param mixed[] $configuration + */ + public function configure(array $configuration): void + { + Assert::allIsAOf($configuration, ObjectInstantiationToWarningComment::class); + $this->objectInstantiationToWarningComments = $configuration; + } +} diff --git a/src/Generic/ValueObject/ObjectInstantiationToWarningComment.php b/src/Generic/ValueObject/ObjectInstantiationToWarningComment.php new file mode 100644 index 0000000..579e814 --- /dev/null +++ b/src/Generic/ValueObject/ObjectInstantiationToWarningComment.php @@ -0,0 +1,13 @@ + +----- + diff --git a/tests/Generic/Rules/ObjectInstantiationToWarningCommentRector/Fixture/new-object-nested.php.inc b/tests/Generic/Rules/ObjectInstantiationToWarningCommentRector/Fixture/new-object-nested.php.inc new file mode 100644 index 0000000..7e73f1e --- /dev/null +++ b/tests/Generic/Rules/ObjectInstantiationToWarningCommentRector/Fixture/new-object-nested.php.inc @@ -0,0 +1,31 @@ +getAutor(); + } +} + +?> +----- +getAutor(); + } +} + +?> diff --git a/tests/Generic/Rules/ObjectInstantiationToWarningCommentRector/Fixture/new-object.php.inc b/tests/Generic/Rules/ObjectInstantiationToWarningCommentRector/Fixture/new-object.php.inc new file mode 100644 index 0000000..7e4a20b --- /dev/null +++ b/tests/Generic/Rules/ObjectInstantiationToWarningCommentRector/Fixture/new-object.php.inc @@ -0,0 +1,31 @@ + +----- + diff --git a/tests/Generic/Rules/ObjectInstantiationToWarningCommentRector/ObjectInstantiationToWarningCommentRectorTest.php b/tests/Generic/Rules/ObjectInstantiationToWarningCommentRector/ObjectInstantiationToWarningCommentRectorTest.php new file mode 100644 index 0000000..a9d0a81 --- /dev/null +++ b/tests/Generic/Rules/ObjectInstantiationToWarningCommentRector/ObjectInstantiationToWarningCommentRectorTest.php @@ -0,0 +1,31 @@ +doTestFile($fileInfo); + } + + /** + * @return \Iterator + */ + public function provideData(): \Iterator + { + return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/configured_rule.php'; + } +} diff --git a/tests/Generic/Rules/ObjectInstantiationToWarningCommentRector/config/configured_rule.php b/tests/Generic/Rules/ObjectInstantiationToWarningCommentRector/config/configured_rule.php new file mode 100644 index 0000000..8fe4b6e --- /dev/null +++ b/tests/Generic/Rules/ObjectInstantiationToWarningCommentRector/config/configured_rule.php @@ -0,0 +1,13 @@ +ruleWithConfiguration(ObjectInstantiationToWarningCommentRector::class, [ + new ObjectInstantiationToWarningComment(\My\Class\To\Comment::class, 'This is a comment'), + ]); +}; diff --git a/tests/Sets/ContentRepository90/Fixture/node-template.php.inc b/tests/Sets/ContentRepository90/Fixture/node-template.php.inc new file mode 100644 index 0000000..19e5588 --- /dev/null +++ b/tests/Sets/ContentRepository90/Fixture/node-template.php.inc @@ -0,0 +1,45 @@ +getIdentifier(); + $nodeTemplate->getName(); + $nodeTemplate->getWorkspace(); + $nodeTemplate->setIdentifier(); + $nodeTemplate->setName(); + } +} +----- +getIdentifier(); + // TODO 9.0 migration: !! NodeTemplate::getName is removed in Neos 9.0. Use the "CreateNodeAggregateWithNode" command to create new nodes or "CreateNodeVariant" command to create variants of an existing node in other dimensions. + + $nodeTemplate->getName(); + // TODO 9.0 migration: !! NodeTemplate::getWorkspace is removed in Neos 9.0. Use the "CreateNodeAggregateWithNode" command to create new nodes or "CreateNodeVariant" command to create variants of an existing node in other dimensions. + + $nodeTemplate->getWorkspace(); + // TODO 9.0 migration: !! NodeTemplate::setIdentifier is removed in Neos 9.0. Use the "CreateNodeAggregateWithNode" command to create new nodes or "CreateNodeVariant" command to create variants of an existing node in other dimensions. + + $nodeTemplate->setIdentifier(); + // TODO 9.0 migration: !! NodeTemplate::setName is removed in Neos 9.0. Use the "CreateNodeAggregateWithNode" command to create new nodes or "CreateNodeVariant" command to create variants of an existing node in other dimensions. + + $nodeTemplate->setName(); + } +}