Skip to content

Commit d57b45e

Browse files
authored
[DeadCode] Skip ClassLikeNameClassNameImportSkipVoter for RecastingRemovalRector (#7448)
1 parent 7e1ee78 commit d57b45e

File tree

4 files changed

+35
-26
lines changed

4 files changed

+35
-26
lines changed

rector.php

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

55
use Rector\CodingStyle\Rector\String_\UseClassKeywordForClassNameResolutionRector;
66
use Rector\Config\RectorConfig;
7+
use Rector\DeadCode\Rector\Cast\RecastingRemovalRector;
78
use Rector\DeadCode\Rector\ConstFetch\RemovePhpVersionIdCheckRector;
89
use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector;
910

@@ -52,4 +53,9 @@
5253
__DIR__ . '/src/Configuration/RectorConfigBuilder.php',
5354
__DIR__ . '/src/Console/Notifier.php',
5455
],
56+
57+
// on php 7.x, substr() result can return false, so force (string) is needed
58+
RecastingRemovalRector::class => [
59+
__DIR__ . '/rules/CodingStyle/ClassNameImport/ClassNameImportSkipVoter/ClassLikeNameClassNameImportSkipVoter.php',
60+
],
5561
]);

rules-tests/DeadCode/Rector/Cast/RecastingRemovalRector/Fixture/skip_substr.php.inc

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\Cast\RecastingRemovalRector\Fixture;
4+
5+
class WithSubstr
6+
{
7+
public function run(): string
8+
{
9+
$className = 'Exception';
10+
return (string) substr($className, 0, -strlen($className) - 1);
11+
}
12+
}
13+
14+
?>
15+
-----
16+
<?php
17+
18+
namespace Rector\Tests\DeadCode\Rector\Cast\RecastingRemovalRector\Fixture;
19+
20+
class WithSubstr
21+
{
22+
public function run(): string
23+
{
24+
$className = 'Exception';
25+
return substr($className, 0, -strlen($className) - 1);
26+
}
27+
}
28+
29+
?>

rules/DeadCode/Rector/Cast/RecastingRemovalRector.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@
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;
1716
use PhpParser\Node\Expr\MethodCall;
1817
use PhpParser\Node\Expr\StaticCall;
1918
use PHPStan\Reflection\Php\PhpPropertyReflection;
2019
use PHPStan\Type\ArrayType;
2120
use PHPStan\Type\BooleanType;
2221
use PHPStan\Type\Constant\ConstantArrayType;
23-
use PHPStan\Type\Constant\ConstantBooleanType;
2422
use PHPStan\Type\FloatType;
2523
use PHPStan\Type\IntegerType;
2624
use PHPStan\Type\MixedType;
@@ -105,13 +103,6 @@ public function refactor(Node $node): ?Node
105103
return null;
106104
}
107105

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-
115106
if ($nodeType instanceof ConstantArrayType && $nodeClass === Array_::class) {
116107
if ($this->shouldSkip($node->expr)) {
117108
return null;

0 commit comments

Comments
 (0)