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

TASK Refactor ContentContext methods #36

Merged
merged 5 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
76 changes: 42 additions & 34 deletions config/set/contentrepository-90.php

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions src/ContentRepository90/Legacy/NodeLegacyStub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Neos\Rector\ContentRepository90\Legacy;

/**
* @deprecated
*/
class NodeLegacyStub
{

public function getContext() : LegacyContextStub {
return new LegacyContextStub([]);
}

public function isLive() :bool {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?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 Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Rector\PostRector\Collector\NodesToAddCollector;
use PhpParser\Node\Expr\Assign;

final class ContextGetCurrentRenderingModeRector extends AbstractRector
{
use AllTraits;
use ContextRectorTrait;

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


public function getRuleDefinition(): RuleDefinition
{
return CodeSampleLoader::fromFile('"ContentContext::getCurrentRenderingMode()" will be replaced with RenderingModeService::findByCurrentUser().', __CLASS__);
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Node\Expr\MethodCall::class];
}

/**
* @param \PhpParser\Node\Stmt\Expression $node
*/
public function refactor(Node $node): ?Node
{
assert($node instanceof Node\Expr\MethodCall);

$oldContextMethod = 'getCurrentRenderingMode';
if (
$this->isContextWithMethod($node, $oldContextMethod)
) {

return $this->nodeFactory->createMethodCall(
$this->nodeFactory->createPropertyFetch(
'this',
'renderingModeService'
),
'findByCurrentUser'
);
}

return null;
}

}
67 changes: 67 additions & 0 deletions src/ContentRepository90/Rules/ContextIsInBackendRector.php
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 Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Rector\PostRector\Collector\NodesToAddCollector;
use PhpParser\Node\Expr\Assign;
use PhpParser\NodeDumper;

final class ContextIsInBackendRector extends AbstractRector
{
use AllTraits;
use ContextRectorTrait;

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


public function getRuleDefinition(): RuleDefinition
{
return CodeSampleLoader::fromFile('"ContentContext::isLive()" will be replaced with RenderingModeService::findByCurrentUser().', __CLASS__);
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Node\Expr\MethodCall::class];
}

/**
* @param \PhpParser\Node\Stmt\Expression $node
*/
public function refactor(Node $node): ?Node
{
assert($node instanceof Node\Expr\MethodCall);

$oldContextMethod = 'isInBackend';
if ($this->isContextWithMethod($node, $oldContextMethod)) {

$renderingModeService = $this->nodeFactory->createMethodCall(
$this->nodeFactory->createPropertyFetch(
'this',
'renderingModeService'
),
'findByCurrentUser'
);

return $this->nodeFactory->createPropertyFetch(
$renderingModeService, 'isEdit'
);

}

return null;
}

}
80 changes: 80 additions & 0 deletions src/ContentRepository90/Rules/ContextIsLiveRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?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 Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Rector\PostRector\Collector\NodesToAddCollector;
use PhpParser\Node\Expr\Assign;
use PhpParser\NodeDumper;

final class ContextIsLiveRector extends AbstractRector
{
use AllTraits;
use ContextRectorTrait;

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


public function getRuleDefinition(): RuleDefinition
{
return CodeSampleLoader::fromFile('"ContentContext::isLive()" will be replaced with RenderingModeService::findByCurrentUser().', __CLASS__);
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Node\Expr\MethodCall::class];
}

/**
* @param \PhpParser\Node\Stmt\Expression $node
*/
public function refactor(Node $node): ?Node
{
assert($node instanceof Node\Expr\MethodCall);

$oldContextMethod = 'isLive';
if ($this->isContextWithMethod($node, $oldContextMethod)) {

$renderingModeService = $this->nodeFactory->createMethodCall(
$this->nodeFactory->createPropertyFetch(
'this',
'renderingModeService'
),
'findByCurrentUser'
);

$isEdit = $this->nodeFactory->createPropertyFetch(
$renderingModeService, 'isEdit'
);
$isPreview = $this->nodeFactory->createPropertyFetch(
$renderingModeService, 'isPreview'
);

$replacementExpression = new Node\Expr\BinaryOp\BooleanAnd(
new Node\Expr\BooleanNot(
$isEdit
),
new Node\Expr\BooleanNot(
$isPreview
),
);

return $replacementExpression;
}

return null;
}

}
27 changes: 27 additions & 0 deletions src/ContentRepository90/Rules/ContextRectorTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare (strict_types=1);

namespace Neos\Rector\ContentRepository90\Rules;

use Neos\Rector\Utility\CodeSampleLoader;
use PhpParser\Node;
use PhpParser\NodeFinder;
use PHPStan\Type\ObjectType;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Rector\PostRector\Collector\NodesToAddCollector;
use PhpParser\Node\Expr\Assign;
use PhpParser\NodeDumper;

trait ContextRectorTrait
{
private function isContextWithMethod(Node\Expr\MethodCall $node, string $methodName): bool
{
return (
$node->name == $methodName
&& $this->isObjectType($node->var, new ObjectType('Neos\Rector\ContentRepository90\Legacy\LegacyContextStub')
)
);
}
}
2 changes: 1 addition & 1 deletion src/ContentRepository90/Rules/NodeFindParentNodeRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function refactor(Node $node): ?Node
{
assert($node instanceof Node\Expr\MethodCall);

if (!$this->isObjectType($node->var, new ObjectType(\Neos\ContentRepository\Core\Projection\ContentGraph\Node::class))) {
if (!$this->isObjectType($node->var, new ObjectType(\Neos\Rector\ContentRepository90\Legacy\NodeLegacyStub::class))) {
return null;
}
if (!$this->isName($node->name, 'findParentNode')) {
Expand Down
2 changes: 1 addition & 1 deletion src/ContentRepository90/Rules/NodeGetChildNodesRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function refactor(Node $node) : ?Node
{
assert($node instanceof Node\Expr\MethodCall);

if (!$this->isObjectType($node->var, new ObjectType(\Neos\ContentRepository\Core\Projection\ContentGraph\Node::class))) {
if (!$this->isObjectType($node->var, new ObjectType(\Neos\Rector\ContentRepository90\Legacy\NodeLegacyStub::class))) {
return null;
}
if (!$this->isName($node->name, 'getChildNodes')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function refactor(Node $node): ?Node
return null;
}

if (!$this->isObjectType($node->var->var, new ObjectType(\Neos\ContentRepository\Core\Projection\ContentGraph\Node::class))) {
if (!$this->isObjectType($node->var->var, new ObjectType(\Neos\Rector\ContentRepository90\Legacy\NodeLegacyStub::class))) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function refactor(Node $node): ?Node
return null;
}

if (!$this->isObjectType($node->var->var, new ObjectType(\Neos\ContentRepository\Core\Projection\ContentGraph\Node::class))) {
if (!$this->isObjectType($node->var->var, new ObjectType(\Neos\Rector\ContentRepository90\Legacy\NodeLegacyStub::class))) {
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion src/ContentRepository90/Rules/NodeGetDepthRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function refactor(Node $node) : ?Node
{
assert($node instanceof Node\Expr\MethodCall);

if (!$this->isObjectType($node->var, new ObjectType(\Neos\ContentRepository\Core\Projection\ContentGraph\Node::class))) {
if (!$this->isObjectType($node->var, new ObjectType(\Neos\Rector\ContentRepository90\Legacy\NodeLegacyStub::class))) {
return null;
}
if (!$this->isName($node->name, 'getDepth')) {
Expand Down
2 changes: 1 addition & 1 deletion src/ContentRepository90/Rules/NodeGetDimensionsRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function refactor(Node $node) : ?Node
{
assert($node instanceof Node\Expr\MethodCall);

if (!$this->isObjectType($node->var, new ObjectType(\Neos\ContentRepository\Core\Projection\ContentGraph\Node::class))) {
if (!$this->isObjectType($node->var, new ObjectType(\Neos\Rector\ContentRepository90\Legacy\NodeLegacyStub::class))) {
return null;
}
if (!$this->isName($node->name, 'getDimensions')) {
Expand Down
2 changes: 1 addition & 1 deletion src/ContentRepository90/Rules/NodeGetIdentifierRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function refactor(Node $node): ?Node
{
assert($node instanceof Node\Expr\MethodCall);

if (!$this->isObjectType($node->var, new ObjectType(\Neos\ContentRepository\Core\Projection\ContentGraph\Node::class))) {
if (!$this->isObjectType($node->var, new ObjectType(\Neos\Rector\ContentRepository90\Legacy\NodeLegacyStub::class))) {
return null;
}
if (!$this->isName($node->name, 'getIdentifier')) {
Expand Down
2 changes: 1 addition & 1 deletion src/ContentRepository90/Rules/NodeGetParentRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function refactor(Node $node) : ?Node
{
assert($node instanceof Node\Expr\MethodCall);

if (!$this->isObjectType($node->var, new ObjectType(\Neos\ContentRepository\Core\Projection\ContentGraph\Node::class))) {
if (!$this->isObjectType($node->var, new ObjectType(\Neos\Rector\ContentRepository90\Legacy\NodeLegacyStub::class))) {
return null;
}
if (!$this->isName($node->name, 'getParent')) {
Expand Down
2 changes: 1 addition & 1 deletion src/ContentRepository90/Rules/NodeGetPathRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function refactor(Node $node) : ?Node
{
assert($node instanceof Node\Expr\MethodCall);

if (!$this->isObjectType($node->var, new ObjectType(\Neos\ContentRepository\Core\Projection\ContentGraph\Node::class))) {
if (!$this->isObjectType($node->var, new ObjectType(\Neos\Rector\ContentRepository90\Legacy\NodeLegacyStub::class))) {
return null;
}
if (!$this->isName($node->name, 'getPath')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function refactor(Node $node) : ?Node
{
assert($node instanceof MethodCall);

if (!$this->isObjectType($node->var, new ObjectType(\Neos\ContentRepository\Core\Projection\ContentGraph\Node::class))) {
if (!$this->isObjectType($node->var, new ObjectType(\Neos\Rector\ContentRepository90\Legacy\NodeLegacyStub::class))) {
return null;
}
if (!$this->isName($node->name, 'isHiddenInIndex')) {
Expand Down
2 changes: 1 addition & 1 deletion src/ContentRepository90/Rules/NodeIsHiddenRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function refactor(Node $node) : ?Node
{
assert($node instanceof Node\Expr\MethodCall);

if (!$this->isObjectType($node->var, new ObjectType(\Neos\ContentRepository\Core\Projection\ContentGraph\Node::class))) {
if (!$this->isObjectType($node->var, new ObjectType(\Neos\Rector\ContentRepository90\Legacy\NodeLegacyStub::class))) {
return null;
}
if (!$this->isName($node->name, 'isHidden')) {
Expand Down
3 changes: 2 additions & 1 deletion src/Generic/Rules/MethodCallToWarningCommentRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Rector\PostRector\Collector\NodesToAddCollector;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;
use Neos\Rector\ContentRepository90\Legacy\NodeLegacyStub;

final class MethodCallToWarningCommentRector extends AbstractRector implements ConfigurableRectorInterface
{
Expand All @@ -32,7 +33,7 @@ public function __construct(
public function getRuleDefinition(): RuleDefinition
{
return CodeSampleLoader::fromFile('"Warning comments for various non-supported use cases', __CLASS__, [
new MethodCallToWarningComment(Node::class, 'getWorkspace', '!! Node::getWorkspace() does not make sense anymore concept-wise. In Neos < 9, it pointed to the workspace where the node was *at home at*. Now, the closest we have here is the node identity.')
new MethodCallToWarningComment(NodeLegacyStub::class, 'getWorkspace', '!! Node::getWorkspace() does not make sense anymore concept-wise. In Neos < 9, it pointed to the workspace where the node was *at home at*. Now, the closest we have here is the node identity.')
]);
}

Expand Down
Loading