forked from rectorphp/rector-symfony
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetCurrencyBundleMethodCallsToIntlRector.php
More file actions
109 lines (95 loc) · 3.54 KB
/
GetCurrencyBundleMethodCallsToIntlRector.php
File metadata and controls
109 lines (95 loc) · 3.54 KB
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
<?php
declare(strict_types=1);
namespace Rector\Symfony\Symfony43\Rector\MethodCall;
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Name\FullyQualified;
use PHPStan\Type\ObjectType;
use Rector\Rector\AbstractRector;
use Rector\Symfony\ValueObject\IntlBundleClassToNewClass;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @changelog https://symfony.com/blog/new-in-symfony-4-3-simpler-access-to-intl-data
* @changelog https://github.com/symfony/symfony/pull/28846
*
* @see \Rector\Symfony\Tests\Symfony43\Rector\MethodCall\GetCurrencyBundleMethodCallsToIntlRector\GetCurrencyBundleMethodCallsToIntlRectorTest
*/
final class GetCurrencyBundleMethodCallsToIntlRector extends AbstractRector
{
/**
* @var IntlBundleClassToNewClass[]
*/
private array $intlBundleClassesToNewClasses = [];
public function __construct()
{
$this->intlBundleClassesToNewClasses[] = new IntlBundleClassToNewClass(
'Symfony\Component\Intl\ResourceBundle\LanguageBundleInterface',
'Symfony\Component\Intl\Languages',
[
'getLanguageNames' => 'getNames',
'getLanguageName' => 'getName',
]
);
$this->intlBundleClassesToNewClasses[] = new IntlBundleClassToNewClass(
'Symfony\Component\Intl\ResourceBundle\RegionBundleInterface',
'Symfony\Component\Intl\Currencies',
[
'getCountryNames' => 'getNames',
'getCountryName' => 'getName',
]
);
$this->intlBundleClassesToNewClasses[] = new IntlBundleClassToNewClass(
'Symfony\Component\Intl\ResourceBundle\CurrencyBundleInterface',
'Symfony\Component\Intl\Currencies',
[
'getCurrencyNames' => 'getNames',
'getCurrencyName' => 'getName',
'getCurrencySymbol' => 'getSymbol',
'getFractionDigits' => 'getFractionDigits',
]
);
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Intl static bundle method were changed to direct static calls', [
new CodeSample(
<<<'CODE_SAMPLE'
$currencyBundle = \Symfony\Component\Intl\Intl::getCurrencyBundle();
$currencyNames = $currencyBundle->getCurrencyNames();
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$currencyNames = \Symfony\Component\Intl\Currencies::getNames();
CODE_SAMPLE
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [MethodCall::class];
}
/**
* @param MethodCall $node
*/
public function refactor(Node $node): ?StaticCall
{
foreach ($this->intlBundleClassesToNewClasses as $intlBundleClassToNewClass) {
if (! $this->isObjectType($node->var, new ObjectType($intlBundleClassToNewClass->getOldClass()))) {
continue;
}
foreach ($intlBundleClassToNewClass->getOldToNewMethods() as $oldMethodName => $newMethodName) {
if (! $this->isName($node->name, $oldMethodName)) {
continue;
}
$currenciesFullyQualified = new FullyQualified($intlBundleClassToNewClass->getNewClass());
return new StaticCall($currenciesFullyQualified, $newMethodName);
}
}
return null;
}
}