-
-
Notifications
You must be signed in to change notification settings - Fork 430
[dead-code] Skip substr casting removal on PHP 7.x, as can return false|string #7449
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\DeadCode\Rector\Cast\RecastingRemovalRector\FixturePhp74; | ||
|
|
||
| final class SkipSubstr | ||
| { | ||
| public function run(string $value): string | ||
| { | ||
| return (string) substr($value, 10, 10); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace DeadCode\Rector\Cast\RecastingRemovalRector; | ||
|
|
||
| use Iterator; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
|
||
| final class RecastingRemovalRectorPhp74Test extends AbstractRectorTestCase | ||
| { | ||
| #[DataProvider('provideData')] | ||
| public function test(string $filePath): void | ||
| { | ||
| $this->doTestFile($filePath); | ||
| } | ||
|
|
||
| public static function provideData(): Iterator | ||
| { | ||
| return self::yieldFilesFromDirectory(__DIR__ . '/FixturePhp74'); | ||
| } | ||
|
|
||
| public function provideConfigFilePath(): string | ||
| { | ||
| return __DIR__ . '/config/configured_rule_php74.php'; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use Rector\Config\RectorConfig; | ||
| use Rector\DeadCode\Rector\Cast\RecastingRemovalRector; | ||
| use Rector\ValueObject\PhpVersion; | ||
|
|
||
| return RectorConfig::configure() | ||
| ->withRules([RecastingRemovalRector::class]) | ||
| ->withPhpVersion(PhpVersion::PHP_74); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector\FixtureTrueInUnion; | ||
|
|
||
| /** | ||
| * true|othertype work on >= php 8.2, ref https://3v4l.org/UJqXT | ||
| */ | ||
| final class TrueInUnionStrToUpper | ||
| { | ||
| public function run($value) | ||
| { | ||
| if ($value) { | ||
| return true; | ||
| } | ||
|
|
||
| return strtoupper('warning'); | ||
| } | ||
| } | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector\FixtureTrueInUnion; | ||
|
|
||
| /** | ||
| * true|othertype work on >= php 8.2, ref https://3v4l.org/UJqXT | ||
| */ | ||
| final class TrueInUnionStrToUpper | ||
| { | ||
| public function run($value): true|string | ||
| { | ||
| if ($value) { | ||
| return true; | ||
| } | ||
|
|
||
| return strtoupper('warning'); | ||
| } | ||
| } | ||
|
|
||
| ?> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,6 +39,7 @@ | |
| use PHPStan\Type\NullType; | ||
| use PHPStan\Type\ObjectType; | ||
| use PHPStan\Type\ObjectWithoutClassType; | ||
| use PHPStan\Type\StringType; | ||
| use PHPStan\Type\ThisType; | ||
| use PHPStan\Type\Type; | ||
| use PHPStan\Type\TypeCombinator; | ||
|
|
@@ -55,9 +56,11 @@ | |
| use Rector\NodeTypeResolver\NodeTypeCorrector\AccessoryNonEmptyStringTypeCorrector; | ||
| use Rector\NodeTypeResolver\NodeTypeCorrector\GenericClassStringTypeCorrector; | ||
| use Rector\NodeTypeResolver\PHPStan\ObjectWithoutClassTypeWithParentTypes; | ||
| use Rector\Php\PhpVersionProvider; | ||
| use Rector\StaticTypeMapper\ValueObject\Type\AliasedObjectType; | ||
| use Rector\StaticTypeMapper\ValueObject\Type\ShortenedObjectType; | ||
| use Rector\TypeDeclaration\PHPStan\ObjectTypeSpecifier; | ||
| use Rector\ValueObject\PhpVersion; | ||
|
|
||
| final class NodeTypeResolver | ||
| { | ||
|
|
@@ -83,6 +86,7 @@ public function __construct( | |
| private readonly AccessoryNonEmptyArrayTypeCorrector $accessoryNonEmptyArrayTypeCorrector, | ||
| private readonly RenamedClassesDataCollector $renamedClassesDataCollector, | ||
| private readonly NodeNameResolver $nodeNameResolver, | ||
| private readonly PhpVersionProvider $phpVersionProvider, | ||
| iterable $nodeTypeResolvers | ||
| ) { | ||
| foreach ($nodeTypeResolvers as $nodeTypeResolver) { | ||
|
|
@@ -620,6 +624,10 @@ private function resolveNativeTypeWithBuiltinMethodCallFallback(Expr $expr, Scop | |
| return $scope->getNativeType($expr); | ||
| } | ||
|
|
||
| if ($this->isSubstrOnPHP74($expr)) { | ||
| return new UnionType([new StringType(), new ConstantBooleanType(false)]); | ||
| } | ||
|
|
||
| return $scope->getType($expr); | ||
| } | ||
|
|
||
|
|
@@ -651,4 +659,20 @@ private function isEnumTypeMatch(MethodCall|NullsafeMethodCall $call, ObjectType | |
|
|
||
| return $classReflection->getName() === $objectType->getClassName(); | ||
| } | ||
|
|
||
| /** | ||
| * substr can return false on php 7.x and bellow | ||
| */ | ||
| private function isSubstrOnPHP74(FuncCall $funcCall): bool | ||
| { | ||
| if ($funcCall->isFirstClassCallable()) { | ||
| return false; | ||
| } | ||
|
|
||
| if (! $this->nodeNameResolver->isName($funcCall, 'substr')) { | ||
| return false; | ||
| } | ||
|
|
||
| return ! $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersion::PHP_80); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PHPStan already detect it clearly when run on php 7.x see https://phpstan.org/r/853f59a6-22ad-48ac-af93-66606b569613 on PHP 7.2 - 7.4 tab.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But not on PHP 8.0+. This also allow to add our test with PHP 7.4 version. |
||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this cast is needed here as downgrade rule is not yet created.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be fixed with incorrect input instead. This structure is clearly wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I will create separate PR target your branch for different logic to handle this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great 👍 We'll need to understand what values are going in any why it returns
false.It should be fixed to return always
stringinsteadThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see #7450