-
-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TypeDeclaration] Add false and true in union support on ReturnUnionT…
…ypeRector (#5355) * [TypeDeclaration] Add false and true in union on ReturnUnionTypeRector * Fix * failing fixture * fixing true and false exists in union * [ci-review] Rector Rectify * fix * fixture fix * fix namespace * reduce complexity * fix phpstan --------- Co-authored-by: GitHub Action <actions@github.com>
- Loading branch information
1 parent
5c03979
commit 09c077e
Showing
11 changed files
with
204 additions
and
7 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
41 changes: 41 additions & 0 deletions
41
...ration/Rector/ClassMethod/ReturnUnionTypeRector/Fixture/true_in_union_become_bool.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,41 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector\Fixture; | ||
|
||
/** | ||
* true|othertype cannot work on <=php 8.1, ref https://3v4l.org/UJqXT | ||
*/ | ||
final class TrueInUnionBecomeBool | ||
{ | ||
public function run($value) | ||
{ | ||
if ($value) { | ||
return true; | ||
} | ||
|
||
return substr('warning', 1); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector\Fixture; | ||
|
||
/** | ||
* true|othertype cannot work on <=php 8.1, ref https://3v4l.org/UJqXT | ||
*/ | ||
final class TrueInUnionBecomeBool | ||
{ | ||
public function run($value): bool|string | ||
{ | ||
if ($value) { | ||
return true; | ||
} | ||
|
||
return substr('warning', 1); | ||
} | ||
} | ||
|
||
?> |
43 changes: 43 additions & 0 deletions
43
...n/Rector/ClassMethod/ReturnUnionTypeRector/FixtureTrueInUnion/true_false_in_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,43 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector\FixtureTrueInUnion; | ||
|
||
final class TrueFalseInUnion | ||
{ | ||
public function run($value) | ||
{ | ||
if ($value) { | ||
return true; | ||
} | ||
|
||
if (rand(0, 1)) { | ||
return false; | ||
} | ||
|
||
return substr('warning', 1); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector\FixtureTrueInUnion; | ||
|
||
final class TrueFalseInUnion | ||
{ | ||
public function run($value): bool|string | ||
{ | ||
if ($value) { | ||
return true; | ||
} | ||
|
||
if (rand(0, 1)) { | ||
return false; | ||
} | ||
|
||
return substr('warning', 1); | ||
} | ||
} | ||
|
||
?> |
41 changes: 41 additions & 0 deletions
41
...aration/Rector/ClassMethod/ReturnUnionTypeRector/FixtureTrueInUnion/true_in_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,41 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector\FixtureTrueInUnion; | ||
|
||
/** | ||
* true|othertype work on >= php 8.2, ref https://3v4l.org/UJqXT | ||
*/ | ||
final class TrueInUnion | ||
{ | ||
public function run($value) | ||
{ | ||
if ($value) { | ||
return true; | ||
} | ||
|
||
return substr('warning', 1); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector\FixtureTrueInUnion; | ||
|
||
/** | ||
* true|othertype work on >= php 8.2, ref https://3v4l.org/UJqXT | ||
*/ | ||
final class TrueInUnion | ||
{ | ||
public function run($value): true|string | ||
{ | ||
if ($value) { | ||
return true; | ||
} | ||
|
||
return substr('warning', 1); | ||
} | ||
} | ||
|
||
?> |
28 changes: 28 additions & 0 deletions
28
rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector/TrueInUnionTest.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 TrueInUnionTest extends AbstractRectorTestCase | ||
{ | ||
#[DataProvider('provideData')] | ||
public function test(string $filePath): void | ||
{ | ||
$this->doTestFile($filePath); | ||
} | ||
|
||
public static function provideData(): Iterator | ||
{ | ||
return self::yieldFilesFromDirectory(__DIR__ . '/FixtureTrueInUnion'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule_true_in_union.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
12 changes: 12 additions & 0 deletions
12
...aration/Rector/ClassMethod/ReturnUnionTypeRector/config/configured_rule_true_in_union.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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use Rector\ValueObject\PhpVersionFeature; | ||
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(ReturnUnionTypeRector::class); | ||
$rectorConfig->phpVersion(PhpVersionFeature::NULL_FALSE_TRUE_STANDALONE_TYPE); | ||
}; |
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