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/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'); } } 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); }