Skip to content

Commit efec3d1

Browse files
committed
[TypeDeclaration] Add NarrowBoolDocblockReturnTypeRector to narrow @return bool to false/true based on native return type
1 parent de0132c commit efec3d1

10 files changed

Lines changed: 348 additions & 0 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\NarrowBoolDocblockReturnTypeRector\Fixture;
4+
5+
class NarrowBoolToFalse
6+
{
7+
/**
8+
* @return bool|string[]
9+
*/
10+
public function run(): false|array
11+
{
12+
return false;
13+
}
14+
}
15+
16+
?>
17+
-----
18+
<?php
19+
20+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\NarrowBoolDocblockReturnTypeRector\Fixture;
21+
22+
class NarrowBoolToFalse
23+
{
24+
/**
25+
* @return false|string[]
26+
*/
27+
public function run(): false|array
28+
{
29+
return false;
30+
}
31+
}
32+
33+
?>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\NarrowBoolDocblockReturnTypeRector\Fixture;
4+
5+
class NarrowBoolToTrue
6+
{
7+
/**
8+
* @return bool|int[]
9+
*/
10+
public function run(): true|array
11+
{
12+
return true;
13+
}
14+
}
15+
16+
?>
17+
-----
18+
<?php
19+
20+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\NarrowBoolDocblockReturnTypeRector\Fixture;
21+
22+
class NarrowBoolToTrue
23+
{
24+
/**
25+
* @return true|int[]
26+
*/
27+
public function run(): true|array
28+
{
29+
return true;
30+
}
31+
}
32+
33+
?>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\NarrowBoolDocblockReturnTypeRector\Fixture;
4+
5+
/**
6+
* @return bool|string[]
7+
*/
8+
function narrowFunctionReturn(): false|array
9+
{
10+
return false;
11+
}
12+
13+
?>
14+
-----
15+
<?php
16+
17+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\NarrowBoolDocblockReturnTypeRector\Fixture;
18+
19+
/**
20+
* @return false|string[]
21+
*/
22+
function narrowFunctionReturn(): false|array
23+
{
24+
return false;
25+
}
26+
27+
?>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\NarrowBoolDocblockReturnTypeRector\Fixture;
4+
5+
class SkipNativeBool
6+
{
7+
/**
8+
* @return bool|string[]
9+
*/
10+
public function run(): bool|array
11+
{
12+
return false;
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\NarrowBoolDocblockReturnTypeRector\Fixture;
4+
5+
class SkipNativeFalseAndTrue
6+
{
7+
/**
8+
* @return bool|string[]
9+
*/
10+
public function run(): false|true|array
11+
{
12+
return false;
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\NarrowBoolDocblockReturnTypeRector\Fixture;
4+
5+
class SkipNoBoolInDocblock
6+
{
7+
/**
8+
* @return false|string[]
9+
*/
10+
public function run(): false|array
11+
{
12+
return false;
13+
}
14+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\NarrowBoolDocblockReturnTypeRector\Fixture;
4+
5+
class SkipNoDocblock
6+
{
7+
public function run(): false|array
8+
{
9+
return false;
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\NarrowBoolDocblockReturnTypeRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class NarrowBoolDocblockReturnTypeRectorTest extends AbstractRectorTestCase
12+
{
13+
#[DataProvider('provideData')]
14+
public function test(string $filePath): void
15+
{
16+
$this->doTestFile($filePath);
17+
}
18+
19+
public static function provideData(): Iterator
20+
{
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
22+
}
23+
24+
public function provideConfigFilePath(): string
25+
{
26+
return __DIR__ . '/config/configured_rule.php';
27+
}
28+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\TypeDeclaration\Rector\ClassMethod\NarrowBoolDocblockReturnTypeRector;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->rule(NarrowBoolDocblockReturnTypeRector::class);
10+
};
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\TypeDeclaration\Rector\ClassMethod;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Identifier;
9+
use PhpParser\Node\Stmt\ClassMethod;
10+
use PhpParser\Node\Stmt\Function_;
11+
use PhpParser\Node\UnionType;
12+
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
13+
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
14+
use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode;
15+
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
16+
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
17+
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
18+
use Rector\Rector\AbstractRector;
19+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
20+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
21+
22+
/**
23+
* @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\NarrowBoolDocblockReturnTypeRector\NarrowBoolDocblockReturnTypeRectorTest
24+
*/
25+
final class NarrowBoolDocblockReturnTypeRector extends AbstractRector
26+
{
27+
public function __construct(
28+
private readonly PhpDocInfoFactory $phpDocInfoFactory,
29+
private readonly DocBlockUpdater $docBlockUpdater,
30+
) {
31+
}
32+
33+
public function getRuleDefinition(): RuleDefinition
34+
{
35+
return new RuleDefinition(
36+
'Narrow @return docblock "bool" to "false" or "true" when the native return type only allows one of them',
37+
[
38+
new CodeSample(
39+
<<<'CODE_SAMPLE'
40+
class SomeClass
41+
{
42+
/**
43+
* @return bool|string[]
44+
*/
45+
public function run(): false|array
46+
{
47+
return false;
48+
}
49+
}
50+
CODE_SAMPLE
51+
,
52+
<<<'CODE_SAMPLE'
53+
class SomeClass
54+
{
55+
/**
56+
* @return false|string[]
57+
*/
58+
public function run(): false|array
59+
{
60+
return false;
61+
}
62+
}
63+
CODE_SAMPLE
64+
),
65+
],
66+
);
67+
}
68+
69+
public function getNodeTypes(): array
70+
{
71+
return [ClassMethod::class, Function_::class];
72+
}
73+
74+
/**
75+
* @param ClassMethod|Function_ $node
76+
*/
77+
public function refactor(Node $node): null|ClassMethod|Function_
78+
{
79+
$constantBoolName = $this->matchNativeConstantBoolName($node);
80+
if ($constantBoolName === null) {
81+
return null;
82+
}
83+
84+
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($node);
85+
if (! $phpDocInfo instanceof PhpDocInfo) {
86+
return null;
87+
}
88+
89+
$returnTagValueNode = $phpDocInfo->getReturnTagValue();
90+
if (! $returnTagValueNode instanceof ReturnTagValueNode) {
91+
return null;
92+
}
93+
94+
if (! $returnTagValueNode->type instanceof UnionTypeNode) {
95+
return null;
96+
}
97+
98+
if (! $this->narrowBoolInUnion($returnTagValueNode->type, $constantBoolName)) {
99+
return null;
100+
}
101+
102+
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node);
103+
104+
return $node;
105+
}
106+
107+
/**
108+
* Returns "false" or "true" when the native return type allows exactly one of them, null otherwise.
109+
*/
110+
private function matchNativeConstantBoolName(ClassMethod|Function_ $node): ?string
111+
{
112+
$returnType = $node->returnType;
113+
if (! $returnType instanceof UnionType) {
114+
return null;
115+
}
116+
117+
$hasFalse = false;
118+
$hasTrue = false;
119+
120+
foreach ($returnType->types as $type) {
121+
if (! $type instanceof Identifier) {
122+
continue;
123+
}
124+
125+
$lowerName = $type->toLowerString();
126+
if ($lowerName === 'bool') {
127+
// ambiguous, both values possible
128+
return null;
129+
}
130+
131+
if ($lowerName === 'false') {
132+
$hasFalse = true;
133+
}
134+
135+
if ($lowerName === 'true') {
136+
$hasTrue = true;
137+
}
138+
}
139+
140+
if ($hasFalse && ! $hasTrue) {
141+
return 'false';
142+
}
143+
144+
if ($hasTrue && ! $hasFalse) {
145+
return 'true';
146+
}
147+
148+
return null;
149+
}
150+
151+
private function narrowBoolInUnion(UnionTypeNode $unionTypeNode, string $constantBoolName): bool
152+
{
153+
$hasChanged = false;
154+
155+
foreach ($unionTypeNode->types as $key => $typeNode) {
156+
if ($typeNode instanceof IdentifierTypeNode && $typeNode->name === 'bool') {
157+
$unionTypeNode->types[$key] = new IdentifierTypeNode($constantBoolName);
158+
$hasChanged = true;
159+
}
160+
}
161+
162+
return $hasChanged;
163+
}
164+
}

0 commit comments

Comments
 (0)