forked from sabbelasichon/typo3-rector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SwitchBehaviorOfArrayUtilityMethodsRector.php
95 lines (81 loc) · 2.72 KB
/
SwitchBehaviorOfArrayUtilityMethodsRector.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
<?php
declare(strict_types=1);
namespace Ssch\TYPO3Rector\TYPO311\v3;
use PhpParser\Node;
use PhpParser\Node\Expr\StaticCall;
use PHPStan\Type\ObjectType;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @changelog https://docs.typo3.org/c/typo3/cms-core/main/en-us/Changelog/11.3/Deprecation-94137-SwitchBehaviorOfArrayUtilityarrayDiffAssocRecursive.html
* @see \Ssch\TYPO3Rector\Tests\Rector\v11\v3\SwitchBehaviorOfArrayUtilityMethodsRector\SwitchBehaviorOfArrayUtilityMethodsRectorTest
*/
final class SwitchBehaviorOfArrayUtilityMethodsRector extends AbstractRector
{
/**
* @readonly
*/
private ValueResolver $valueResolver;
public function __construct(ValueResolver $valueResolver)
{
$this->valueResolver = $valueResolver;
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [StaticCall::class];
}
/**
* @param StaticCall $node
*/
public function refactor(Node $node): ?Node
{
if ($this->shouldSkip($node)) {
return null;
}
$useAssocBehavior = isset($node->args[2]) && $this->valueResolver->getValue($node->args[2]->value);
if ($useAssocBehavior) {
return null;
}
return $this->nodeFactory->createStaticCall(
'TYPO3\CMS\Core\Utility\ArrayUtility',
'arrayDiffKeyRecursive',
[$node->args[0], $node->args[1]]
);
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Handles the methods arrayDiffAssocRecursive() and arrayDiffKeyRecursive() of ArrayUtility',
[
new CodeSample(
<<<'CODE_SAMPLE'
$foo = ArrayUtility::arrayDiffAssocRecursive([], [], true);
$bar = ArrayUtility::arrayDiffAssocRecursive([], [], false);
$test = ArrayUtility::arrayDiffAssocRecursive([], []);
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$foo = ArrayUtility::arrayDiffAssocRecursive([], [], true);
$bar = ArrayUtility::arrayDiffKeyRecursive([], []);
$test = ArrayUtility::arrayDiffKeyRecursive([], []);
CODE_SAMPLE
),
]
);
}
private function shouldSkip(StaticCall $staticCall): bool
{
if (! $this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType(
$staticCall,
new ObjectType('TYPO3\CMS\Core\Utility\ArrayUtility')
)) {
return true;
}
return ! $this->isName($staticCall->name, 'arrayDiffAssocRecursive');
}
}