diff --git a/config/set/php85.php b/config/set/php85.php index 074c18b2af9..be4cad3a560 100644 --- a/config/set/php85.php +++ b/config/set/php85.php @@ -11,6 +11,7 @@ use Rector\Php85\Rector\ClassMethod\NullDebugInfoReturnRector; use Rector\Php85\Rector\Const_\DeprecatedAnnotationToDeprecatedAttributeRector; use Rector\Php85\Rector\FuncCall\ArrayKeyExistsNullToEmptyStringRector; +use Rector\Php85\Rector\FuncCall\ChrArgModuloRector; use Rector\Php85\Rector\FuncCall\RemoveFinfoBufferContextArgRector; use Rector\Php85\Rector\Switch_\ColonAfterSwitchCaseRector; use Rector\Removing\Rector\FuncCall\RemoveFuncCallArgRector; @@ -34,6 +35,7 @@ DeprecatedAnnotationToDeprecatedAttributeRector::class, ColonAfterSwitchCaseRector::class, ArrayKeyExistsNullToEmptyStringRector::class, + ChrArgModuloRector::class, ] ); diff --git a/rules-tests/Php85/Rector/FuncCall/ChrArgModuloRector/ChrArgModuloRectorTest.php b/rules-tests/Php85/Rector/FuncCall/ChrArgModuloRector/ChrArgModuloRectorTest.php new file mode 100644 index 00000000000..093a907ccf2 --- /dev/null +++ b/rules-tests/Php85/Rector/FuncCall/ChrArgModuloRector/ChrArgModuloRectorTest.php @@ -0,0 +1,28 @@ +doTestFile($filePath); + } + + public static function provideData(): Iterator + { + return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/configured_rule.php'; + } +} diff --git a/rules-tests/Php85/Rector/FuncCall/ChrArgModuloRector/Fixture/chr.php.inc b/rules-tests/Php85/Rector/FuncCall/ChrArgModuloRector/Fixture/chr.php.inc new file mode 100644 index 00000000000..320ac129c3a --- /dev/null +++ b/rules-tests/Php85/Rector/FuncCall/ChrArgModuloRector/Fixture/chr.php.inc @@ -0,0 +1,11 @@ + +----- + \ No newline at end of file diff --git a/rules-tests/Php85/Rector/FuncCall/ChrArgModuloRector/Fixture/chr_var.php.inc b/rules-tests/Php85/Rector/FuncCall/ChrArgModuloRector/Fixture/chr_var.php.inc new file mode 100644 index 00000000000..2e9bae1ede9 --- /dev/null +++ b/rules-tests/Php85/Rector/FuncCall/ChrArgModuloRector/Fixture/chr_var.php.inc @@ -0,0 +1,13 @@ + +----- + \ No newline at end of file diff --git a/rules-tests/Php85/Rector/FuncCall/ChrArgModuloRector/Fixture/skip_chr_inbond.php.inc b/rules-tests/Php85/Rector/FuncCall/ChrArgModuloRector/Fixture/skip_chr_inbond.php.inc new file mode 100644 index 00000000000..c303cdffff7 --- /dev/null +++ b/rules-tests/Php85/Rector/FuncCall/ChrArgModuloRector/Fixture/skip_chr_inbond.php.inc @@ -0,0 +1,5 @@ + \ No newline at end of file diff --git a/rules-tests/Php85/Rector/FuncCall/ChrArgModuloRector/config/configured_rule.php b/rules-tests/Php85/Rector/FuncCall/ChrArgModuloRector/config/configured_rule.php new file mode 100644 index 00000000000..15d2a0b412f --- /dev/null +++ b/rules-tests/Php85/Rector/FuncCall/ChrArgModuloRector/config/configured_rule.php @@ -0,0 +1,13 @@ +rule(ChrArgModuloRector::class); + + $rectorConfig->phpVersion(PhpVersion::PHP_85); +}; diff --git a/rules/Php85/Rector/FuncCall/ChrArgModuloRector.php b/rules/Php85/Rector/FuncCall/ChrArgModuloRector.php new file mode 100644 index 00000000000..8a36f3085c7 --- /dev/null +++ b/rules/Php85/Rector/FuncCall/ChrArgModuloRector.php @@ -0,0 +1,97 @@ +isFirstClassCallable()) { + return null; + } + + if (! $this->isName($node, 'chr')) { + return null; + } + + $args = $node->getArgs(); + + if (! isset($node->args[0])) { + return null; + } + + $argExpr = $args[0]->value; + + if ($argExpr instanceof Mod) { + return null; + } + + $value = $this->valueResolver->getValue($argExpr); + if (! is_int($value)) { + return null; + } + + if ( $value >= 0 && $value <= 255) { + return null; + } + + $args[0]->value = new Mod($argExpr, new LNumber(256)); + $node->args = $args; + + return $node; + } + + public function provideMinPhpVersion(): int + { + return PhpVersionFeature::DEPRECATE_OUTSIDE_INTERVEL_VAL_IN_CHR_FUNCTION; + } +} diff --git a/src/ValueObject/PhpVersionFeature.php b/src/ValueObject/PhpVersionFeature.php index 1c72c63d39b..b76487f4a62 100644 --- a/src/ValueObject/PhpVersionFeature.php +++ b/src/ValueObject/PhpVersionFeature.php @@ -798,4 +798,10 @@ final class PhpVersionFeature * @var int */ public const DEPRECATE_NULL_ARG_IN_ARRAY_KEY_EXISTS_FUNCTION = PhpVersion::PHP_85; + + /** + * @see https://wiki.php.net/rfc/deprecations_php_8_5#eprecate_passing_integers_outside_the_interval_0_255_to_chr + * @var int + */ + public const DEPRECATE_OUTSIDE_INTERVEL_VAL_IN_CHR_FUNCTION = PhpVersion::PHP_85; }