Skip to content

Commit cbb17de

Browse files
authored
[DowngradePhp81] Add AddReturnTypeWillChangeAttributeRector (#372)
- PHP 8.1 added provisional return types to built-in interfaces (ArrayAccess, Countable, Iterator, IteratorAggregate, Stringable), triggering deprecation notices on implementations without matching return types. - This rule automatically adds #[\ReturnTypeWillChange] to affected methods, suppressing those notices for codebases that need to remain compatible with older PHP versions and cannot yet add the required return types. Fixes: rectorphp/rector#9515
1 parent b566446 commit cbb17de

11 files changed

Lines changed: 431 additions & 0 deletions

File tree

config/set/downgrade-php81.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Rector\Config\RectorConfig;
1010
use Rector\ValueObject\PhpVersion;
1111
use Rector\DowngradePhp81\Rector\ClassConst\DowngradeFinalizePublicClassConstantRector;
12+
use Rector\DowngradePhp81\Rector\ClassMethod\AddReturnTypeWillChangeAttributeRector;
1213
use Rector\DowngradePhp81\Rector\FuncCall\DowngradeArrayIsListRector;
1314
use Rector\DowngradePhp81\Rector\FuncCall\DowngradeFirstClassCallableSyntaxRector;
1415
use Rector\DowngradePhp81\Rector\FunctionLike\DowngradeNeverTypeDeclarationRector;
@@ -25,6 +26,7 @@
2526
$rectorConfig->phpVersion(PhpVersion::PHP_80);
2627

2728
$rectorConfig->rules([
29+
AddReturnTypeWillChangeAttributeRector::class,
2830
DowngradeFinalizePublicClassConstantRector::class,
2931
DowngradeFirstClassCallableSyntaxRector::class,
3032
DowngradeNeverTypeDeclarationRector::class,
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\DowngradePhp81\Rector\ClassMethod\AddReturnTypeWillChangeAttributeRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class AddReturnTypeWillChangeAttributeRectorTest 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: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace Rector\Tests\DowngradePhp81\Rector\ClassMethod\AddReturnTypeWillChangeAttributeRector\Fixture;
4+
5+
class ArrayAccessClass implements \ArrayAccess
6+
{
7+
public function offsetGet($offset)
8+
{
9+
}
10+
11+
public function offsetExists($offset)
12+
{
13+
}
14+
15+
public function offsetSet($offset, $value)
16+
{
17+
}
18+
19+
public function offsetUnset($offset)
20+
{
21+
}
22+
}
23+
24+
?>
25+
-----
26+
<?php
27+
28+
namespace Rector\Tests\DowngradePhp81\Rector\ClassMethod\AddReturnTypeWillChangeAttributeRector\Fixture;
29+
30+
class ArrayAccessClass implements \ArrayAccess
31+
{
32+
#[\ReturnTypeWillChange]
33+
public function offsetGet($offset)
34+
{
35+
}
36+
37+
#[\ReturnTypeWillChange]
38+
public function offsetExists($offset)
39+
{
40+
}
41+
42+
#[\ReturnTypeWillChange]
43+
public function offsetSet($offset, $value)
44+
{
45+
}
46+
47+
#[\ReturnTypeWillChange]
48+
public function offsetUnset($offset)
49+
{
50+
}
51+
}
52+
53+
?>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Rector\Tests\DowngradePhp81\Rector\ClassMethod\AddReturnTypeWillChangeAttributeRector\Fixture;
4+
5+
class SomeCountable implements \Countable
6+
{
7+
public function count()
8+
{
9+
}
10+
}
11+
12+
?>
13+
-----
14+
<?php
15+
16+
namespace Rector\Tests\DowngradePhp81\Rector\ClassMethod\AddReturnTypeWillChangeAttributeRector\Fixture;
17+
18+
class SomeCountable implements \Countable
19+
{
20+
#[\ReturnTypeWillChange]
21+
public function count()
22+
{
23+
}
24+
}
25+
26+
?>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace Rector\Tests\DowngradePhp81\Rector\ClassMethod\AddReturnTypeWillChangeAttributeRector\Fixture;
4+
5+
class SomeIterator implements \Iterator
6+
{
7+
public function current()
8+
{
9+
}
10+
11+
public function key()
12+
{
13+
}
14+
15+
public function next()
16+
{
17+
}
18+
19+
public function rewind()
20+
{
21+
}
22+
23+
public function valid()
24+
{
25+
}
26+
}
27+
28+
?>
29+
-----
30+
<?php
31+
32+
namespace Rector\Tests\DowngradePhp81\Rector\ClassMethod\AddReturnTypeWillChangeAttributeRector\Fixture;
33+
34+
class SomeIterator implements \Iterator
35+
{
36+
#[\ReturnTypeWillChange]
37+
public function current()
38+
{
39+
}
40+
41+
#[\ReturnTypeWillChange]
42+
public function key()
43+
{
44+
}
45+
46+
#[\ReturnTypeWillChange]
47+
public function next()
48+
{
49+
}
50+
51+
#[\ReturnTypeWillChange]
52+
public function rewind()
53+
{
54+
}
55+
56+
#[\ReturnTypeWillChange]
57+
public function valid()
58+
{
59+
}
60+
}
61+
62+
?>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Rector\Tests\DowngradePhp81\Rector\ClassMethod\AddReturnTypeWillChangeAttributeRector\Fixture;
4+
5+
class SomeIteratorAggregate implements \IteratorAggregate
6+
{
7+
public function getIterator()
8+
{
9+
}
10+
}
11+
12+
?>
13+
-----
14+
<?php
15+
16+
namespace Rector\Tests\DowngradePhp81\Rector\ClassMethod\AddReturnTypeWillChangeAttributeRector\Fixture;
17+
18+
class SomeIteratorAggregate implements \IteratorAggregate
19+
{
20+
#[\ReturnTypeWillChange]
21+
public function getIterator()
22+
{
23+
}
24+
}
25+
26+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Rector\Tests\DowngradePhp81\Rector\ClassMethod\AddReturnTypeWillChangeAttributeRector\Fixture;
4+
5+
class ArrayAccessAlreadyAttributed implements \ArrayAccess
6+
{
7+
#[\ReturnTypeWillChange]
8+
public function offsetGet($offset)
9+
{
10+
}
11+
12+
#[\ReturnTypeWillChange]
13+
public function offsetExists($offset)
14+
{
15+
}
16+
17+
#[\ReturnTypeWillChange]
18+
public function offsetSet($offset, $value)
19+
{
20+
}
21+
22+
#[\ReturnTypeWillChange]
23+
public function offsetUnset($offset)
24+
{
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Rector\Tests\DowngradePhp81\Rector\ClassMethod\AddReturnTypeWillChangeAttributeRector\Fixture;
4+
5+
class ArrayAccessWithReturnType implements \ArrayAccess
6+
{
7+
public function offsetGet($offset): mixed
8+
{
9+
}
10+
11+
public function offsetExists($offset): bool
12+
{
13+
}
14+
15+
public function offsetSet($offset, $value): void
16+
{
17+
}
18+
19+
public function offsetUnset($offset): void
20+
{
21+
}
22+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Rector\Tests\DowngradePhp81\Rector\ClassMethod\AddReturnTypeWillChangeAttributeRector\Fixture;
4+
5+
class SomeStringable implements \Stringable
6+
{
7+
public function __toString()
8+
{
9+
}
10+
}
11+
12+
?>
13+
-----
14+
<?php
15+
16+
namespace Rector\Tests\DowngradePhp81\Rector\ClassMethod\AddReturnTypeWillChangeAttributeRector\Fixture;
17+
18+
class SomeStringable implements \Stringable
19+
{
20+
#[\ReturnTypeWillChange]
21+
public function __toString()
22+
{
23+
}
24+
}
25+
26+
?>
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\DowngradePhp81\Rector\ClassMethod\AddReturnTypeWillChangeAttributeRector;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->rule(AddReturnTypeWillChangeAttributeRector::class);
10+
};

0 commit comments

Comments
 (0)