-
-
Notifications
You must be signed in to change notification settings - Fork 336
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement ReturnTypeFromStrictParamRector (#4501)
* Implement ReturnTypeFromStrictParamRector * more tests * Create skip_closure.php.inc * Update skip_closure.php.inc * fix * more tests * fix names * more tests, symplify * skip union return types * simplify * skip magic * Update skip_closure.php.inc * cleanup * fix * Update type-declaration.php * Create skip_already_return_typed.inc * Update skip_already_return_typed.inc * cover indirect modification by reference * Update skip_assign_by_ref.php.inc * simplify Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com> * earlier $node->stmts check * fix * Revert "fix" This reverts commit 82107dd. * Revert "earlier $node->stmts check" This reverts commit 6d61b59. * fix phpstan --------- Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com>
- Loading branch information
1 parent
b3c4aff
commit 875b2a3
Showing
21 changed files
with
536 additions
and
0 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
23 changes: 23 additions & 0 deletions
23
...ypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictParamRector/Fixture/example.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\ReturnTypeFromStrictParamRector\Fixture; | ||
|
||
class Example { | ||
public function doFoo(Example $param) { | ||
return $param; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictParamRector\Fixture; | ||
|
||
class Example { | ||
public function doFoo(Example $param): Example { | ||
return $param; | ||
} | ||
} | ||
|
||
?> |
23 changes: 23 additions & 0 deletions
23
...ation/Rector/ClassMethod/ReturnTypeFromStrictParamRector/Fixture/example_nullable.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\ReturnTypeFromStrictParamRector\Fixture; | ||
|
||
class ExampleNullable { | ||
public function doFoo(?ExampleNullable $param) { | ||
return $param; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictParamRector\Fixture; | ||
|
||
class ExampleNullable { | ||
public function doFoo(?ExampleNullable $param): ?ExampleNullable { | ||
return $param; | ||
} | ||
} | ||
|
||
?> |
23 changes: 23 additions & 0 deletions
23
...aration/Rector/ClassMethod/ReturnTypeFromStrictParamRector/Fixture/example_scalar.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\ReturnTypeFromStrictParamRector\Fixture; | ||
|
||
class ExampleInt { | ||
public function doFoo(int $param) { | ||
return $param; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictParamRector\Fixture; | ||
|
||
class ExampleInt { | ||
public function doFoo(int $param): int { | ||
return $param; | ||
} | ||
} | ||
|
||
?> |
31 changes: 31 additions & 0 deletions
31
...tion/Rector/ClassMethod/ReturnTypeFromStrictParamRector/Fixture/example_with_call.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,31 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictParamRector\Fixture; | ||
|
||
class ExampleWithCall { | ||
public function doFoo(ExampleWithCall $param) { | ||
$this->doBar($param); | ||
|
||
return $param; | ||
} | ||
|
||
public function doBar($ref) {} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictParamRector\Fixture; | ||
|
||
class ExampleWithCall { | ||
public function doFoo(ExampleWithCall $param): ExampleWithCall { | ||
$this->doBar($param); | ||
|
||
return $param; | ||
} | ||
|
||
public function doBar($ref) {} | ||
} | ||
|
||
?> |
12 changes: 12 additions & 0 deletions
12
...tion/Rector/ClassMethod/ReturnTypeFromStrictParamRector/Fixture/skill_null_return.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,12 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictParamRector\Fixture; | ||
|
||
class SkipNullReturn { | ||
public function doFoo(SkipNullReturn $param) { | ||
if (rand(0,1)) { | ||
return null; | ||
} | ||
return $param; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
.../Rector/ClassMethod/ReturnTypeFromStrictParamRector/Fixture/skip_already_return_typed.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,10 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictParamRector\Fixture; | ||
|
||
class SkipAlreadyReturnTyped { | ||
public function doFoo(SkipAlreadyReturnTyped $param): int { | ||
return $param; | ||
} | ||
} | ||
|
12 changes: 12 additions & 0 deletions
12
...eclaration/Rector/ClassMethod/ReturnTypeFromStrictParamRector/Fixture/skip_assign.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,12 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictParamRector\Fixture; | ||
|
||
class SkipAssign { | ||
public function doFoo(SkipAssign $param) { | ||
$param = 4; | ||
|
||
return $param; | ||
} | ||
} | ||
|
14 changes: 14 additions & 0 deletions
14
...ion/Rector/ClassMethod/ReturnTypeFromStrictParamRector/Fixture/skip_assign_by_ref.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,14 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictParamRector\Fixture; | ||
|
||
class SkipAssignByRef { | ||
public function doFoo(SkipAssignByRef $param) { | ||
$z = &$param; | ||
|
||
$z = 5; | ||
|
||
return $param; | ||
} | ||
} | ||
|
14 changes: 14 additions & 0 deletions
14
...ation/Rector/ClassMethod/ReturnTypeFromStrictParamRector/Fixture/skip_call_by_ref.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,14 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictParamRector\Fixture; | ||
|
||
class SkipCallbByRef { | ||
public function doFoo(SkipCallbByRef $param) { | ||
$this->doBar($param); | ||
|
||
return $param; | ||
} | ||
|
||
public function doBar(&$ref) {} | ||
} | ||
|
14 changes: 14 additions & 0 deletions
14
...claration/Rector/ClassMethod/ReturnTypeFromStrictParamRector/Fixture/skip_closure.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,14 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictParamRector\Fixture; | ||
|
||
class SkipClosure { | ||
public function doFoo() { | ||
// we have other rules which add closure return types | ||
// leave it as is | ||
$c = function (SkipClosure $param) { | ||
return $param; | ||
}; | ||
return $c; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...ector/ClassMethod/ReturnTypeFromStrictParamRector/Fixture/skip_conditional_return.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,14 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictParamRector\Fixture; | ||
|
||
class SkipConditionalReturn { | ||
public function doFoo(SkipAssign $param) { | ||
if (rand(0,1)) { | ||
return 4; | ||
} | ||
|
||
return $param; | ||
} | ||
} | ||
|
11 changes: 11 additions & 0 deletions
11
...Declaration/Rector/ClassMethod/ReturnTypeFromStrictParamRector/Fixture/skip_magic.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,11 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictParamRector\Fixture; | ||
|
||
class SkipMagic | ||
{ | ||
public function __get(string $name) | ||
{ | ||
return $name; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
.../Rector/ClassMethod/ReturnTypeFromStrictParamRector/Fixture/skip_multiple_returns.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\ReturnTypeFromStrictParamRector\Fixture; | ||
|
||
class SkipMultipleReturns { | ||
public function doFoo(SkipMultipleReturns $param, B $param2) { | ||
if (rand(0,1)) { | ||
return $param2; | ||
} | ||
|
||
return $param; | ||
} | ||
} | ||
|
||
class B {} |
15 changes: 15 additions & 0 deletions
15
...Rector/ClassMethod/ReturnTypeFromStrictParamRector/Fixture/skip_parent_overridden.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\ReturnTypeFromStrictParamRector\Fixture; | ||
|
||
class SkipParentOverridden extends SkipParentOverriddenBase { | ||
public function doFoo(SkipParentOverridden $param) { | ||
return $param; | ||
} | ||
} | ||
|
||
|
||
class SkipParentOverriddenBase { | ||
public function doFoo(SkipParentOverridden $param) { | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...eclaration/Rector/ClassMethod/ReturnTypeFromStrictParamRector/Fixture/skip_phpdoc.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,13 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictParamRector\Fixture; | ||
|
||
class SkipPhpdoc { | ||
/** | ||
* @param SkipPhpdoc $param | ||
*/ | ||
public function doFoo($param) { | ||
return $param; | ||
} | ||
} | ||
|
14 changes: 14 additions & 0 deletions
14
...Declaration/Rector/ClassMethod/ReturnTypeFromStrictParamRector/Fixture/skip_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,14 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictParamRector\Fixture; | ||
|
||
class SkipUnion | ||
{ | ||
public function run(A|B $p) | ||
{ | ||
return $p; | ||
} | ||
} | ||
|
||
class A {} | ||
class B {} |
13 changes: 13 additions & 0 deletions
13
...eDeclaration/Rector/ClassMethod/ReturnTypeFromStrictParamRector/Fixture/skip_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,13 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictParamRector\Fixture; | ||
|
||
class SkipPossibleVoid | ||
{ | ||
public function run(int $p) | ||
{ | ||
if (rand(0, 1)) { | ||
return $p; | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...ector/ClassMethod/ReturnTypeFromStrictParamRector/ReturnTypeFromStrictParamRectorTest.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\ReturnTypeFromStrictParamRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class ReturnTypeFromStrictParamRectorTest 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'; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...Declaration/Rector/ClassMethod/ReturnTypeFromStrictParamRector/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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use Rector\Core\ValueObject\PhpVersion; | ||
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictParamRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(ReturnTypeFromStrictParamRector::class); | ||
$rectorConfig->phpVersion(PhpVersion::PHP_74); | ||
}; |
Oops, something went wrong.