8000 [Symfony 4.3] Add GetCurrencyBundleMethodCallsToIntlRector by TomasVotruba · Pull Request #386 · rectorphp/rector-symfony · GitHub
[go: up one dir, main page]

Skip to content

[Symfony 4.3] Add GetCurrencyBundleMethodCallsToIntlRector #386

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/sets/symfony/symfony43.php
8000
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Rector\Renaming\Rector\MethodCall\RenameMethodRector;
use Rector\Renaming\Rector\Name\RenameClassRector;
use Rector\Renaming\ValueObject\MethodCallRename;
use Rector\Symfony\Rector\MethodCall\GetCurrencyBundleMethodCallsToIntlRector;
use Rector\Symfony\Rector\MethodCall\MakeDispatchFirstArgumentEventRector;
use Rector\Symfony\Rector\MethodCall\WebTestCaseAssertIsSuccessfulRector;
use Rector\Symfony\Rector\MethodCall\WebTestCaseAssertResponseCodeRector;
Expand All @@ -23,6 +24,7 @@
WebTestCaseAssertResponseCodeRector::class,
TwigBundleFilesystemLoaderToTwigRector::class,
MakeDispatchFirstArgumentEventRector::class,
GetCurrencyBundleMethodCallsToIntlRector::class,
]);

$rectorConfig->ruleWithConfiguration(RenameMethodRector::class, [
Expand Down
17 changes: 16 additions & 1 deletion docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 82 Rules Overview
# 83 Rules Overview

## ActionSuffixRemoverRector

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

<br>

## GetCurrencyBundleMethodCallsToIntlRector

Intl static bundle method were changed to direct static calls

- class: [`Rector\Symfony\Rector\MethodCall\GetCurrencyBundleMethodCallsToIntlRector`](../src/Rector/MethodCall/GetCurrencyBundleMethodCallsToIntlRector.php)

```diff
-$currencyBundle = \Symfony\Component\Intl\Intl::getCurrencyBundle();
-
-$currencyNames = $currencyBundle->getCurrencyNames();
+$currencyNames = \Symfony\Component\Intl\Currencies::getNames();
```

<br>

## GetHelperControllerToServiceRector

Replace `$this->getDoctrine()` and `$this->dispatchMessage()` calls in AbstractController with direct service use
Expand Down
109 changes: 109 additions & 0 deletions src/Rector/MethodCall/GetCurrencyBundleMethodCallsToIntlRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

declare(strict_types=1);

namespace Rector\Symfony\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\Core\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\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;
}
}
44 changes: 44 additions & 0 deletions src/ValueObject/IntlBundleClassToNewClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace Rector\Symfony\ValueObject;

use Rector\Core\Validation\RectorAssert;
use Webmozart\Assert\Assert;

final class IntlBundleClassToNewClass
{
/**
* @param array<string, string> $oldToNewMethods
*/
public function __construct(
private readonly string $oldClass,
private readonly string $newClass,
private readonly array $oldToNewMethods
) {
RectorAssert::className($oldClass);
RectorAssert::className($newClass);

Assert::allString($oldToNewMethods);
Assert::allString(array_keys($oldToNewMethods));
}

public function getOldClass(): string
{
return $this->oldClass;
}

public function getNewClass(): string
{
return $this->newClass;
}

/**
* @return array<string, string>
*/
public function getOldToNewMethods(): array
{
return $this->oldToNewMethods;
}
}
12 changes: 12 additions & 0 deletions stubs/Symfony/Component/Intl/Intl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Symfony\Component\Intl;

use Symfony\Component\Intl\ResourceBundle\CurrencyBundleInterface;

final class Intl
{
public static function getCurrencyBundle(): CurrencyBundleInterface
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Symfony\Component\Intl\ResourceBundle;

interface CurrencyBundleInterface
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Rector\Tests\Naming\Rector\MethodCall\GetCurrencyBundleMethodCallsToIntlRector\Fixture;

$currencyBundle = \Symfony\Component\Intl\Intl::getCurrencyBundle();

$currencyNames = $currencyBundle->getCurrencyNames();

?>
-----
<?php

namespace Rector\Tests\Naming\Rector\MethodCall\GetCurrencyBundleMethodCallsToIntlRector\Fixture;

$currencyBundle = \Symfony\Component\Intl\Intl::getCurrencyBundle();

$currencyNames = \Symfony\Component\Intl\Currencies::getNames();

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Symfony\Tests\Rector\MethodCall\GetCurrencyBundleMethodCallsToIntlRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class GetCurrencyBundleMethodCallsToIntlRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Symfony\Rector\MethodCall\GetCurrencyBundleMethodCallsToIntlRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(GetCurrencyBundleMethodCallsToIntlRector::class);
};
0