Skip to content

Commit 15d2df3

Browse files
authored
Directly check type of substr() on RecastingRemovalRector (#7446)
* Directly check type of substr() on RecastingRemovalRector * update fixture
1 parent c410da9 commit 15d2df3

4 files changed

Lines changed: 13 additions & 10 deletions

File tree

rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector/Fixture/true_in_union_become_bool.php.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ final class TrueInUnionBecomeBool
1313
return true;
1414
}
1515

16-
return strtoupper('warning');
16+
return substr('warning', 1);
1717
}
1818
}
1919

@@ -34,7 +34,7 @@ final class TrueInUnionBecomeBool
3434
return true;
3535
}
3636

37-
return strtoupper('warning');
37+
return substr('warning', 1);
3838
}
3939
}
4040

rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector/FixtureTrueInUnion/true_in_union.php.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ final class TrueInUnion
1313
return true;
1414
}
1515

16-
return strtoupper('warning');
16+
return substr('warning', 1);
1717
}
1818
}
1919

@@ -34,7 +34,7 @@ final class TrueInUnion
3434
return true;
3535
}
3636

37-
return strtoupper('warning');
37+
return substr('warning', 1);
3838
}
3939
}
4040

rules/DeadCode/Rector/Cast/RecastingRemovalRector.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@
1313
use PhpParser\Node\Expr\Cast\Int_;
1414
use PhpParser\Node\Expr\Cast\Object_;
1515
use PhpParser\Node\Expr\Cast\String_;
16+
use PhpParser\Node\Expr\FuncCall;
1617
use PhpParser\Node\Expr\MethodCall;
1718
use PhpParser\Node\Expr\StaticCall;
1819
use PHPStan\Reflection\Php\PhpPropertyReflection;
1920
use PHPStan\Type\ArrayType;
2021
use PHPStan\Type\BooleanType;
2122
use PHPStan\Type\Constant\ConstantArrayType;
23+
use PHPStan\Type\Constant\ConstantBooleanType;
2224
use PHPStan\Type\FloatType;
2325
use PHPStan\Type\IntegerType;
2426
use PHPStan\Type\MixedType;
@@ -103,6 +105,13 @@ public function refactor(Node $node): ?Node
103105
return null;
104106
}
105107

108+
// substr can return false on php 7.x
109+
if ($node->expr instanceof FuncCall
110+
&& $this->isName($node->expr, 'substr')
111+
&& ! $node->expr->isFirstClassCallable()) {
112+
$nodeType = new UnionType([new StringType(), new ConstantBooleanType(false)]);
113+
}
114+
106115
if ($nodeType instanceof ConstantArrayType && $nodeClass === Array_::class) {
107116
if ($this->shouldSkip($node->expr)) {
108117
return null;

src/NodeTypeResolver/NodeTypeResolver.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
use PHPStan\Type\NullType;
4040
use PHPStan\Type\ObjectType;
4141
use PHPStan\Type\ObjectWithoutClassType;
42-
use PHPStan\Type\StringType;
4342
use PHPStan\Type\ThisType;
4443
use PHPStan\Type\Type;
4544
use PHPStan\Type\TypeCombinator;
@@ -621,11 +620,6 @@ private function resolveNativeTypeWithBuiltinMethodCallFallback(Expr $expr, Scop
621620
return $scope->getNativeType($expr);
622621
}
623622

624-
// substr can return false on php 7.x
625-
if ($this->nodeNameResolver->isName($expr, 'substr') && ! $expr->isFirstClassCallable()) {
626-
return new UnionType([new StringType(), new ConstantBooleanType(false)]);
627-
}
628-
629623
return $scope->getType($expr);
630624
}
631625

0 commit comments

Comments
 (0)