From 9474f8a689d0a7b7a03245fdd389abf58d8a434e Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 17 Nov 2026 10:07:13 +0700 Subject: [PATCH] Fix negative value on ChrArgModuloRector --- .../Fixture/chr_negative.php.inc | 15 +++++++++++++++ .../Php85/Rector/FuncCall/ChrArgModuloRector.php | 10 ++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 rules-tests/Php85/Rector/FuncCall/ChrArgModuloRector/Fixture/chr_negative.php.inc diff --git a/rules-tests/Php85/Rector/FuncCall/ChrArgModuloRector/Fixture/chr_negative.php.inc b/rules-tests/Php85/Rector/FuncCall/ChrArgModuloRector/Fixture/chr_negative.php.inc new file mode 100644 index 00000000000..4c50d7b804b --- /dev/null +++ b/rules-tests/Php85/Rector/FuncCall/ChrArgModuloRector/Fixture/chr_negative.php.inc @@ -0,0 +1,15 @@ + +----- + diff --git a/rules/Php85/Rector/FuncCall/ChrArgModuloRector.php b/rules/Php85/Rector/FuncCall/ChrArgModuloRector.php index 7f714c81231..90ddc406d1e 100644 --- a/rules/Php85/Rector/FuncCall/ChrArgModuloRector.php +++ b/rules/Php85/Rector/FuncCall/ChrArgModuloRector.php @@ -6,8 +6,9 @@ use PhpParser\Node; use PhpParser\Node\Expr\BinaryOp\Mod; +use PhpParser\Node\Expr\BinaryOp\Plus; use PhpParser\Node\Expr\FuncCall; -use PhpParser\Node\Scalar\LNumber; +use PhpParser\Node\Scalar\Int_; use Rector\PhpParser\Node\Value\ValueResolver; use Rector\Rector\AbstractRector; use Rector\RuleDoc\CodeSample\CodeSample; @@ -84,7 +85,12 @@ public function refactor(Node $node): ?Node return null; } - $args[0]->value = new Mod($argExpr, new LNumber(256)); + $mod = new Mod($argExpr, new Int_(256)); + + // modulo keeps the sign of the dividend, so a negative value is still out of range + $args[0]->value = $value % 256 < 0 + ? new Plus($mod, new Int_(256)) + : $mod; $node->args = $args; return $node;