From b7d4f0678c3ff0574b67292a4951863c0f079313 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 7 Oct 2025 20:53:57 +0700 Subject: [PATCH 1/2] [DeadCode] Skip remove cast (string) on substr as may return false on php 7.x --- rector.php | 6 ------ .../Fixture/skip_substr.php.inc | 17 +++++++++++++++++ src/NodeTypeResolver/NodeTypeResolver.php | 6 ++++++ 3 files changed, 23 insertions(+), 6 deletions(-) create mode 100644 rules-tests/DeadCode/Rector/Cast/RecastingRemovalRector/Fixture/skip_substr.php.inc diff --git a/rector.php b/rector.php index 375966c5c91..b24f7fe82f0 100644 --- a/rector.php +++ b/rector.php @@ -4,7 +4,6 @@ use Rector\CodingStyle\Rector\String_\UseClassKeywordForClassNameResolutionRector; use Rector\Config\RectorConfig; -use Rector\DeadCode\Rector\Cast\RecastingRemovalRector; use Rector\DeadCode\Rector\ConstFetch\RemovePhpVersionIdCheckRector; use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector; @@ -53,9 +52,4 @@ __DIR__ . '/src/Configuration/RectorConfigBuilder.php', __DIR__ . '/src/Console/Notifier.php', ], - - // todo: properly handle, substr() can return false on php 7.x - RecastingRemovalRector::class => [ - __DIR__ . '/rules/CodingStyle/ClassNameImport/ClassNameImportSkipVoter/ClassLikeNameClassNameImportSkipVoter.php', - ], ]); diff --git a/rules-tests/DeadCode/Rector/Cast/RecastingRemovalRector/Fixture/skip_substr.php.inc b/rules-tests/DeadCode/Rector/Cast/RecastingRemovalRector/Fixture/skip_substr.php.inc new file mode 100644 index 00000000000..07f63228bfb --- /dev/null +++ b/rules-tests/DeadCode/Rector/Cast/RecastingRemovalRector/Fixture/skip_substr.php.inc @@ -0,0 +1,17 @@ + diff --git a/src/NodeTypeResolver/NodeTypeResolver.php b/src/NodeTypeResolver/NodeTypeResolver.php index 6cde992ec83..ea9ddec9ac7 100644 --- a/src/NodeTypeResolver/NodeTypeResolver.php +++ b/src/NodeTypeResolver/NodeTypeResolver.php @@ -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; @@ -620,6 +621,11 @@ private function resolveNativeTypeWithBuiltinMethodCallFallback(Expr $expr, Scop return $scope->getNativeType($expr); } + // substr can return false on php 7.x + if ($this->nodeNameResolver->isName($expr, 'substr') && ! $expr->isFirstClassCallable()) { + return new UnionType([new StringType(), new ConstantBooleanType(false)]); + } + return $scope->getType($expr); } From 0edfac468d70726850cbe9c60dc7b02953cb3daa Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 7 Oct 2025 20:58:39 +0700 Subject: [PATCH 2/2] fix fixture test --- .../Fixture/true_in_union_become_bool.php.inc | 4 ++-- .../FixtureTrueInUnion/true_in_union.php.inc | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector/Fixture/true_in_union_become_bool.php.inc b/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector/Fixture/true_in_union_become_bool.php.inc index a766298d571..624d4ff8f7d 100644 --- a/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector/Fixture/true_in_union_become_bool.php.inc +++ b/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector/Fixture/true_in_union_become_bool.php.inc @@ -13,7 +13,7 @@ final class TrueInUnionBecomeBool return true; } - return substr('warning', 1); + return strtoupper('warning'); } } @@ -34,7 +34,7 @@ final class TrueInUnionBecomeBool return true; } - return substr('warning', 1); + return strtoupper('warning'); } } diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector/FixtureTrueInUnion/true_in_union.php.inc b/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector/FixtureTrueInUnion/true_in_union.php.inc index db2839b5ad0..880dfafd56e 100644 --- a/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector/FixtureTrueInUnion/true_in_union.php.inc +++ b/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector/FixtureTrueInUnion/true_in_union.php.inc @@ -13,7 +13,7 @@ final class TrueInUnion return true; } - return substr('warning', 1); + return strtoupper('warning'); } } @@ -34,7 +34,7 @@ final class TrueInUnion return true; } - return substr('warning', 1); + return strtoupper('warning'); } }