-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathContextGetFirstLevelNodeCacheRector.php
65 lines (54 loc) · 2.05 KB
/
ContextGetFirstLevelNodeCacheRector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
declare (strict_types=1);
namespace Neos\Rector\ContentRepository90\Rules;
use Neos\Rector\Utility\CodeSampleLoader;
use PhpParser\Node;
use PhpParser\NodeFinder;
use PhpParser\NodeTraverser;
use PHPStan\Type\ObjectType;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
final class ContextGetFirstLevelNodeCacheRector extends AbstractRector
{
use AllTraits;
public function __construct(
) {
}
public function getRuleDefinition(): RuleDefinition
{
return CodeSampleLoader::fromFile('"Context::getFirstLevelNodeCache()" will be removed.', __CLASS__);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Node\Stmt\Expression::class];
}
/**
* @param \PhpParser\Node\Stmt\Expression $node
*/
public function refactor(Node $node)
{
assert($node instanceof Node\Stmt\Expression);
if ($this->containsContextGetFirstLevelNodeCache($node->expr)) {
return NodeTraverser::REMOVE_NODE;
}
return $node;
}
private function containsContextGetFirstLevelNodeCache(Node\Expr $expr): bool
{
$nodeFinder = new NodeFinder();
return $nodeFinder->findFirst(
$expr,
fn(Node $node) => $node instanceof Node\Expr\MethodCall
// WARNING: The System cannot infer the Context type properly, as the factory has no types.
// Thus, we simply check on the method name getFirstLevelNodeCache() which is unique enough.
//&& (
// $this->isObjectType($node->var, new ObjectType(LegacyContextStub::class))
// || $this->isObjectType($node->var, new ObjectType('Neos\ContentRepository\Domain\Service\Context'))
// || $this->isObjectType($node->var, new ObjectType('Neos\Neos\Domain\Service\ContentContext'))
&& $this->isName($node->name, 'getFirstLevelNodeCache')
) !== null;
}
}