-
-
Notifications
You must be signed in to change notification settings - Fork 338
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TypeDeclaration] Add ReturnUnionTypeRector (#4655)
Co-authored-by: GitHub Action <actions@github.com>
- Loading branch information
1 parent
75e46c2
commit 9b03d83
Showing
17 changed files
with
368 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
...s/TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector/Fixture/nullable_union.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector\Fixture; | ||
|
||
use stdClass; | ||
|
||
final class NullableUnion | ||
{ | ||
public function run() | ||
{ | ||
if (rand(0, 1)) { | ||
return null; | ||
} | ||
|
||
return new stdClass; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector\Fixture; | ||
|
||
use stdClass; | ||
|
||
final class NullableUnion | ||
{ | ||
public function run(): ?\stdClass | ||
{ | ||
if (rand(0, 1)) { | ||
return null; | ||
} | ||
|
||
return new stdClass; | ||
} | ||
} | ||
|
||
?> |
15 changes: 15 additions & 0 deletions
15
...ts/TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector/Fixture/skip_docblock.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector\Fixture; | ||
|
||
use stdClass; | ||
|
||
final class SkipDocblock | ||
{ | ||
/** | ||
* @return null|stdClass | ||
*/ | ||
public function run() | ||
{ | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
.../TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector/Fixture/skip_mixed_null.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector\Fixture; | ||
|
||
final class SkipMixedNull | ||
{ | ||
public function run(mixed $param) | ||
{ | ||
if (rand(0, 1)) { | ||
return $param; | ||
} | ||
|
||
return null; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...eDeclaration/Rector/ClassMethod/ReturnUnionTypeRector/Fixture/skip_non_union_type.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector\Fixture; | ||
|
||
use stdClass; | ||
|
||
final class SkipNonUnionType | ||
{ | ||
public function run() | ||
{ | ||
$stdClass = new stdClass; | ||
|
||
if (rand(0, 1)) { | ||
$stdClass->foo = 1; | ||
return $stdClass; | ||
} | ||
|
||
if (rand(0, 1)) { | ||
$stdClass->bar = 1; | ||
return $stdClass; | ||
} | ||
|
||
return $stdClass; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...peDeclaration/Rector/ClassMethod/ReturnUnionTypeRector/Fixture/skip_possible_void.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector\Fixture; | ||
|
||
use stdClass; | ||
|
||
final class SkipPossibleVoid | ||
{ | ||
public function run() | ||
{ | ||
$stdClass = new stdClass; | ||
|
||
if (rand(0, 1)) { | ||
$stdClass->foo = 1; | ||
return $stdClass; | ||
} | ||
|
||
if (rand(0, 1)) { | ||
$stdClass->bar = 1; | ||
return $stdClass; | ||
} | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
...ests/TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector/Fixture/union_multi.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector\Fixture; | ||
|
||
use DateTime; | ||
use stdClass; | ||
|
||
final class UnionMulti | ||
{ | ||
public function run() | ||
{ | ||
if (rand(0, 1)) { | ||
return null; | ||
} | ||
|
||
if (rand(0, 1)) { | ||
return new DateTime('now'); | ||
} | ||
|
||
return new stdClass; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector\Fixture; | ||
|
||
use DateTime; | ||
use stdClass; | ||
|
||
final class UnionMulti | ||
{ | ||
public function run(): null|\DateTime|\stdClass | ||
{ | ||
if (rand(0, 1)) { | ||
return null; | ||
} | ||
|
||
if (rand(0, 1)) { | ||
return new DateTime('now'); | ||
} | ||
|
||
return new stdClass; | ||
} | ||
} | ||
|
||
?> |
28 changes: 28 additions & 0 deletions
28
...ts/TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector/ReturnUnionTypeRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class ReturnUnionTypeRectorTest extends AbstractRectorTestCase | ||
{ | ||
#[DataProvider('provideData')] | ||
public function test(string $filePath): void | ||
{ | ||
$this->doTestFile($filePath); | ||
} | ||
|
||
public static function provideData(): Iterator | ||
{ | ||
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...tests/TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector/config/configured_rule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(ReturnUnionTypeRector::class); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.