-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
ConfigurationManagerAddControllerConfigurationMethodRector.php
123 lines (109 loc) · 4.04 KB
/
ConfigurationManagerAddControllerConfigurationMethodRector.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
declare(strict_types=1);
namespace Ssch\TYPO3Rector\TYPO310\v0;
use PhpParser\Builder\Method;
use PhpParser\Builder\Param;
use PhpParser\Node;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Nop;
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/Breaking-88496-MethodGetSwitchableControllerActionsHasBeenRemoved.html
* @see \Ssch\TYPO3Rector\Tests\Rector\v10\v0\ConfigurationManagerAddControllerConfigurationMethodRector\ConfigurationManagerAddControllerConfigurationMethodRectorTest
*/
final class ConfigurationManagerAddControllerConfigurationMethodRector extends AbstractRector implements DocumentedRuleInterface
{
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Class_::class];
}
/**
* @param Class_ $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->isObjectType(
$node,
new ObjectType('TYPO3\CMS\Extbase\Configuration\AbstractConfigurationManager')
)) {
return null;
}
// already checked
$classMethod = $node->getMethod('getControllerConfiguration');
if ($classMethod instanceof ClassMethod) {
return null;
}
$this->addMethodGetControllerConfiguration($node);
return $node;
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Add additional method getControllerConfiguration for AbstractConfigurationManager',
[
new CodeSample(
<<<'CODE_SAMPLE'
final class MyExtbaseConfigurationManager extends AbstractConfigurationManager
{
protected function getSwitchableControllerActions($extensionName, $pluginName)
{
$switchableControllerActions = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['modules'][$pluginName]['controllers'] ?? false;
if ( ! is_array($switchableControllerActions)) {
$switchableControllerActions = [];
}
return $switchableControllerActions;
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
final class MyExtbaseConfigurationManager extends AbstractConfigurationManager
{
protected function getSwitchableControllerActions($extensionName, $pluginName)
{
$switchableControllerActions = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['modules'][$pluginName]['controllers'] ?? false;
if ( ! is_array($switchableControllerActions)) {
$switchableControllerActions = [];
}
return $switchableControllerActions;
}
protected function getControllerConfiguration($extensionName, $pluginName): array
{
return $this->getSwitchableControllerActions($extensionName, $pluginName);
}
}
CODE_SAMPLE
),
]
);
}
private function addMethodGetControllerConfiguration(Class_ $class): void
{
$methodBuilder = new Method('getControllerConfiguration');
$methodBuilder->makeProtected();
$methodBuilder->addParams([
(new Param('extensionName'))->getNode(),
(new Param('pluginName'))->getNode(),
]);
$newMethod = $methodBuilder->getNode();
$newMethod->returnType = new Identifier('array');
$newMethod->stmts[] = new Return_($this->nodeFactory->createMethodCall(
'this',
'getSwitchableControllerActions',
[new Variable('extensionName'), new Variable('pluginName')]
));
$class->stmts[] = new Nop();
$class->stmts[] = $newMethod;
}
}