-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
BackendUtilityEditOnClickRector.php
96 lines (86 loc) · 3.2 KB
/
BackendUtilityEditOnClickRector.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
<?php
declare(strict_types=1);
namespace Ssch\TYPO3Rector\TYPO310\v1;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\BinaryOp\Concat;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\String_;
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.1/Deprecation-88787-BackendUtilityEditOnClick.html
* @see \Ssch\TYPO3Rector\Tests\Rector\v10\v1\BackendUtilityEditOnClickRector\BackendUtilityEditOnClickRectorTest
*/
final class BackendUtilityEditOnClickRector extends AbstractRector implements DocumentedRuleInterface
{
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [StaticCall::class];
}
/**
* @param StaticCall $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType(
$node,
new ObjectType('TYPO3\CMS\Backend\Utility\BackendUtility')
)) {
return null;
}
if (! $this->isName($node->name, 'editOnClick')) {
return null;
}
$firstArgument = $node->args[0];
return new Concat($this->createUriBuilderCall($firstArgument), $this->createRequestUriCall());
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Migrate the method BackendUtility::editOnClick() to use UriBuilder API', [
new CodeSample(
<<<'CODE_SAMPLE'
$pid = 2;
$params = '&edit[pages][' . $pid . ']=new&returnNewPageId=1';
$url = BackendUtility::editOnClick($params);
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$pid = 2;
$params = '&edit[pages][' . $pid . ']=new&returnNewPageId=1';
$url = GeneralUtility::makeInstance(UriBuilder::class)->buildUriFromRoute('record_edit') . $params . '&returnUrl=' . rawurlencode(GeneralUtility::getIndpEnv('REQUEST_URI'));;
CODE_SAMPLE
),
]);
}
private function createUriBuilderCall(Arg $firstArgument): Concat
{
return new Concat(new Concat($this->nodeFactory->createMethodCall(
$this->nodeFactory->createStaticCall(
'TYPO3\CMS\Core\Utility\GeneralUtility',
'makeInstance',
[$this->nodeFactory->createClassConstReference('TYPO3\CMS\Backend\Routing\UriBuilder')]
),
'buildUriFromRoute',
[$this->nodeFactory->createArg('record_edit')]
), $firstArgument->value), new String_('&returnUrl='));
}
private function createRequestUriCall(): FuncCall
{
return new FuncCall(new Name('rawurlencode'), [
$this->nodeFactory->createArg(
$this->nodeFactory->createStaticCall('TYPO3\CMS\Core\Utility\GeneralUtility', 'getIndpEnv', [
$this->nodeFactory->createArg('REQUEST_URI'),
])
),
]);
}
}