Skip to content

Commit dc3850f

Browse files
TomasVotrubapeterfoxactions-user
authored
[TypeDeclaration] Adds AddClosureParamTypeFromArgRector (#6258)
* Allow for dynamic type generation for FunctionLike type declaration * Allows for dynamic hinting * Split of rules * Fixes and improvements * Refactor and rename of the rule * Adds a rule to handle class strings exclusively * Feedback improvement * cleanup node detection to improve types, use only int keys to keep it simple * simplify object detection * use exact types over non-existing, to enable reflection and class discovery * merge into AddClosureParamTypeFromArgRector as same functionality * cs * [ci-review] Rector Rectify --------- Co-authored-by: Peter Fox <[email protected]> Co-authored-by: GitHub Action <[email protected]>
1 parent 314861b commit dc3850f

20 files changed

+483
-16
lines changed

rector.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
declare(strict_types=1);
44

5+
use Rector\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedStrictParamTypeRector;
56
use Rector\CodingStyle\Rector\String_\UseClassKeywordForClassNameResolutionRector;
67
use Rector\Config\RectorConfig;
78
use Rector\DeadCode\Rector\ConstFetch\RemovePhpVersionIdCheckRector;
9+
use Rector\Naming\Rector\ClassMethod\RenameParamToMatchTypeRector;
810
use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector;
911

1012
return RectorConfig::configure()
@@ -46,6 +48,14 @@
4648
'*/Source*',
4749
'*/Expected/*',
4850

51+
// stmts aware type
52+
RenameParamToMatchTypeRector::class => [
53+
__DIR__ . '/rules/DeadCode/Rector/If_/RemoveUnusedNonEmptyArrayBeforeForeachRector.php',
54+
],
55+
AddMethodCallBasedStrictParamTypeRector::class => [
56+
__DIR__ . '/rules/DeadCode/Rector/If_/RemoveUnusedNonEmptyArrayBeforeForeachRector.php',
57+
],
58+
4959
// keep configs untouched, as the classes are just strings
5060
UseClassKeywordForClassNameResolutionRector::class => [__DIR__ . '/config', '*/config/*'],
5161

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddClosureParamTypeFromArgRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class AddClosureParamTypeFromArgRectorTest extends AbstractRectorTestCase
12+
{
13+
#[DataProvider('provideData')]
14+
public function test(string $filePath): void
15+
{
16+
$this->doTestFile($filePath);
17+
}
18+
19+
public static function provideData(): Iterator
20+
{
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
22+
}
23+
24+
public function provideConfigFilePath(): string
25+
{
26+
return __DIR__ . '/config/configured_rule.php';
27+
}
28+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddClosureParamTypeFromArgRector\Fixture;
4+
5+
use Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddClosureParamTypeFromArgRector\Source\SimpleContainer;
6+
7+
SimpleContainer::someCall(\Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddClosureParamTypeFromArgRector\Source\SomeType::class, function ($object) {
8+
return $object;
9+
});
10+
11+
?>
12+
-----
13+
<?php
14+
15+
namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddClosureParamTypeFromArgRector\Fixture;
16+
17+
use Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddClosureParamTypeFromArgRector\Source\SimpleContainer;
18+
19+
SimpleContainer::someCall(\Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddClosureParamTypeFromArgRector\Source\SomeType::class, function (\Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddClosureParamTypeFromArgRector\Source\SomeType $object) {
20+
return $object;
21+
});
22+
23+
?>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddClosureParamTypeFromArgRector\Fixture;
4+
5+
use Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddClosureParamTypeFromArgRector\Source\SimpleContainer;
6+
use Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddClosureParamTypeFromArgRector\Source\SomeType;
7+
8+
SimpleContainer::someCall(SomeType::class, fn(object $var) => $var);
9+
10+
?>
11+
-----
12+
<?php
13+
14+
namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddClosureParamTypeFromArgRector\Fixture;
15+
16+
use Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddClosureParamTypeFromArgRector\Source\SimpleContainer;
17+
use Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddClosureParamTypeFromArgRector\Source\SomeType;
18+
19+
SimpleContainer::someCall(SomeType::class, fn(\Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddClosureParamTypeFromArgRector\Source\SomeType $var) => $var);
20+
21+
?>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddClosureParamTypeFromArgRector\Fixture;
4+
5+
use Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddClosureParamTypeFromArgRector\Source\SimpleContainer;
6+
7+
SimpleContainer::someCall('test', function ($object) {
8+
return $object;
9+
});
10+
11+
?>
12+
-----
13+
<?php
14+
15+
namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddClosureParamTypeFromArgRector\Fixture;
16+
17+
use Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddClosureParamTypeFromArgRector\Source\SimpleContainer;
18+
19+
SimpleContainer::someCall('test', function (string $object) {
20+
return $object;
21+
});
22+
23+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddClosureParamTypeFromArgRector\Fixture;
4+
5+
use Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddClosureParamTypeFromArgRector\Source\SimpleContainer;
6+
7+
SimpleContainer::someCall(anotherCallback: fn ($var) => $var, callback: fn($var) => $var);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddClosureParamTypeFromArgRector\Fixture;
4+
5+
use Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddClosureParamTypeFromArgRector\Source\SimpleContainer;
6+
7+
SimpleContainer::someCall(fn() => 'test');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddClosureParamTypeFromArgRector\Fixture;
4+
5+
use Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddClosureParamTypeFromArgRector\Source\SimpleContainer;
6+
7+
SimpleContainer::someCall('test');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddClosureParamTypeFromArgRector\Fixture;
4+
5+
use Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddClosureParamTypeFromArgRector\Source\NonTargetedClass;
6+
7+
NonTargetedClass::someCall(function ($name) {
8+
return $name;
9+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddClosureParamTypeFromArgRector\Fixture;
4+
5+
use Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddClosureParamTypeFromArgRector\Source\SimpleContainer;
6+
7+
SimpleContainer::someOtherCall(function ($name) {
8+
return $name;
9+
});

0 commit comments

Comments
 (0)