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 @@ -12,6 +12,7 @@
use Rector\Php85\Rector\Const_\DeprecatedAnnotationToDeprecatedAttributeRector;
use Rector\Php85\Rector\FuncCall\ArrayKeyExistsNullToEmptyStringRector;
use Rector\Php85\Rector\FuncCall\ChrArgModuloRector;
use Rector\Php85\Rector\FuncCall\OrdSingleByteRector;
use Rector\Php85\Rector\FuncCall\RemoveFinfoBufferContextArgRector;
use Rector\Php85\Rector\Switch_\ColonAfterSwitchCaseRector;
use Rector\Removing\Rector\FuncCall\RemoveFuncCallArgRector;
Expand All @@ -37,6 +38,7 @@
ColonAfterSwitchCaseRector::class,
ArrayKeyExistsNullToEmptyStringRector::class,
ChrArgModuloRector::class,
OrdSingleByteRector::class,
]
);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
namespace Rector\Tests\Php85\Rector\FuncCall\OrdSingleByteRector\Fixture;
echo ord(123);
?>
-----
<?php
namespace Rector\Tests\Php85\Rector\FuncCall\OrdSingleByteRector\Fixture;
echo ord(1);
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
namespace Rector\Tests\Php85\Rector\FuncCall\OrdSingleByteRector\Fixture;
$a = 'abc';
echo ord($a);
?>
-----
<?php
namespace Rector\Tests\Php85\Rector\FuncCall\OrdSingleByteRector\Fixture;
$a = 'abc';
echo ord($a[0]);
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
namespace Rector\Tests\Php85\Rector\FuncCall\OrdSingleByteRector\Fixture;
$a = true;
echo ord($a);
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
namespace Rector\Tests\Php85\Rector\FuncCall\OrdSingleByteRector\Fixture;
$i = 123;
echo ord($i);
?>
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\OrdSingleByteRector;

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

final class OrdSingleByteRectorTest 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,13 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Php85\Rector\FuncCall\OrdSingleByteRector;
use Rector\ValueObject\PhpVersion;

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

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

declare(strict_types=1);

namespace Rector\Php85\Rector\FuncCall;

use PhpParser\Node;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Scalar\Int_;
use PhpParser\Node\Scalar\String_;
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_string_which_are_not_one_byte_long_to_ord
* @see \Rector\Tests\Php85\Rector\FuncCall\OrdSingleByteRector\OrdSingleByteRectorTest
*/
final class OrdSingleByteRector extends AbstractRector implements MinPhpVersionInterface
{
public function __construct(
private readonly ValueResolver $valueResolver
) {

}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Replace ord($str) with ord($str[0])',
[
new CodeSample(
<<<'CODE_SAMPLE'
echo ord('abc');
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
echo ord('a');
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, 'ord')) {
return null;
}

$args = $node->getArgs();

if (! isset($node->args[0])) {
return null;
}

$argExpr = $args[0]->value;
$type = $this->nodeTypeResolver->getNativeType($argExpr);

if (! $type->isString()->yes() && ! $type->isInteger()->yes()) {
return null;
}

$value = $this->valueResolver->getValue($argExpr);
$isInt = is_int($value);

if (! $argExpr instanceof Int_) {

if ($isInt) {
return null;
}

$args[0]->value = new ArrayDimFetch($argExpr, new Int_(0));
$node->args = $args;

return $node;
}

$value = (string) $value;
$byte = $value[0] ?? '';

$byteValue = $isInt ? new Int_((int) $byte) : new String_($byte);

$args[0]->value = $byteValue;
$node->args = $args;

return $node;
}

public function provideMinPhpVersion(): int
{
return PhpVersionFeature::DEPRECATE_ORD_WITH_MULTIBYTE_STRING;
}
}
6 changes: 6 additions & 0 deletions src/ValueObject/PhpVersionFeature.php
Original file line number Diff line number Diff line change
Expand Up @@ -810,4 +810,10 @@ final class PhpVersionFeature
* @var int
*/
public const DEPRECATE_OUTSIDE_INTERVEL_VAL_IN_CHR_FUNCTION = PhpVersion::PHP_85;

/**
* @see https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_passing_string_which_are_not_one_byte_long_to_ord
* @var int
*/
public const DEPRECATE_ORD_WITH_MULTIBYTE_STRING = PhpVersion::PHP_85;
}
Loading