-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
BackendUtilityGetViewDomainToPageRouterRector.php
104 lines (89 loc) · 3.26 KB
/
BackendUtilityGetViewDomainToPageRouterRector.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
declare(strict_types=1);
namespace Ssch\TYPO3Rector\TYPO310\v0;
use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Return_;
use PHPStan\Type\ObjectType;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\Contract\DocumentedRuleInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @changelog https://docs.typo3.org/c/typo3/cms-core/main/en-us/Changelog/10.0/Deprecation-88499-BackendUtilitygetViewDomain.html
* @see \Ssch\TYPO3Rector\Tests\Rector\v10\v0\BackendUtilityGetViewDomainToPageRouterRector\BackendUtilityGetViewDomainToPageRouterRectorTest
*/
final class BackendUtilityGetViewDomainToPageRouterRector extends AbstractRector implements DocumentedRuleInterface
{
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Expression::class, Return_::class];
}
/**
* @param Expression|Return_ $node
* @return Node[]|null
*/
public function refactor(Node $node): ?array
{
$methodCall = null;
if ($node instanceof Return_) {
$methodCall = $node->expr;
} elseif ($node->expr instanceof Assign) {
$methodCall = $node->expr->expr;
}
if (! $methodCall instanceof StaticCall) {
return null;
}
if (! $this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType(
$methodCall,
new ObjectType('TYPO3\CMS\Backend\Utility\BackendUtility')
)) {
return null;
}
if (! $this->isName($methodCall->name, 'getViewDomain')) {
return null;
}
$siteAssign = new Expression(new Assign(new Variable('site'), $this->nodeFactory->createMethodCall(
$this->nodeFactory->createStaticCall('TYPO3\CMS\Core\Utility\GeneralUtility', 'makeInstance', [
$this->nodeFactory->createClassConstReference('TYPO3\CMS\Core\Site\SiteFinder'),
]),
'getSiteByPageId',
$methodCall->args
)));
$methodCallGenerateUri = $this->nodeFactory->createMethodCall(
$this->nodeFactory->createMethodCall(new Variable('site'), 'getRouter'),
'generateUri',
[$methodCall->args[0]]
);
if ($node->expr instanceof Assign) {
$node->expr->expr = $methodCallGenerateUri;
} else {
$node->expr = $methodCallGenerateUri;
}
return [$siteAssign, $node];
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Refactor method call BackendUtility::getViewDomain() to PageRouter', [
new CodeSample(
<<<'CODE_SAMPLE'
use TYPO3\CMS\Backend\Utility\BackendUtility;
$domain1 = BackendUtility::getViewDomain(1);
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use TYPO3\CMS\Core\Site\SiteFinder;
use TYPO3\CMS\Core\Utility\GeneralUtility;
$site = GeneralUtility::makeInstance(SiteFinder::class)->getSiteByPageId(1);
$domain1 = $site->getRouter()->generateUri(1);
CODE_SAMPLE
),
]);
}
}