Skip to content

Commit 974a73f

Browse files
authored
[DeadCode] Skip remove cast (string) on substr as may return false on php 7.x on RecastingRemovalRector (#7442)
* [DeadCode] Skip remove cast (string) on substr as may return false on php 7.x * fix fixture test
1 parent 69ccff5 commit 974a73f

File tree

5 files changed

+27
-10
lines changed

5 files changed

+27
-10
lines changed

rector.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Rector\CodingStyle\Rector\String_\UseClassKeywordForClassNameResolutionRector;
66
use Rector\Config\RectorConfig;
7-
use Rector\DeadCode\Rector\Cast\RecastingRemovalRector;
87
use Rector\DeadCode\Rector\ConstFetch\RemovePhpVersionIdCheckRector;
98
use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector;
109

@@ -53,9 +52,4 @@
5352
__DIR__ . '/src/Configuration/RectorConfigBuilder.php',
5453
__DIR__ . '/src/Console/Notifier.php',
5554
],
56-
57-
// todo: properly handle, substr() can return false on php 7.x
58-
RecastingRemovalRector::class => [
59-
__DIR__ . '/rules/CodingStyle/ClassNameImport/ClassNameImportSkipVoter/ClassLikeNameClassNameImportSkipVoter.php',
60-
],
6155
]);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\Cast\RecastingRemovalRector\Fixture;
4+
5+
/**
6+
* On PHP 7.4, substr() can returns false
7+
*/
8+
class SkipSubstr
9+
{
10+
public function run(): string
11+
{
12+
$className = 'Exception';
13+
return (string) substr($className, 0, -strlen($className) - 1);
14+
}
15+
}
16+
17+
?>

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 substr('warning', 1);
16+
return strtoupper('warning');
1717
}
1818
}
1919

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

37-
return substr('warning', 1);
37+
return strtoupper('warning');
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 substr('warning', 1);
16+
return strtoupper('warning');
1717
}
1818
}
1919

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

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

src/NodeTypeResolver/NodeTypeResolver.php

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

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+
623629
return $scope->getType($expr);
624630
}
625631

0 commit comments

Comments
 (0)