Skip to content

Commit cfd2280

Browse files
committed
feature: add handling for void magic methods
1 parent 6535bab commit cfd2280

File tree

6 files changed

+244
-0
lines changed

6 files changed

+244
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUselessVoidReturnFromDocblockOnVoidMagicMethodsRector\Fixture;
4+
5+
class RemoveReturnClone
6+
{
7+
/**
8+
* Other comments
9+
*
10+
* @return void
11+
*/
12+
function __clone()
13+
{
14+
//
15+
}
16+
}
17+
18+
?>
19+
-----
20+
<?php
21+
22+
namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUselessVoidReturnFromDocblockOnVoidMagicMethodsRector\Fixture;
23+
24+
class RemoveReturnClone
25+
{
26+
/**
27+
* Other comments
28+
*/
29+
function __clone()
30+
{
31+
//
32+
}
33+
}
34+
35+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUselessVoidReturnFromDocblockOnVoidMagicMethodsRector\Fixture;
4+
5+
class RemoveReturnConstruct
6+
{
7+
/**
8+
* @return void
9+
*/
10+
function __construct()
11+
{
12+
//
13+
}
14+
}
15+
16+
?>
17+
-----
18+
<?php
19+
20+
namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUselessVoidReturnFromDocblockOnVoidMagicMethodsRector\Fixture;
21+
22+
class RemoveReturnConstruct
23+
{
24+
function __construct()
25+
{
26+
//
27+
}
28+
}
29+
30+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUselessVoidReturnFromDocblockOnVoidMagicMethodsRector\Fixture;
4+
5+
class RemoveReturnDestruct
6+
{
7+
/**
8+
* @return void
9+
*/
10+
function __destruct()
11+
{
12+
//
13+
}
14+
}
15+
16+
?>
17+
-----
18+
<?php
19+
20+
namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUselessVoidReturnFromDocblockOnVoidMagicMethodsRector\Fixture;
21+
22+
class RemoveReturnDestruct
23+
{
24+
function __destruct()
25+
{
26+
//
27+
}
28+
}
29+
30+
?>
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\DeadCode\Rector\ClassMethod\RemoveUselessVoidReturnFromDocblockOnVoidMagicMethodsRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class RemoveUselessVoidReturnFromDocblockVoidMagicMethodsRectorTest 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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\DeadCode\Rector\ClassMethod\RemoveUselessVoidReturnFromDocblockVoidMagicMethodsRector;
6+
use Rector\Config\RectorConfig;
7+
8+
return RectorConfig::configure()
9+
->withRules([RemoveUselessVoidReturnFromDocblockVoidMagicMethodsRector::class]);
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\DeadCode\Rector\ClassMethod;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Stmt\ClassMethod;
9+
use PHPStan\PhpDocParser\Ast\Node as AstNode;
10+
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
11+
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
12+
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
13+
use Rector\PhpDocParser\PhpDocParser\PhpDocNodeTraverser;
14+
use Rector\Rector\AbstractRector;
15+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
16+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
17+
18+
/**
19+
* @see \Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUselessVoidReturnFromDocblockOnVoidMagicMethodsRector\RemoveUselessVoidReturnFromDocblockVoidMagicMethodsRectorTest
20+
*/
21+
final class RemoveUselessVoidReturnFromDocblockVoidMagicMethodsRector extends AbstractRector
22+
{
23+
public function __construct(
24+
private readonly DocBlockUpdater $docBlockUpdater,
25+
private readonly PhpDocInfoFactory $phpDocInfoFactory,
26+
) {
27+
}
28+
29+
public function getRuleDefinition(): RuleDefinition
30+
{
31+
return new RuleDefinition(
32+
'Remove useless @return void docblock from magic methods __construct, __destruct, and __clone',
33+
[
34+
new CodeSample(
35+
<<<'CODE_SAMPLE'
36+
class SomeClass
37+
{
38+
/**
39+
* @return void
40+
*/
41+
public function __construct() {}
42+
43+
/**
44+
* @return void
45+
*/
46+
public function __destruct() {}
47+
48+
/**
49+
* @return void
50+
*/
51+
public function __clone() {}
52+
}
53+
CODE_SAMPLE
54+
,
55+
<<<'CODE_SAMPLE'
56+
class SomeClass
57+
{
58+
public function __construct() {}
59+
60+
public function __destruct() {}
61+
62+
public function __clone() {}
63+
}
64+
CODE_SAMPLE
65+
),
66+
]
67+
);
68+
}
69+
70+
public function getNodeTypes(): array
71+
{
72+
return [ClassMethod::class];
73+
}
74+
75+
/**
76+
* @param ClassMethod $node
77+
*/
78+
public function refactor(Node $node): ?Node
79+
{
80+
$magicMethodNames = ['__construct', '__destruct', '__clone'];
81+
82+
$methodName = $this->getName($node);
83+
84+
if (! in_array($methodName, $magicMethodNames, true)) {
85+
return null;
86+
}
87+
88+
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
89+
90+
$returnTagValueNode = $phpDocInfo->getReturnTagValue();
91+
92+
$phpDocNodeTraverser = new PhpDocNodeTraverser();
93+
$phpDocNodeTraverser->traverseWithCallable(
94+
$phpDocInfo->getPhpDocNode(),
95+
'',
96+
function (AstNode $astNode) use ($returnTagValueNode): ?int {
97+
if (! $astNode instanceof PhpDocTagNode) {
98+
return null;
99+
}
100+
101+
if ($astNode->value === $returnTagValueNode) {
102+
return PhpDocNodeTraverser::NODE_REMOVE;
103+
}
104+
105+
return null;
106+
}
107+
);
108+
109+
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node);
110+
return $node;
111+
}
112+
}

0 commit comments

Comments
 (0)