Skip to content

Commit 22bee06

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

File tree

6 files changed

+251
-0
lines changed

6 files changed

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

0 commit comments

Comments
 (0)