From a25e073b62d1c69af2ac8b3a46a74b5a0f7576d3 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Wed, 8 Oct 2025 12:11:53 +0700 Subject: [PATCH 1/2] Directly check type of substr() on RecastingRemovalRector --- rules/DeadCode/Rector/Cast/RecastingRemovalRector.php | 9 +++++++++ src/NodeTypeResolver/NodeTypeResolver.php | 6 ------ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/rules/DeadCode/Rector/Cast/RecastingRemovalRector.php b/rules/DeadCode/Rector/Cast/RecastingRemovalRector.php index 616459d996a..fa2f3ce3fbd 100644 --- a/rules/DeadCode/Rector/Cast/RecastingRemovalRector.php +++ b/rules/DeadCode/Rector/Cast/RecastingRemovalRector.php @@ -13,12 +13,14 @@ use PhpParser\Node\Expr\Cast\Int_; use PhpParser\Node\Expr\Cast\Object_; use PhpParser\Node\Expr\Cast\String_; +use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\StaticCall; use PHPStan\Reflection\Php\PhpPropertyReflection; use PHPStan\Type\ArrayType; use PHPStan\Type\BooleanType; use PHPStan\Type\Constant\ConstantArrayType; +use PHPStan\Type\Constant\ConstantBooleanType; use PHPStan\Type\FloatType; use PHPStan\Type\IntegerType; use PHPStan\Type\MixedType; @@ -103,6 +105,13 @@ public function refactor(Node $node): ?Node return null; } + // substr can return false on php 7.x + if ($node->expr instanceof FuncCall + && $this->isName($node->expr, 'substr') + && ! $node->expr->isFirstClassCallable()) { + $nodeType = new UnionType([new StringType(), new ConstantBooleanType(false)]); + } + if ($nodeType instanceof ConstantArrayType && $nodeClass === Array_::class) { if ($this->shouldSkip($node->expr)) { return null; diff --git a/src/NodeTypeResolver/NodeTypeResolver.php b/src/NodeTypeResolver/NodeTypeResolver.php index ea9ddec9ac7..6cde992ec83 100644 --- a/src/NodeTypeResolver/NodeTypeResolver.php +++ b/src/NodeTypeResolver/NodeTypeResolver.php @@ -39,7 +39,6 @@ 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; @@ -621,11 +620,6 @@ 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 39a293f4f76e6b124cf90e8ce6747f17fb1e0e6b Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Wed, 8 Oct 2025 12:15:44 +0700 Subject: [PATCH 2/2] update fixture --- .../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 624d4ff8f7d..a766298d571 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 strtoupper('warning'); + return substr('warning', 1); } } @@ -34,7 +34,7 @@ final class TrueInUnionBecomeBool return true; } - return strtoupper('warning'); + return substr('warning', 1); } } 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 880dfafd56e..db2839b5ad0 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 strtoupper('warning'); + return substr('warning', 1); } } @@ -34,7 +34,7 @@ final class TrueInUnion return true; } - return strtoupper('warning'); + return substr('warning', 1); } }