Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TypeDeclaration] Adds AddParamTypeForFunctionLikeWithinCallLikeArgDe…
…clarationRector rule (#5547) * Adds AddParamTypeForFunctionLikeWithinCallLikeDeclarationRector rule * Add skip test for named parameters * PHPStan fix * Adds the ability to use named parameters as well as position * Better naming of rule * Reset hasChanged state Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com> * Reject first class callable --------- Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com>
- Loading branch information
1 parent
4951acc
commit 18a8aec
Showing
12 changed files
with
471 additions
and
1 deletion.
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
28 changes: 28 additions & 0 deletions
28
...rgDeclarationRector/AddParamTypeForFunctionLikeWithinCallLikeArgDeclarationRectorTest.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\FunctionLike\AddParamTypeForFunctionLikeWithinCallLikeArgDeclarationRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class AddParamTypeForFunctionLikeWithinCallLikeArgDeclarationRectorTest 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'; | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...ike/AddParamTypeForFunctionLikeWithinCallLikeArgDeclarationRector/Fixture/fixture.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,51 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddParamTypeForFunctionLikeWithinCallLikeArgDeclarationRector\Fixture; | ||
|
||
use SomeNamespace\SomeClass; | ||
use SomeNamespace\SomeClassForNamed; | ||
|
||
SomeClass::someCall(function ($name) { | ||
return $name; | ||
}); | ||
|
||
$var = new SomeClass(); | ||
$var->someCall(function ($name) { | ||
return $name; | ||
}); | ||
|
||
(new SomeClass())->someCall(function ($name) { | ||
return $name; | ||
}); | ||
|
||
$var->someCall(fn ($name) => $name); | ||
|
||
SomeClassForNamed::someCall('a', 'b', callback: fn ($var) => $var); | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddParamTypeForFunctionLikeWithinCallLikeArgDeclarationRector\Fixture; | ||
|
||
use SomeNamespace\SomeClass; | ||
use SomeNamespace\SomeClassForNamed; | ||
|
||
SomeClass::someCall(function (string $name) { | ||
return $name; | ||
}); | ||
|
||
$var = new SomeClass(); | ||
$var->someCall(function (string $name) { | ||
return $name; | ||
}); | ||
|
||
(new SomeClass())->someCall(function (string $name) { | ||
return $name; | ||
}); | ||
|
||
$var->someCall(fn (string $name) => $name); | ||
|
||
SomeClassForNamed::someCall('a', 'b', callback: fn (string $var) => $var); | ||
|
||
?> |
19 changes: 19 additions & 0 deletions
19
...ForFunctionLikeWithinCallLikeArgDeclarationRector/Fixture/overrides_previous_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,19 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddParamTypeForFunctionLikeWithinCallLikeArgDeclarationRector\Fixture; | ||
|
||
use SomeNamespace\SomeClass; | ||
|
||
SomeClass::someCall(fn(int|string $var) => $var); | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddParamTypeForFunctionLikeWithinCallLikeArgDeclarationRector\Fixture; | ||
|
||
use SomeNamespace\SomeClass; | ||
|
||
SomeClass::someCall(fn(string $var) => $var); | ||
|
||
?> |
9 changes: 9 additions & 0 deletions
9
...ctionLikeWithinCallLikeArgDeclarationRector/Fixture/skip_if_calllike_arg_is_named.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,9 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddParamTypeForFunctionLikeWithinCallLikeArgDeclarationRector\Fixture; | ||
|
||
use Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddParamTypeForFunctionLikeWithinCallLikeArgDeclarationRector\Source\ClassForNamedParameters; | ||
|
||
SomeClass::someCall(anotherCallback: fn ($var) => $var, callback: fn($var) => $var); | ||
|
||
?> |
7 changes: 7 additions & 0 deletions
7
...inCallLikeArgDeclarationRector/Fixture/skip_if_non_functionlike_parameter_missing.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,7 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddParamTypeForFunctionLikeWithinCallLikeArgDeclarationRector\Fixture; | ||
|
||
use SomeNamespace\SomeClass; | ||
|
||
SomeClass::someCall(fn() => 'test'); |
7 changes: 7 additions & 0 deletions
7
...keArgDeclarationRector/Fixture/skip_if_non_functionlike_parameters_in_method_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,7 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddParamTypeForFunctionLikeWithinCallLikeArgDeclarationRector\Fixture; | ||
|
||
use SomeNamespace\SomeClass; | ||
|
||
SomeClass::someCall('test'); |
9 changes: 9 additions & 0 deletions
9
...FunctionLikeWithinCallLikeArgDeclarationRector/Fixture/skip_if_non_matching_class.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,9 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddParamTypeForFunctionLikeWithinCallLikeArgDeclarationRector\Fixture; | ||
|
||
use SomeNamespace\SomeClass; | ||
|
||
AnotherClass::someCall(function ($name) { | ||
return $name; | ||
}); |
9 changes: 9 additions & 0 deletions
9
...unctionLikeWithinCallLikeArgDeclarationRector/Fixture/skip_if_non_matching_method.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,9 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddParamTypeForFunctionLikeWithinCallLikeArgDeclarationRector\Fixture; | ||
|
||
use SomeNamespace\SomeClass; | ||
|
||
SomeClass::someOtherCall(function ($name) { | ||
return $name; | ||
}); |
31 changes: 31 additions & 0 deletions
31
.../AddParamTypeForFunctionLikeWithinCallLikeArgDeclarationRector/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,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use PHPStan\Type\StringType; | ||
use Rector\Config\RectorConfig; | ||
use Rector\TypeDeclaration\Rector\FunctionLike\AddParamTypeForFunctionLikeWithinCallLikeArgDeclarationRector; | ||
use Rector\TypeDeclaration\ValueObject\AddParamTypeForFunctionLikeWithinCallLikeArgDeclaration; | ||
use Rector\ValueObject\PhpVersionFeature; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig | ||
->ruleWithConfiguration(AddParamTypeForFunctionLikeWithinCallLikeArgDeclarationRector::class, [ | ||
new AddParamTypeForFunctionLikeWithinCallLikeArgDeclaration( | ||
'SomeNamespace\SomeClass', | ||
'someCall', | ||
0, | ||
0, | ||
new StringType() | ||
), | ||
new AddParamTypeForFunctionLikeWithinCallLikeArgDeclaration( | ||
'SomeNamespace\SomeClassForNamed', | ||
'someCall', | ||
'callback', | ||
0, | ||
new StringType() | ||
), | ||
]); | ||
|
||
$rectorConfig->phpVersion(PhpVersionFeature::MIXED_TYPE); | ||
}; |
Oops, something went wrong.