-
-
Notifications
You must be signed in to change notification settings - Fork 422
[Php85] Add ArrayKeyExistsNullToEmptyStringRector #7183
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
TomasVotruba
merged 14 commits into
rectorphp:main
from
arshidkv12:8.5_ArrayKey…StringRector
Aug 28, 2025
The head ref may contain hidden characters: "8.5_ArrayKey\u2026StringRector"
Merged
Changes from 12 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
0ce0609
Add ArrayKeyExistsNullToEmptyStringRector to fix deprecated null key
3e180f6
Add ArrayKeyExistsNullToEmptyStringRector to fix deprecated null key
181cd01
Add ArrayKeyExistsNullToEmptyStringRector to fix deprecated null key
803bf0f
Add ArrayKeyExistsNullToEmptyStringRector to fix deprecated null key
d169a04
Add ArrayKeyExistsNullToEmptyStringRector to fix deprecated null key
623e191
Add ArrayKeyExistsNullToEmptyStringRector to fix deprecated null key
614a883
Add ArrayKeyExistsNullToEmptyStringRector to fix deprecated null key
d4f4d2b
Add ArrayKeyExistsNullToEmptyStringRector to fix deprecated null key
9a7ccf8
Add ArrayKeyExistsNullToEmptyStringRector to fix deprecated null key
e2b68e2
Add ArrayKeyExistsNullToEmptyStringRector to fix deprecated null key
e5ea7e1
Add ArrayKeyExistsNullToEmptyStringRector to fix deprecated null key
9602db8
Add ArrayKeyExistsNullToEmptyStringRector to fix deprecated null key
f6e2dfb
Add ArrayKeyExistsNullToEmptyStringRector to fix deprecated null key
696a334
Add ArrayKeyExistsNullToEmptyStringRector to fix deprecated null key
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
28 changes: 28 additions & 0 deletions
28
...cCall/ArrayKeyExistsNullToEmptyStringRector/ArrayKeyExistsNullToEmptyStringRectorTest.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\ArrayKeyExistsNullToEmptyStringRector; | ||
|
|
||
| use Iterator; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
|
||
| final class ArrayKeyExistsNullToEmptyStringRectorTest 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'; | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
...ests/Php85/Rector/FuncCall/ArrayKeyExistsNullToEmptyStringRector/Fixture/key_null.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 | ||
|
|
||
| array_key_exists(null, $array); | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| array_key_exists('', $array); | ||
|
|
||
| ?> |
11 changes: 11 additions & 0 deletions
11
.../Php85/Rector/FuncCall/ArrayKeyExistsNullToEmptyStringRector/Fixture/key_null_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,11 @@ | ||
| <?php | ||
|
|
||
| array_key_exists($k, $array); | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| array_key_exists((string) $k, $array); | ||
|
|
||
| ?> |
13 changes: 13 additions & 0 deletions
13
...ts/Php85/Rector/FuncCall/ArrayKeyExistsNullToEmptyStringRector/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\ArrayKeyExistsNullToEmptyStringRector; | ||
| use Rector\ValueObject\PhpVersion; | ||
|
|
||
| return static function (RectorConfig $rectorConfig): void { | ||
| $rectorConfig->rule(ArrayKeyExistsNullToEmptyStringRector::class); | ||
|
|
||
| $rectorConfig->phpVersion(PhpVersion::PHP_85); | ||
| }; |
183 changes: 183 additions & 0 deletions
183
rules/Php81/NodeManipulator/NullToStrictStringConverter.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,183 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\Php81\NodeManipulator; | ||
|
|
||
| use PhpParser\Node\Arg; | ||
| use PhpParser\Node\Expr; | ||
| use PhpParser\Node\Expr\Cast\String_ as CastString_; | ||
| use PhpParser\Node\Expr\FuncCall; | ||
| use PhpParser\Node\Expr\MethodCall; | ||
| use PhpParser\Node\Expr\Ternary; | ||
| use PhpParser\Node\Scalar\InterpolatedString; | ||
| use PhpParser\Node\Scalar\String_; | ||
| use PHPStan\Analyser\Scope; | ||
| use PHPStan\Reflection\Native\ExtendedNativeParameterReflection; | ||
| use PHPStan\Reflection\ParametersAcceptor; | ||
| use PHPStan\Type\ErrorType; | ||
| use PHPStan\Type\MixedType; | ||
| use PHPStan\Type\NullType; | ||
| use PHPStan\Type\Type; | ||
| use PHPStan\Type\UnionType; | ||
| use Rector\NodeAnalyzer\PropertyFetchAnalyzer; | ||
| use Rector\NodeTypeResolver\NodeTypeResolver; | ||
| use Rector\PhpParser\Node\Value\ValueResolver; | ||
|
|
||
| final readonly class NullToStrictStringConverter | ||
| { | ||
| public function __construct( | ||
| private ValueResolver $valueResolver, | ||
| private NodeTypeResolver $nodeTypeResolver, | ||
| private PropertyFetchAnalyzer $propertyFetchAnalyzer, | ||
| ) { | ||
| } | ||
|
|
||
| /** | ||
| * @param Arg[] $args | ||
| */ | ||
| public function convertIfNull( | ||
| FuncCall $funcCall, | ||
| array $args, | ||
| int $position, | ||
| bool $isTrait, | ||
| Scope $scope, | ||
| ParametersAcceptor $parametersAcceptor | ||
| ): ?FuncCall { | ||
| if (! isset($args[$position])) { | ||
| return null; | ||
| } | ||
|
|
||
| $argValue = $args[$position]->value; | ||
| if ($this->valueResolver->isNull($argValue)) { | ||
| $args[$position]->value = new String_(''); | ||
| $funcCall->args = $args; | ||
| return $funcCall; | ||
| } | ||
|
|
||
| if ($this->shouldSkipValue($argValue, $scope, $isTrait)) { | ||
| return null; | ||
| } | ||
|
|
||
| $parameter = $parametersAcceptor->getParameters()[$position] ?? null; | ||
| if ($parameter instanceof ExtendedNativeParameterReflection && $parameter->getType() instanceof UnionType) { | ||
| $parameterType = $parameter->getType(); | ||
| if (! $this->isValidUnionType($parameterType)) { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| if ($argValue instanceof Ternary && ! $this->shouldSkipValue($argValue->else, $scope, $isTrait)) { | ||
| if ($this->valueResolver->isNull($argValue->else)) { | ||
| $argValue->else = new String_(''); | ||
| } else { | ||
| $argValue->else = new CastString_($argValue->else); | ||
| } | ||
|
|
||
| $args[$position]->value = $argValue; | ||
| $funcCall->args = $args; | ||
| return $funcCall; | ||
| } | ||
|
|
||
| $args[$position]->value = new CastString_($argValue); | ||
| $funcCall->args = $args; | ||
| return $funcCall; | ||
| } | ||
|
|
||
| private function shouldSkipValue(Expr $expr, Scope $scope, bool $isTrait): bool | ||
| { | ||
| $type = $this->nodeTypeResolver->getType($expr); | ||
| if ($type->isString()->yes()) { | ||
| return true; | ||
| } | ||
|
|
||
| $nativeType = $this->nodeTypeResolver->getNativeType($expr); | ||
| if ($nativeType->isString()->yes()) { | ||
| return true; | ||
| } | ||
|
|
||
| if ($this->shouldSkipType($type)) { | ||
| return true; | ||
| } | ||
|
|
||
| if ($expr instanceof InterpolatedString) { | ||
| return true; | ||
| } | ||
|
|
||
| if ($this->isAnErrorType($expr, $nativeType, $scope)) { | ||
| return true; | ||
| } | ||
|
|
||
| return $this->shouldSkipTrait($expr, $type, $isTrait); | ||
| } | ||
|
|
||
| private function isValidUnionType(Type $type): bool | ||
| { | ||
| if (! $type instanceof UnionType) { | ||
| return false; | ||
| } | ||
|
|
||
| foreach ($type->getTypes() as $childType) { | ||
| if ($childType->isString()->yes()) { | ||
| continue; | ||
| } | ||
|
|
||
| if ($childType->isInteger()->yes()) { | ||
| continue; | ||
| } | ||
|
|
||
| if ($childType->isNull()->yes()) { | ||
| continue; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| private function shouldSkipType(Type $type): bool | ||
| { | ||
| return ! $type instanceof MixedType | ||
| && ! $type->isNull() | ||
| ->yes() | ||
| && ! $this->isValidUnionType($type); | ||
| } | ||
|
|
||
| private function shouldSkipTrait(Expr $expr, Type $type, bool $isTrait): bool | ||
| { | ||
| if (! $type instanceof MixedType) { | ||
| return false; | ||
| } | ||
|
|
||
| if (! $isTrait) { | ||
| return false; | ||
| } | ||
|
|
||
| if ($type->isExplicitMixed()) { | ||
| return false; | ||
| } | ||
|
|
||
| if (! $expr instanceof MethodCall) { | ||
| return $this->propertyFetchAnalyzer->isLocalPropertyFetch($expr); | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| private function isAnErrorType(Expr $expr, Type $type, Scope $scope): bool | ||
| { | ||
| if ($type instanceof ErrorType) { | ||
| return true; | ||
| } | ||
|
|
||
| $parentScope = $scope->getParentScope(); | ||
| if ($parentScope instanceof Scope) { | ||
| return $parentScope->getType($expr) instanceof ErrorType; | ||
| } | ||
|
|
||
| return $type instanceof MixedType | ||
| && ! $type->isExplicitMixed() | ||
| && $type->getSubtractedType() instanceof NullType; | ||
| } | ||
| } | ||
Oops, something went wrong.
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.