-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathWorkspaceGetNameRector.php
62 lines (51 loc) · 1.72 KB
/
WorkspaceGetNameRector.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
<?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\SharedModel\Workspace\Workspace;
final class WorkspaceGetNameRector extends AbstractRector
{
use AllTraits;
public function __construct(
private readonly NodesToAddCollector $nodesToAddCollector,
)
{
}
public function getRuleDefinition(): RuleDefinition
{
return CodeSampleLoader::fromFile('"Workspace::getName()" will be rewritten', __CLASS__);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [\PhpParser\Node\Expr\MethodCall::class];
}
/**
* @param \PhpParser\Node\Expr\MethodCall $node
*/
public function refactor(Node $node): ?Node
{
assert($node instanceof Node\Expr\MethodCall);
if (!$this->isObjectType($node->var, new ObjectType(Workspace::class))) {
return null;
}
if (!$this->isName($node->name, 'getName')) {
return null;
}
$this->nodesToAddCollector->addNodesBeforeNode(
[
self::todoComment('Check if you could change your code to work with the WorkspaceName value object instead.')
],
$node
);
$propertyFetchAggregateId = $this->nodeFactory->createPropertyFetch($node->var, 'workspaceName');
return $this->nodeFactory->createPropertyFetch($propertyFetchAggregateId, 'value');
}
}