-
-
Notifications
You must be signed in to change notification settings - Fork 432
[type-declaration] Add AddReturnTypeFromTryCatchTypeRector #7099
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
85db284
[type-declaration] Add AddReturnTypeFromTryCatchTypeRector
TomasVotruba 80038cd
use ClassMethodReturnTypeOverrideGuard
TomasVotruba 652a93f
skip silent void
TomasVotruba 00db9a1
finalize classes
TomasVotruba c2d96ae
[ci] fail on missed final class
TomasVotruba File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
28 changes: 28 additions & 0 deletions
28
...assMethod/AddReturnTypeFromTryCatchTypeRector/AddReturnTypeFromTryCatchTypeRectorTest.php
This file contains hidden or 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\AddReturnTypeFromTryCatchTypeRector; | ||
|
|
||
| use Iterator; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
|
||
| final class AddReturnTypeFromTryCatchTypeRectorTest 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'; | ||
| } | ||
| } |
35 changes: 35 additions & 0 deletions
35
...n/Rector/ClassMethod/AddReturnTypeFromTryCatchTypeRector/Fixture/simple_try_catch.php.inc
This file contains hidden or 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,35 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddReturnTypeFromTryCatchTypeRector\Fixture; | ||
|
|
||
| final class SimpleTryCatch | ||
| { | ||
| public function run() | ||
| { | ||
| try { | ||
| return 1; | ||
| } catch (\Exception $e) { | ||
| return 2; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddReturnTypeFromTryCatchTypeRector\Fixture; | ||
|
|
||
| final class SimpleTryCatch | ||
| { | ||
| public function run(): int | ||
| { | ||
| try { | ||
| return 1; | ||
| } catch (\Exception $e) { | ||
| return 2; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| ?> |
23 changes: 23 additions & 0 deletions
23
...tion/Rector/ClassMethod/AddReturnTypeFromTryCatchTypeRector/Fixture/skip_doc_type.php.inc
This file contains hidden or 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\AddReturnTypeFromTryCatchTypeRector\Fixture; | ||
|
|
||
| final class SimpleTryCatch | ||
| { | ||
| public function run() | ||
| { | ||
| try { | ||
| return 1; | ||
| } catch (\Exception $e) { | ||
| return $this->getIntDoc(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @return int | ||
| */ | ||
| private function getIntDoc() | ||
| { | ||
| return mt_rand(0, 1) ? 'string' : 1; | ||
| } | ||
| } | ||
15 changes: 15 additions & 0 deletions
15
...n/Rector/ClassMethod/AddReturnTypeFromTryCatchTypeRector/Fixture/skip_if_no_catch.php.inc
This file contains hidden or 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\AddReturnTypeFromTryCatchTypeRector\Fixture; | ||
|
|
||
| final class SkipIfNoCatch | ||
| { | ||
| public function run() | ||
| { | ||
| try { | ||
| return 1; | ||
| } finally { | ||
| return 5; | ||
| } | ||
| } | ||
| } |
9 changes: 9 additions & 0 deletions
9
...ion/Rector/ClassMethod/AddReturnTypeFromTryCatchTypeRector/Fixture/skip_interface.php.inc
This file contains hidden or 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,9 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddReturnTypeFromTryCatchTypeRector\Fixture; | ||
|
|
||
| interface SkipInterface | ||
| { | ||
| public function run(); | ||
| } | ||
|
|
20 changes: 20 additions & 0 deletions
20
...on/Rector/ClassMethod/AddReturnTypeFromTryCatchTypeRector/Fixture/skip_known_type.php.inc
This file contains hidden or 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,20 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddReturnTypeFromTryCatchTypeRector\Fixture; | ||
|
|
||
| final class SkipKnownType | ||
| { | ||
| public function run(): int | ||
| { | ||
| try { | ||
|
|
||
| return 1; | ||
|
|
||
| } catch (\Exception $e) { | ||
|
|
||
| return 2; | ||
|
|
||
| } | ||
| } | ||
| } | ||
|
|
19 changes: 19 additions & 0 deletions
19
...n/Rector/ClassMethod/AddReturnTypeFromTryCatchTypeRector/Fixture/skip_silent_void.php.inc
This file contains hidden or 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,19 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddReturnTypeFromTryCatchTypeRector\Fixture; | ||
|
|
||
| final class SkipSilentVoid | ||
| { | ||
| public function run() | ||
| { | ||
| try { | ||
| if (rand(0, 1)) { | ||
| return; | ||
| } | ||
|
|
||
| return 1; | ||
| } catch (\Exception $e) { | ||
| return 2; | ||
| } | ||
| } | ||
| } |
10 changes: 10 additions & 0 deletions
10
...aration/Rector/ClassMethod/AddReturnTypeFromTryCatchTypeRector/config/configured_rule.php
This file contains hidden or 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\AddReturnTypeFromTryCatchTypeRector; | ||
|
|
||
| return static function (RectorConfig $rectorConfig): void { | ||
| $rectorConfig->rule(AddReturnTypeFromTryCatchTypeRector::class); | ||
| }; |
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.