-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathDowngradeVoidCastRector.php
More file actions
84 lines (73 loc) · 2.03 KB
/
DowngradeVoidCastRector.php
File metadata and controls
84 lines (73 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
declare(strict_types=1);
namespace Rector\DowngradePhp85\Rector\Expression;
use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\Cast\Void_;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Expression;
use Rector\Naming\Naming\VariableNaming;
use Rector\PHPStan\ScopeFetcher;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see https://wiki.php.net/rfc/marking_return_value_as_important
* @see \Rector\Tests\DowngradePhp85\Rector\Expression\DowngradeVoidCastRector\DowngradeVoidCastRectorTest
*/
final class DowngradeVoidCastRector extends AbstractRector
{
public function __construct(
private readonly VariableNaming $variableNaming
) {
}
public function getNodeTypes(): array
{
return [Expression::class];
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Replace void casts with proper handling of return values',
[
new CodeSample(
<<<'CODE_SAMPLE'
#[\NoDiscard]
function getPhpVersion(): string
{
return 'PHP 8.5';
}
(void) getPhpVersion();
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
#[\NoDiscard]
function getPhpVersion(): string
{
return 'PHP 8.5';
}
$_void = getPhpVersion();
CODE_SAMPLE
),
]
);
}
/**
* @param Expression $node
*/
public function refactor(Node $node): ?Node
{
if (! $node->expr instanceof Void_) {
return null;
}
$scope = ScopeFetcher::fetch($node);
$variable = new Variable($this->variableNaming->createCountedValueName('_void', $scope));
// the assign is needed to avoid warning
// see https://3v4l.org/ie68D#v8.5.3 vs https://3v4l.org/nLc5J#v8.5.3
$node->expr = new Assign(
$variable,
$node->expr->expr
);
return $node;
}
}