Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/set/php85.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -34,6 +35,7 @@
DeprecatedAnnotationToDeprecatedAttributeRector::class,
ColonAfterSwitchCaseRector::class,
ArrayKeyExistsNullToEmptyStringRector::class,
ChrArgModuloRector::class,
]
);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Php85\Rector\FuncCall\ChrArgModuloRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class ChrArgModuloRectorTest 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';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
namespace Rector\Tests\Php85\Rector\FuncCall\ChrArgModuloRector\Fixture;

echo chr(300);
?>
-----
<?php
namespace Rector\Tests\Php85\Rector\FuncCall\ChrArgModuloRector\Fixture;

echo chr(300 % 256);
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
namespace Rector\Tests\Php85\Rector\FuncCall\ChrArgModuloRector\Fixture;

$n = 400;
echo chr($n);
?>
-----
<?php
namespace Rector\Tests\Php85\Rector\FuncCall\ChrArgModuloRector\Fixture;

$n = 400;
echo chr($n % 256);
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
namespace Rector\Tests\Php85\Rector\FuncCall\ChrArgModuloRector\Fixture;

echo chr(44);
?>
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\Php85\Rector\FuncCall\ChrArgModuloRector;
use Rector\ValueObject\PhpVersion;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(ChrArgModuloRector::class);

$rectorConfig->phpVersion(PhpVersion::PHP_85);
};
97 changes: 97 additions & 0 deletions rules/Php85/Rector/FuncCall/ChrArgModuloRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

declare(strict_types=1);

namespace Rector\Php85\Rector\FuncCall;

use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp\Mod;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Scalar\LNumber;
use Rector\PhpParser\Node\Value\ValueResolver;
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 https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_passing_integers_outside_the_interval_0_255_to_chr
* @see \Rector\Tests\Php85\Rector\FuncCall\ChrArgModuloRector\ChrArgModuloRectorTest
*/
final class ChrArgModuloRector extends AbstractRector implements MinPhpVersionInterface
{
public function __construct(
private readonly ValueResolver $valueResolver
){

}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Wrap chr() argument with % 256 to avoid deprecated out-of-range integers',
[
new CodeSample(
<<<'CODE_SAMPLE'
echo chr(300);
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
echo chr(300 % 256);
CODE_SAMPLE
),
]
);
}

public function getNodeTypes(): array
{
return [FuncCall::class];
}

/**
* @param FuncCall $node
*/
public function refactor(Node $node): ?Node
{
if ($node->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;
}
}
6 changes: 6 additions & 0 deletions src/ValueObject/PhpVersionFeature.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Loading