Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEATURE: Add todo warnings for all NodeTemplate::* methods #118

Merged
merged 2 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions config/set/contentrepository-90.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,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;
Expand All @@ -80,6 +81,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;
Expand Down Expand Up @@ -436,6 +438,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.')]
);

/**
* Neos\ContentRepository\Domain\Service\NodeTypeManager
*/
Expand Down
83 changes: 83 additions & 0 deletions src/Generic/Rules/ObjectInstantiationToWarningCommentRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

declare (strict_types=1);

namespace Neos\Rector\Generic\Rules;

use Neos\Rector\Generic\ValueObject\ObjectInstantiationToWarningComment;
use Neos\Rector\Utility\CodeSampleLoader;
use PhpParser\Node;
use PhpParser\Node\Name;
use PHPStan\Type\ObjectType;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PostRector\Collector\NodesToAddCollector;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;

final class ObjectInstantiationToWarningCommentRector extends AbstractRector implements ConfigurableRectorInterface
{
use AllTraits;

/**
* @var ObjectInstantiationToWarningComment[]
*/
private array $objectInstantiationToWarningComments = [];

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

public function getRuleDefinition(): RuleDefinition
{
return CodeSampleLoader::fromFile('"Warning comments for various non-supported signals', __CLASS__, [
new ObjectInstantiationToWarningComment(Node::class, '!! This signal "beforeMove" on Node doesn\'t exist anymore')
]);
}

/**
* @return array<class-string<Node>>
*/
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;
}
}
13 changes: 13 additions & 0 deletions src/Generic/ValueObject/ObjectInstantiationToWarningComment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
declare(strict_types=1);

namespace Neos\Rector\Generic\ValueObject;

class ObjectInstantiationToWarningComment
{
public function __construct(
public readonly string $className,
public readonly string $warningMessage,
) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

use Neos\Flow\Package\Package as BasePackage;

class Package extends BasePackage
{
public function boot()
{
$myComment = new \My\Class\To\Comment();
}
}

?>
-----
<?php

use Neos\Flow\Package\Package as BasePackage;

class Package extends BasePackage
{
public function boot()
{
// TODO 9.0 migration: This is a comment

$myComment = new \My\Class\To\Comment();
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use My\Class\To\Comment;
use Neos\Flow\Package\Package as BasePackage;

class Package extends BasePackage
{
public function boot()
{
$author = (new Comment(['author']))->getAutor();
}
}

?>
-----
<?php

use My\Class\To\Comment;
use Neos\Flow\Package\Package as BasePackage;

class Package extends BasePackage
{
public function boot()
{
// TODO 9.0 migration: This is a comment

$author = (new Comment(['author']))->getAutor();
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use My\Class\To\Comment;
use Neos\Flow\Package\Package as BasePackage;

class Package extends BasePackage
{
public function boot()
{
$myComment = new Comment();
}
}

?>
-----
<?php

use My\Class\To\Comment;
use Neos\Flow\Package\Package as BasePackage;

class Package extends BasePackage
{
public function boot()
{
// TODO 9.0 migration: This is a comment

$myComment = new Comment();
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Neos\Rector\Tests\Generic\Rules\ToStringToPropertyFetchRector;

use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class ObjectInstantiationToWarningCommentRectorTest 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,13 @@
<?php

declare (strict_types=1);

use Neos\Rector\Generic\Rules\ObjectInstantiationToWarningCommentRector;
use Neos\Rector\Generic\ValueObject\ObjectInstantiationToWarningComment;
use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->ruleWithConfiguration(ObjectInstantiationToWarningCommentRector::class, [
new ObjectInstantiationToWarningComment(\My\Class\To\Comment::class, 'This is a comment'),
]);
};
45 changes: 45 additions & 0 deletions tests/Sets/ContentRepository90/Fixture/node-template.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

use Neos\ContentRepository\Domain\Model\NodeTemplate;

class SomeClass {

public function foo()
{
$nodeTemplate = new NodeTemplate();
$nodeTemplate->getIdentifier();
$nodeTemplate->getName();
$nodeTemplate->getWorkspace();
$nodeTemplate->setIdentifier();
$nodeTemplate->setName();
}
}
-----
<?php

use Neos\ContentRepository\Domain\Model\NodeTemplate;

class SomeClass {

public function foo()
{
// TODO 9.0 migration: !! 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.

$nodeTemplate = new NodeTemplate();
// TODO 9.0 migration: !! NodeTemplate::getIdentifier 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->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();
}
}
Loading