Skip to content

Commit 3050036

Browse files
arshidkv12Arshid
authored andcommitted
[Php85] Add ChrArgModuloRector (#7190)
* Wrap chr() argument with % 256 to avoid deprecated out-of-range integers * Wrap chr() argument with % 256 to avoid deprecated out-of-range integers * Wrap chr() argument with % 256 to avoid deprecated out-of-range integers * Wrap chr() argument with % 256 to avoid deprecated out-of-range integers * Wrap chr() argument with % 256 to avoid deprecated out-of-range integers * Wrap chr() argument with % 256 to avoid deprecated out-of-range integers * Wrap chr() argument with % 256 to avoid deprecated out-of-range integers * Wrap chr() argument with % 256 to avoid deprecated out-of-range integers * Wrap chr() argument with % 256 to avoid deprecated out-of-range integers * Wrap chr() argument with % 256 to avoid deprecated out-of-range integers * Wrap chr() argument with % 256 to avoid deprecated out-of-range integers --------- Co-authored-by: Arshid <arshid@Arshids-MacBook-Air.local>
1 parent ae13ed0 commit 3050036

8 files changed

Lines changed: 81 additions & 6 deletions

File tree

config/set/php85.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Rector\Php85\Rector\Const_\DeprecatedAnnotationToDeprecatedAttributeRector;
1313
use Rector\Php85\Rector\FuncCall\ArrayKeyExistsNullToEmptyStringRector;
1414
use Rector\Php85\Rector\FuncCall\OrdSingleByteRector;
15+
use Rector\Php85\Rector\FuncCall\ChrArgModuloRector;
1516
use Rector\Php85\Rector\FuncCall\RemoveFinfoBufferContextArgRector;
1617
use Rector\Php85\Rector\Switch_\ColonAfterSwitchCaseRector;
1718
use Rector\Removing\Rector\FuncCall\RemoveFuncCallArgRector;
@@ -36,7 +37,6 @@
3637
DeprecatedAnnotationToDeprecatedAttributeRector::class,
3738
ColonAfterSwitchCaseRector::class,
3839
ArrayKeyExistsNullToEmptyStringRector::class,
39-
OrdSingleByteRector::class,
4040
]
4141
);
4242

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\Php85\Rector\FuncCall\ChrArgModuloRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class ChrArgModuloRectorTest extends AbstractRectorTestCase
12+
{
13+
#[DataProvider('provideData')]
14+
public function test(string $filePath): void
15+
{
16+
$this->doTestFile($filePath);
17+
}
18+
19+
public static function provideData(): Iterator
20+
{
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
22+
}
23+
24+
public function provideConfigFilePath(): string
25+
{
26+
return __DIR__ . '/config/configured_rule.php';
27+
}
28+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
namespace Rector\Tests\Php85\Rector\FuncCall\ChrArgModuloRector\Fixture;
3+
4+
echo chr(300);
5+
?>
6+
-----
7+
<?php
8+
namespace Rector\Tests\Php85\Rector\FuncCall\ChrArgModuloRector\Fixture;
9+
10+
echo chr(300 % 256);
11+
?>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
namespace Rector\Tests\Php85\Rector\FuncCall\ChrArgModuloRector\Fixture;
3+
4+
$n = 400;
5+
echo chr($n);
6+
?>
7+
-----
8+
<?php
9+
namespace Rector\Tests\Php85\Rector\FuncCall\ChrArgModuloRector\Fixture;
10+
11+
$n = 400;
12+
echo chr($n % 256);
13+
?>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
namespace Rector\Tests\Php85\Rector\FuncCall\ChrArgModuloRector\Fixture;
3+
4+
echo chr(44);
5+
?>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\Php85\Rector\FuncCall\ChrArgModuloRector;
7+
use Rector\ValueObject\PhpVersion;
8+
9+
return static function (RectorConfig $rectorConfig): void {
10+
$rectorConfig->rule(ChrArgModuloRector::class);
11+
12+
$rectorConfig->phpVersion(PhpVersion::PHP_85);
13+
};

rules/Php85/Rector/FuncCall/ChrArgModuloRector.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use PhpParser\Node;
88
use PhpParser\Node\Expr\BinaryOp\Mod;
9-
use PhpParser\Node\Expr\Cast\Int_;
109
use PhpParser\Node\Expr\FuncCall;
1110
use PhpParser\Node\Scalar\LNumber;
1211
use Rector\PhpParser\Node\Value\ValueResolver;
@@ -81,11 +80,11 @@ public function refactor(Node $node): ?Node
8180
return null;
8281
}
8382

84-
$modValue = $value % 256;
85-
86-
$modValue = new \PhpParser\Node\Scalar\Int_($modValue);
83+
if ( $value >= 0 && $value <= 255) {
84+
return null;
85+
}
8786

88-
$args[0]->value = $modValue;
87+
$args[0]->value = new Mod($argExpr, new LNumber(256));
8988
$node->args = $args;
9089

9190
return $node;

src/ValueObject/PhpVersionFeature.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -810,4 +810,10 @@ final class PhpVersionFeature
810810
* @var int
811811
*/
812812
public const DEPRECATE_ORD_WITH_MULTIBYTE_STRING = PhpVersion::PHP_85;
813+
814+
/**
815+
* @see https://wiki.php.net/rfc/deprecations_php_8_5#eprecate_passing_integers_outside_the_interval_0_255_to_chr
816+
* @var int
817+
*/
818+
public const DEPRECATE_OUTSIDE_INTERVEL_VAL_IN_CHR_FUNCTION = PhpVersion::PHP_85;
813819
}

0 commit comments

Comments
 (0)