-
-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Php84] Add rule for RoundingMode enum (#6369)
* Add new rule for rounding modes in PHP 8.4 * Added namespace to test * Improve tests * Fixed namespace * Skip first class callable * Used FullyQualified instead of name * Added RoundingModeEnumRector to php84.php * Removed constructor * Return null for no change * cs * cs --------- Co-authored-by: Abdul Malik Ikhsan <[email protected]>
- Loading branch information
1 parent
6aca457
commit a72a021
Showing
7 changed files
with
187 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
29 changes: 29 additions & 0 deletions
29
rules-tests/Php84/Rector/FuncCall/RoundingModeEnumRector/Fixture/rounding_constants.php.inc
This file contains 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,29 @@ | ||
<?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, '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, \RoundingMode::HalfAwayFromZero); | ||
round(1.5, 0, \RoundingMode::HalfTowardsZero); | ||
round(1.5, 0, \RoundingMode::HalfEven); | ||
round(1.5, 0, \RoundingMode::HalfOdd); | ||
round(1.5, 0, 'invalid'); | ||
round(1.5, 0, PHP_INT_MAX); | ||
|
||
?> |
10 changes: 10 additions & 0 deletions
10
...p84/Rector/FuncCall/RoundingModeEnumRector/Fixture/skip_indirect_use_of_constants.php.inc
This file contains 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 = PHP_ROUND_HALF_DOWN; | ||
|
||
round(1.5, 0, $mode); | ||
|
||
?> |
28 changes: 28 additions & 0 deletions
28
rules-tests/Php84/Rector/FuncCall/RoundingModeEnumRector/RoundingModeEnumRectorTest.php
This file contains 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\Php84\Rector\FuncCall\RoundingModeEnumRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class RoundingModeEnumRectorTest 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'; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
rules-tests/Php84/Rector/FuncCall/RoundingModeEnumRector/config/configured_rule.php
This file contains 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,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use Rector\Php84\Rector\FuncCall\RoundingModeEnumRector; | ||
use Rector\ValueObject\PhpVersion; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(RoundingModeEnumRector::class); | ||
|
||
$rectorConfig->phpVersion(PhpVersion::PHP_84); | ||
}; |
This file contains 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,99 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Php84\Rector\FuncCall; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\ClassConstFetch; | ||
use PhpParser\Node\Expr\ConstFetch; | ||
use PhpParser\Node\Expr\FuncCall; | ||
use PhpParser\Node\Name\FullyQualified; | ||
use Rector\Rector\AbstractRector; | ||
use Rector\ValueObject\PhpVersionFeature; | ||
use Rector\VersionBonding\Contract\MinPhpVersionInterface; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \Rector\Tests\Php84\Rector\FuncCall\RoundingModeEnumRector\RoundingModeEnumRectorTest | ||
*/ | ||
final class RoundingModeEnumRector extends AbstractRector implements MinPhpVersionInterface | ||
{ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Replace rounding mode constant to RoundMode enum in round()', [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
round(1.5, 0, PHP_ROUND_HALF_UP); | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
round(1.5, 0, RoundingMode::HalfAwayFromZero); | ||
CODE_SAMPLE | ||
, | ||
), | ||
]); | ||
} | ||
|
||
public function getNodeTypes(): array | ||
{ | ||
return [FuncCall::class]; | ||
} | ||
|
||
/** | ||
* @param FuncCall $node | ||
*/ | ||
public function refactor(Node $node): ?FuncCall | ||
{ | ||
|
||
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 ConstFetch) { | ||
$enumCase = match ($modeArg->name->toString()) { | ||
'PHP_ROUND_HALF_UP' => 'HalfAwayFromZero', | ||
'PHP_ROUND_HALF_DOWN' => 'HalfTowardsZero', | ||
'PHP_ROUND_HALF_EVEN' => 'HalfEven', | ||
'PHP_ROUND_HALF_ODD' => 'HalfOdd', | ||
default => null, | ||
}; | ||
|
||
if ($enumCase === null) { | ||
return null; | ||
} | ||
|
||
$args[2]->value = new ClassConstFetch(new FullyQualified('RoundingMode'), $enumCase); | ||
$hasChanged = true; | ||
} | ||
|
||
if ($hasChanged) { | ||
return $node; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public function provideMinPhpVersion(): int | ||
{ | ||
return PhpVersionFeature::ROUNDING_MODES; | ||
} | ||
} |
This file contains 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