Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d1e6641

Browse filesBrowse files
authored
[Symfony 4.3] Add GetCurrencyBundleMethodCallsToIntlRector (#386)
1 parent c5abb44 commit d1e6641
Copy full SHA for d1e6641

File tree

Expand file treeCollapse file tree

9 files changed

+247
-1
lines changed
Filter options
Expand file treeCollapse file tree

9 files changed

+247
-1
lines changed

‎config/sets/symfony/symfony43.php

Copy file name to clipboardExpand all lines: config/sets/symfony/symfony43.php
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Rector\Renaming\Rector\MethodCall\RenameMethodRector;
1111
use Rector\Renaming\Rector\Name\RenameClassRector;
1212
use Rector\Renaming\ValueObject\MethodCallRename;
13+
use Rector\Symfony\Rector\MethodCall\GetCurrencyBundleMethodCallsToIntlRector;
1314
use Rector\Symfony\Rector\MethodCall\MakeDispatchFirstArgumentEventRector;
1415
use Rector\Symfony\Rector\MethodCall\WebTestCaseAssertIsSuccessfulRector;
1516
use Rector\Symfony\Rector\MethodCall\WebTestCaseAssertResponseCodeRector;
@@ -23,6 +24,7 @@
2324
WebTestCaseAssertResponseCodeRector::class,
2425
TwigBundleFilesystemLoaderToTwigRector::class,
2526
MakeDispatchFirstArgumentEventRector::class,
27+
GetCurrencyBundleMethodCallsToIntlRector::class,
2628
]);
2729

2830
$rectorConfig->ruleWithConfiguration(RenameMethodRector::class, [

‎docs/rector_rules_overview.md

Copy file name to clipboardExpand all lines: docs/rector_rules_overview.md
+16-1Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 82 Rules Overview
1+
# 83 Rules Overview
22

33
## ActionSuffixRemoverRector
44

@@ -676,6 +676,21 @@ Move constructor dependency from form type class to an `$options` parameter
676676

677677
<br>
678678

679+
## GetCurrencyBundleMethodCallsToIntlRector
680+
681+
Intl static bundle method were changed to direct static calls
682+
683+
- class: [`Rector\Symfony\Rector\MethodCall\GetCurrencyBundleMethodCallsToIntlRector`](../src/Rector/MethodCall/GetCurrencyBundleMethodCallsToIntlRector.php)
684+
685+
```diff
686+
-$currencyBundle = \Symfony\Component\Intl\Intl::getCurrencyBundle();
687+
-
688+
-$currencyNames = $currencyBundle->getCurrencyNames();
689+
+$currencyNames = \Symfony\Component\Intl\Currencies::getNames();
690+
```
691+
692+
<br>
693+
679694
## GetHelperControllerToServiceRector
680695

681696
Replace `$this->getDoctrine()` and `$this->dispatchMessage()` calls in AbstractController with direct service use
+109Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Symfony\Rector\MethodCall;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Expr\MethodCall;
9+
use PhpParser\Node\Expr\StaticCall;
10+
use PhpParser\Node\Name\FullyQualified;
11+
use PHPStan\Type\ObjectType;
12+
use Rector\Core\Rector\AbstractRector;
13+
use Rector\Symfony\ValueObject\IntlBundleClassToNewClass;
14+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
15+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
16+
17+
/**
18+
* @changelog https://symfony.com/blog/new-in-symfony-4-3-simpler-access-to-intl-data
19+
* @changelog https://github.com/symfony/symfony/pull/28846
20+
*
21+
* @see \Rector\Symfony\Tests\Rector\MethodCall\GetCurrencyBundleMethodCallsToIntlRector\GetCurrencyBundleMethodCallsToIntlRectorTest
22+
*/
23+
final class GetCurrencyBundleMethodCallsToIntlRector extends AbstractRector
24+
{
25+
/**
26+
* @var IntlBundleClassToNewClass[]
27+
*/
28+
private array $intlBundleClassesToNewClasses = [];
29+
30+
public function __construct()
31+
{
32+
$this->intlBundleClassesToNewClasses[] = new IntlBundleClassToNewClass(
33+
'Symfony\Component\Intl\ResourceBundle\LanguageBundleInterface',
34+
'Symfony\Component\Intl\Languages',
35+
[
36+
'getLanguageNames' => 'getNames',
37+
'getLanguageName' => 'getName',
38+
]
39+
);
40+
41+
$this->intlBundleClassesToNewClasses[] = new IntlBundleClassToNewClass(
42+
'Symfony\Component\Intl\ResourceBundle\RegionBundleInterface',
43+
'Symfony\Component\Intl\Currencies',
44+
[
45+
'getCountryNames' => 'getNames',
46+
'getCountryName' => 'getName',
47+
]
48+
);
49+
50+
$this->intlBundleClassesToNewClasses[] = new IntlBundleClassToNewClass(
51+
'Symfony\Component\Intl\ResourceBundle\CurrencyBundleInterface',
52+
'Symfony\Component\Intl\Currencies',
53+
[
54+
'getCurrencyNames' => 'getNames',
55+
'getCurrencyName' => 'getName',
56+
'getCurrencySymbol' => 'getSymbol',
57+
'getFractionDigits' => 'getFractionDigits',
58+
]
59+
);
60+
}
61+
62+
public function getRuleDefinition(): RuleDefinition
63+
{
64+
return new RuleDefinition('Intl static bundle method were changed to direct static calls', [
65+
new CodeSample(
66+
<<<'CODE_SAMPLE'
67+
$currencyBundle = \Symfony\Component\Intl\Intl::getCurrencyBundle();
68+
69+
$currencyNames = $currencyBundle->getCurrencyNames();
70+
CODE_SAMPLE
71+
,
72+
<<<'CODE_SAMPLE'
73+
$currencyNames = \Symfony\Component\Intl\Currencies::getNames();
74+
CODE_SAMPLE
75+
),
76+
]);
77+
}
78+
79+
/**
80+
* @return array<class-string<Node>>
81+
*/
82+
public function getNodeTypes(): array
83+
{
84+
return [MethodCall::class];
85+
}
86+
87+
/**
88+
* @param MethodCall $node
89+
*/
90+
public function refactor(Node $node): ?StaticCall
91+
{
92+
foreach ($this->intlBundleClassesToNewClasses as $intlBundleClassToNewClass) {
93+
if (! $this->isObjectType($node->var, new ObjectType($intlBundleClassToNewClass->getOldClass()))) {
94+
continue;
95+
}
96+
97+
foreach ($intlBundleClassToNewClass->getOldToNewMethods() as $oldMethodName => $newMethodName) {
98+
if (! $this->isName($node->name, $oldMethodName)) {
99+
continue;
100+
}
101+
102+
$currenciesFullyQualified = new FullyQualified($intlBundleClassToNewClass->getNewClass());
103+
return new StaticCall($currenciesFullyQualified, $newMethodName);
104+
}
105+
}
106+
107+
return null;
108+
}
109+
}
+44Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Symfony\ValueObject;
6+
7+
use Rector\Core\Validation\RectorAssert;
8+
use Webmozart\Assert\Assert;
9+
10+
final class IntlBundleClassToNewClass
11+
{
12+
/**
13+
* @param array<string, string> $oldToNewMethods
14+
*/
15+
public function __construct(
16+
private readonly string $oldClass,
17+
private readonly string $newClass,
18+
private readonly array $oldToNewMethods
19+
) {
20+
RectorAssert::className($oldClass);
21+
RectorAssert::className($newClass);
22+
23+
Assert::allString($oldToNewMethods);
24+
Assert::allString(array_keys($oldToNewMethods));
25+
}
26+
27+
public function getOldClass(): string
28+
{
29+
return $this->oldClass;
30+
}
31+
32+
public function getNewClass(): string
33+
{
34+
return $this->newClass;
35+
}
36+
37+
/**
38+
* @return array<string, string>
39+
*/
40+
public function getOldToNewMethods(): array
41+
{
42+
return $this->oldToNewMethods;
43+
}
44+
}

‎stubs/Symfony/Component/Intl/Intl.php

Copy file name to clipboard
+12Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Symfony\Component\Intl;
4+
5+
use Symfony\Component\Intl\ResourceBundle\CurrencyBundleInterface;
6+
7+
final class Intl
8+
{
9+
public static function getCurrencyBundle(): CurrencyBundleInterface
10+
{
11+
}
12+
}
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Symfony\Component\Intl\ResourceBundle;
4+
5+
interface CurrencyBundleInterface
6+
{
7+
}
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Rector\Tests\Naming\Rector\MethodCall\GetCurrencyBundleMethodCallsToIntlRector\Fixture;
4+
5+
$currencyBundle = \Symfony\Component\Intl\Intl::getCurrencyBundle();
6+
7+
$currencyNames = $currencyBundle->getCurrencyNames();
8+
9+
?>
10+
-----
11+
<?php
12+
13+
namespace Rector\Tests\Naming\Rector\MethodCall\GetCurrencyBundleMethodCallsToIntlRector\Fixture;
14+
15+
$currencyBundle = \Symfony\Component\Intl\Intl::getCurrencyBundle();
16+
17+
$currencyNames = \Symfony\Component\Intl\Currencies::getNames();
18+
19+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Symfony\Tests\Rector\MethodCall\GetCurrencyBundleMethodCallsToIntlRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class GetCurrencyBundleMethodCallsToIntlRectorTest extends AbstractRectorTestCase
12+
{
13+
#[DataProvider('provideData')]
14+
public function test(string $filePath): void
15+
{
16+
$this->doTestFile($filePath);
17+
}
18+
19+
public static function provideData(): Iterator
20+
{
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
22+
}
23+
24+
public function provideConfigFilePath(): string
25+
{
26+
return __DIR__ . '/config/configured_rule.php';
27+
}
28+
}
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\Symfony\Rector\MethodCall\GetCurrencyBundleMethodCallsToIntlRector;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->rule(GetCurrencyBundleMethodCallsToIntlRector::class);
10+
};

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.