-
Notifications
You must be signed in to change notification settings - Fork 24
[DowngradePhp84] Downgrade RoundingMode enum #260
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
samsonasik
merged 13 commits into
rectorphp:main
from
jorgsowa:feat/downgrade-rounding-mode-php84
Dec 20, 2024
Merged
Changes from 4 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
d497751
[DowngradePhp84] Downgrade RoundingMode enum
jorgsowa 8d1c1ca
Review comments
jorgsowa c78fd72
Fixed description of rule
jorgsowa 978bfee
Registered rule to downgrade-php84 ruleset
jorgsowa e699752
Used FullyQualified for constant name
jorgsowa 26ea27c
Fixed PHPStan issue
jorgsowa 8285cfb
Formatting
jorgsowa 97394aa
run rector once more
samsonasik 46767bf
ensure FullyQualified check
samsonasik b27b29d
add no namespace fixture
samsonasik 942d28a
from use
samsonasik 9bab626
run rector once more
samsonasik d2dafe6
cs fix
samsonasik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
...4/Rector/FuncCall/DowngradeRoundingModeEnumRector/DowngradeRoundingModeEnumRectorTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\Tests\DowngradePhp84\Rector\FuncCall\DowngradeRoundingModeEnumRector; | ||
|
|
||
| use Iterator; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
|
||
| final class DowngradeRoundingModeEnumRectorTest 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'; | ||
| } | ||
| } |
37 changes: 37 additions & 0 deletions
37
...ts/DowngradePhp84/Rector/FuncCall/DowngradeRoundingModeEnumRector/Fixture/fixture.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\Php84\Rector\FuncCall\RoundingModeEnumRector; | ||
|
|
||
| round(1.5, 0); | ||
| round(1.5); | ||
| round(1.5, 0, \RoundingMode::HalfAwayFromZero); | ||
| round(1.5, 0, \RoundingMode::HalfTowardsZero); | ||
| round(1.5, 0, \RoundingMode::HalfEven); | ||
| round(1.5, 0, \RoundingMode::HalfOdd); | ||
| round(1.5, 0, \RoundingMode::TowardsZero); | ||
| round(1.5, 0, \RoundingMode::AwayFromZero); | ||
| round(1.5, 0, \RoundingMode::NegativeInfinity); | ||
| round(1.5, 0, \RoundingMode::PositiveInfinity); | ||
| round(1.5, 0, 'invalid'); | ||
| round(1.5, 0, PHP_INT_MAX); | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\Php84\Rector\FuncCall\RoundingModeEnumRector; | ||
|
|
||
| round(1.5, 0); | ||
| round(1.5); | ||
| round(1.5, 0, PHP_ROUND_HALF_UP); | ||
| round(1.5, 0, PHP_ROUND_HALF_DOWN); | ||
| round(1.5, 0, PHP_ROUND_HALF_EVEN); | ||
| round(1.5, 0, PHP_ROUND_HALF_ODD); | ||
| round(1.5, 0, \RoundingMode::TowardsZero); | ||
| round(1.5, 0, \RoundingMode::AwayFromZero); | ||
| round(1.5, 0, \RoundingMode::NegativeInfinity); | ||
| round(1.5, 0, \RoundingMode::PositiveInfinity); | ||
| round(1.5, 0, 'invalid'); | ||
| round(1.5, 0, PHP_INT_MAX); | ||
|
|
||
| ?> |
10 changes: 10 additions & 0 deletions
10
...uncCall/DowngradeRoundingModeEnumRector/Fixture/skip_indirect_use_of_roundin_mode.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\Php84\Rector\FuncCall\RoundingModeEnumRector; | ||
|
|
||
|
|
||
| $mode = \RoundingMode::HalfTowardsZero; | ||
|
|
||
| round(1.5, 0, $mode); | ||
|
|
||
| ?> |
10 changes: 10 additions & 0 deletions
10
...DowngradePhp84/Rector/FuncCall/DowngradeRoundingModeEnumRector/config/configured_rule.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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\DowngradePhp84\Rector\FuncCall\DowngradeRoundingModeEnumRector; | ||
|
|
||
| return static function (RectorConfig $rectorConfig): void { | ||
| $rectorConfig->rule(DowngradeRoundingModeEnumRector::class); | ||
| }; |
94 changes: 94 additions & 0 deletions
94
rules/DowngradePhp84/Rector/FuncCall/DowngradeRoundingModeEnumRector.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\DowngradePhp84\Rector\FuncCall; | ||
|
|
||
| use PhpParser\Node; | ||
| use PhpParser\Node\Expr\ClassConstFetch; | ||
| use PhpParser\Node\Expr\MethodCall; | ||
| use Rector\Rector\AbstractRector; | ||
| use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
| use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
|
||
| /** | ||
| * @changelog wiki.php.net/rfc/correctly_name_the_rounding_mode_and_make_it_an_enum | ||
| * | ||
| * @see \Rector\Tests\DowngradePhp84\Rector\FuncCall\DowngradeRoundingModeEnumRector\DowngradeRoundingModeEnumRectorTest | ||
| */ | ||
| final class DowngradeRoundingModeEnumRector extends AbstractRector | ||
| { | ||
| public function getNodeTypes(): array | ||
| { | ||
| return [Node\Expr\FuncCall::class]; | ||
| } | ||
|
|
||
| public function getRuleDefinition(): RuleDefinition | ||
| { | ||
| return new RuleDefinition( | ||
| 'Replace RoundingMode enum to rounding mode constant in round()', | ||
| [ | ||
| new CodeSample( | ||
| <<<'CODE_SAMPLE' | ||
| round(1.5, 0, RoundingMode::HalfAwayFromZero); | ||
| CODE_SAMPLE | ||
| , | ||
| <<<'CODE_SAMPLE' | ||
| round(1.5, 0, PHP_ROUND_HALF_UP); | ||
| CODE_SAMPLE | ||
| ), | ||
| ] | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @param MethodCall $node | ||
| */ | ||
| public function refactor(Node $node): ?Node | ||
| { | ||
| if (!$this->isName($node, 'round')) { | ||
| return null; | ||
| } | ||
|
|
||
| if ($node->isFirstClassCallable()) { | ||
| return null; | ||
| } | ||
|
|
||
| $args = $node->getArgs(); | ||
|
|
||
| if (count($args) !== 3) { | ||
| return null; | ||
| } | ||
|
|
||
| if (!isset($args[2])) { | ||
| return null; | ||
| } | ||
|
|
||
| $modeArg = $args[2]->value; | ||
| $hasChanged = false; | ||
| if ($modeArg instanceof ClassConstFetch) { | ||
jorgsowa marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
jorgsowa marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if ($modeArg->class->name === 'RoundingMode') { | ||
| $constantName = match ($modeArg->name->name) { | ||
| 'HalfAwayFromZero' => 'PHP_ROUND_HALF_UP', | ||
jorgsowa marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 'HalfTowardsZero' => 'PHP_ROUND_HALF_DOWN', | ||
| 'HalfEven' => 'PHP_ROUND_HALF_EVEN', | ||
| 'HalfOdd' => 'PHP_ROUND_HALF_ODD', | ||
| default => null, | ||
| }; | ||
|
|
||
| if ($constantName === null) { | ||
| return null; | ||
| } | ||
|
|
||
| $args[2]->value = new Node\Expr\ConstFetch(new Node\Name($constantName)); | ||
jorgsowa marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
jorgsowa marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| $hasChanged = true; | ||
| } | ||
| } | ||
|
|
||
| if ($hasChanged) { | ||
| return $node; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.