Skip to content

Commit 9828dbe

Browse files
authored
[Downgradephp85] Add DowngradeVoidCastRector (#369)
* [Downgradephp85] Add DowngradeVoidCastRector * folder rename * fix phpstan * use php 8.5 to allow test (void) cast syntax
1 parent 549a1e3 commit 9828dbe

File tree

9 files changed

+189
-2
lines changed

9 files changed

+189
-2
lines changed

.github/workflows/code_analysis.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ jobs:
5050
-
5151
uses: shivammathur/setup-php@v2
5252
with:
53-
php-version: 8.3
53+
# use php 8.5 to allow test (void) cast syntax
54+
php-version: 8.5
5455
coverage: none
5556

5657
- uses: "ramsey/composer-install@v2"

config/set/downgrade-php85.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Rector\Config\RectorConfig;
66
use Rector\DowngradePhp85\Rector\Class_\DowngradeFinalPropertyPromotionRector;
7+
use Rector\DowngradePhp85\Rector\Expression\DowngradeVoidCastRector;
78
use Rector\DowngradePhp85\Rector\FuncCall\DowngradeArrayFirstLastRector;
89
use Rector\DowngradePhp85\Rector\StmtsAwareInterface\DowngradePipeOperatorRector;
910
use Rector\Renaming\Rector\ClassConstFetch\RenameClassConstFetchRector;
@@ -18,6 +19,7 @@
1819
DowngradeArrayFirstLastRector::class,
1920
DowngradeFinalPropertyPromotionRector::class,
2021
DowngradePipeOperatorRector::class,
22+
DowngradeVoidCastRector::class,
2123
]);
2224

2325
// https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_driver_specific_pdo_constants_and_methods
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\DowngradePhp85\Rector\Expression\DowngradeVoidCastRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use PHPUnit\Framework\Attributes\RequiresPhp;
10+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
11+
12+
final class DowngradeVoidCastRectorTest extends AbstractRectorTestCase
13+
{
14+
#[DataProvider('provideData')]
15+
#[RequiresPhp('8.5')]
16+
public function test(string $filePath): void
17+
{
18+
$this->doTestFile($filePath);
19+
}
20+
21+
public static function provideData(): Iterator
22+
{
23+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
24+
}
25+
26+
public function provideConfigFilePath(): string
27+
{
28+
return __DIR__ . '/config/configured_rule.php';
29+
}
30+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\DowngradePhp85\Rector\Expression\DowngradeVoidCastRector\Fixture;
6+
7+
final class Fixture
8+
{
9+
#[\NoDiscard]
10+
function foo(): string {
11+
return 'bar';
12+
}
13+
14+
public function run()
15+
{
16+
(void) $this->foo();
17+
}
18+
}
19+
20+
?>
21+
-----
22+
<?php
23+
24+
declare(strict_types=1);
25+
26+
namespace Rector\Tests\DowngradePhp85\Rector\Expression\DowngradeVoidCastRector\Fixture;
27+
28+
final class Fixture
29+
{
30+
#[\NoDiscard]
31+
function foo(): string {
32+
return 'bar';
33+
}
34+
35+
public function run()
36+
{
37+
$_void = $this->foo();
38+
}
39+
}
40+
41+
?>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\DowngradePhp85\Rector\Expression\DowngradeVoidCastRector\Fixture;
6+
7+
final class SkipDifferentCast
8+
{
9+
public function run()
10+
{
11+
(int) call();
12+
}
13+
}
14+
15+
?>
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\DowngradePhp85\Rector\Expression\DowngradeVoidCastRector;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->rule(DowngradeVoidCastRector::class);
10+
};

rules/DowngradePhp80/Rector/ClassMethod/DowngradeTrailingCommasInParamUseRector.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ private function cleanTrailingComma(Closure|ClassMethod|Function_ $node, array $
118118
{
119119
$lastPosition = array_key_last($array);
120120

121+
if ($lastPosition === null) {
122+
return null;
123+
}
124+
121125
$last = $array[$lastPosition];
122126
if (! $this->followedByCommaAnalyzer->isFollowed($this->file, $last)) {
123127
return null;

rules/DowngradePhp80/Rector/Class_/DowngradePropertyPromotionRector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ private function getOldComments(ClassMethod $constructorClassMethod): array
112112
{
113113
$oldComments = [];
114114
foreach ($constructorClassMethod->params as $param) {
115-
$oldComments[$this->getName($param->var)] = $param->getAttribute(AttributeKey::COMMENTS);
115+
$oldComments[(string) $this->getName($param->var)] = $param->getAttribute(AttributeKey::COMMENTS);
116116
}
117117

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

0 commit comments

Comments
 (0)