-
-
Notifications
You must be signed in to change notification settings - Fork 431
Php8.5 OrdSingleByteRector #7219
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3f3de0c
OrdSingleByteRector
8cc7f07
OrdSingleByteRector
89eb0b4
OrdSingleByteRector
3c43485
OrdSingleByteRector
795f52e
OrdSingleByteRector
ba1f3a3
OrdSingleByteRector
d40bfc0
OrdSingleByteRector
bebd0f0
OrdSingleByteRector
f653cbf
OrdSingleByteRector
9eeff42
OrdSingleByteRector
3384157
Update rules/Php85/Rector/FuncCall/OrdSingleByteRector.php
arshidkv12 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
rules-tests/Php85/Rector/FuncCall/OrdSingleByteRector/Fixture/multibyte_int.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| ?> | ||
11 changes: 11 additions & 0 deletions
11
rules-tests/Php85/Rector/FuncCall/OrdSingleByteRector/Fixture/multibyte_str.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
samsonasik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ?> | ||
| ----- | ||
| <?php | ||
| namespace Rector\Tests\Php85\Rector\FuncCall\OrdSingleByteRector\Fixture; | ||
| $a = 'abc'; | ||
| echo ord($a[0]); | ||
| ?> | ||
5 changes: 5 additions & 0 deletions
5
rules-tests/Php85/Rector/FuncCall/OrdSingleByteRector/Fixture/skip_bool.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| ?> |
5 changes: 5 additions & 0 deletions
5
rules-tests/Php85/Rector/FuncCall/OrdSingleByteRector/Fixture/skip_multibyte_int_var.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| ?> |
28 changes: 28 additions & 0 deletions
28
rules-tests/Php85/Rector/FuncCall/OrdSingleByteRector/OrdSingleByteRectorTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'; | ||
| } | ||
| } |
13 changes: 13 additions & 0 deletions
13
rules-tests/Php85/Rector/FuncCall/OrdSingleByteRector/config/configured_rule.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.