-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
UseIconsFromSubFolderInIconRegistryRector.php
133 lines (112 loc) · 3.91 KB
/
UseIconsFromSubFolderInIconRegistryRector.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
124
125
126
127
128
129
130
131
132
133
<?php
declare(strict_types=1);
namespace Ssch\TYPO3Rector\TYPO310\v4;
use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Type\ObjectType;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\Contract\DocumentedRuleInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @changelog https://github.com/TYPO3/typo3/tree/v10.4.10/typo3/sysext/core/Resources/Public/Icons/T3Icons/svgs
* @see \Ssch\TYPO3Rector\Tests\Rector\v10\v4\UseIconsFromSubFolderInIconRegistryRector\UseIconsFromSubFolderInIconRegistryRectorTest
*/
final class UseIconsFromSubFolderInIconRegistryRector extends AbstractRector implements DocumentedRuleInterface
{
/**
* @var string
*/
private const SOURCE = 'source';
/**
* @readonly
*/
private ValueResolver $valueResolver;
public function __construct(ValueResolver $valueResolver)
{
$this->valueResolver = $valueResolver;
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [MethodCall::class];
}
/**
* @param MethodCall $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType(
$node,
new ObjectType('TYPO3\CMS\Core\Imaging\IconRegistry')
)) {
return null;
}
if (! $this->isName($node->name, 'registerIcon')) {
return null;
}
if (! $this->isSvgIconProvider($node)) {
return null;
}
$options = $this->valueResolver->getValue($node->args[2]->value);
if (! is_array($options)) {
return null;
}
if (! array_key_exists(self::SOURCE, $options)) {
return null;
}
$source = (string) $options[self::SOURCE];
if (! str_starts_with($source, 'typo3/sysext/core/Resources/Public/Icons/T3Icons/content/')) {
return null;
}
$options[self::SOURCE] = Strings::replace(
$source,
'#typo3/sysext/core/Resources/Public/Icons/T3Icons/content/#i',
'typo3/sysext/core/Resources/Public/Icons/T3Icons/svgs/content/'
);
$node->args[2]->value = $this->nodeFactory->createArray($options);
return null;
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Use icons from subfolder in IconRegistry', [
new CodeSample(
<<<'CODE_SAMPLE'
\TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Imaging\IconRegistry::class)
->registerIcon(
'apps-pagetree-reference',
TYPO3\CMS\Core\Imaging\IconProvider\SvgIconProvider::class,
[
'source' => 'typo3/sysext/core/Resources/Public/Icons/T3Icons/content/content-text.svg',
]
);
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
\TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Imaging\IconRegistry::class)
->registerIcon(
'apps-pagetree-reference',
TYPO3\CMS\Core\Imaging\IconProvider\SvgIconProvider::class,
[
'source' => 'typo3/sysext/core/Resources/Public/Icons/T3Icons/svgs/content/content-text.svg',
]
);
CODE_SAMPLE
),
]);
}
private function isSvgIconProvider(MethodCall $methodCall): bool
{
$iconProviderClassName = $this->valueResolver->getValue($methodCall->args[1]->value);
if ($iconProviderClassName === null) {
return false;
}
return (new ObjectType($iconProviderClassName))->equals(
new ObjectType('TYPO3\CMS\Core\Imaging\IconProvider\SvgIconProvider')
);
}
}