Skip to content

Commit 50a4095

Browse files
authored
handle cases, if int is returned in nested call in NoIntegerRefactorReturnRule (#250)
1 parent cb45457 commit 50a4095

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

src/Rules/Rector/NoIntegerRefactorReturnRule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function processNode(Node $node, Scope $scope): array
6565
$constantNames = $this->findUsedNodeVisitorConstantNames($node);
6666

6767
$undesiredConstantNames = array_diff($constantNames, ['REMOVE_NODE']);
68-
if ($undesiredConstantNames === []) {
68+
if ($constantNames !== [] && $undesiredConstantNames === []) {
6969
return [];
7070
}
7171

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Symplify\PHPStanRules\Tests\Rules\Rector\NoIntegerRefactorReturnRule\Fixture;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Stmt\Class_;
9+
use PhpParser\Node\Stmt\Interface_;
10+
use PhpParser\NodeVisitor;
11+
use Rector\Rector\AbstractRector;
12+
13+
final class NestedReturnInt extends AbstractRector
14+
{
15+
public function getNodeTypes(): array
16+
{
17+
return [Class_::class, Interface_::class];
18+
}
19+
20+
public function refactor(Node $node): \PhpParser\Node|int
21+
{
22+
if ($node instanceof Class_) {
23+
return $node;
24+
}
25+
26+
return $this->getDontTraverseChildren();
27+
}
28+
29+
private function getDontTraverseChildren(): int
30+
{
31+
return NodeVisitor::DONT_TRAVERSE_CHILDREN;
32+
}
33+
}

tests/Rules/Rector/NoIntegerRefactorReturnRule/NoIntegerRefactorReturnRuleTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ public static function provideData(): Iterator
2222
$errorMessage = NoIntegerRefactorReturnRule::ERROR_MESSAGE;
2323
yield [__DIR__ . '/Fixture/SimpleReturnInt.php', [[$errorMessage, 20]]];
2424

25+
$errorMessage = NoIntegerRefactorReturnRule::ERROR_MESSAGE;
26+
yield [__DIR__ . '/Fixture/NestedReturnInt.php', [[$errorMessage, 20]]];
27+
2528
yield [__DIR__ . '/Fixture/SkipBareNodeReturn.php', []];
2629
yield [__DIR__ . '/Fixture/AllowRemoveNode.php', []];
2730
yield [__DIR__ . '/Fixture/AllowNestedClosure.php', []];

0 commit comments

Comments
 (0)