Skip to content

Commit

Permalink
[TypeDeclaration] Adds AddParamTypeForFunctionLikeWithinCallLikeArgDe…
Browse files Browse the repository at this point in the history
…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
peterfox and samsonasik committed Feb 22, 2024
1 parent 4951acc commit 18a8aec
Show file tree
Hide file tree
Showing 12 changed files with 471 additions and 1 deletion.
17 changes: 16 additions & 1 deletion build/target-repository/docs/rector_rules_overview.md
Expand Up @@ -56,7 +56,7 @@

- [Transform](#transform) (23)

- [TypeDeclaration](#typedeclaration) (43)
- [TypeDeclaration](#typedeclaration) (44)

- [Visibility](#visibility) (3)

Expand Down Expand Up @@ -6406,6 +6406,21 @@ Add param types where needed

<br>

### AddParamTypeForFunctionLikeWithinCallLikeArgDeclarationRector

Add param types where needed

:wrench: **configure it!**

- class: [`Rector\TypeDeclaration\Rector\FunctionLike\AddParamTypeForFunctionLikeWithinCallLikeArgDeclarationRector`](../rules/TypeDeclaration/Rector/FunctionLike/AddParamTypeForFunctionLikeWithinCallLikeArgDeclarationRector.php)

```diff
-(new SomeClass)->process(function ($parameter) {});
+(new SomeClass)->process(function (string $parameter) {});
```

<br>

### AddParamTypeFromPropertyTypeRector

Adds param type declaration based on property type the value is assigned to PHPUnit provider return type declaration
Expand Down
@@ -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';
}
}
@@ -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);

?>
@@ -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);

?>
@@ -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);

?>
@@ -0,0 +1,7 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddParamTypeForFunctionLikeWithinCallLikeArgDeclarationRector\Fixture;

use SomeNamespace\SomeClass;

SomeClass::someCall(fn() => 'test');
@@ -0,0 +1,7 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddParamTypeForFunctionLikeWithinCallLikeArgDeclarationRector\Fixture;

use SomeNamespace\SomeClass;

SomeClass::someCall('test');
@@ -0,0 +1,9 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddParamTypeForFunctionLikeWithinCallLikeArgDeclarationRector\Fixture;

use SomeNamespace\SomeClass;

AnotherClass::someCall(function ($name) {
return $name;
});
@@ -0,0 +1,9 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddParamTypeForFunctionLikeWithinCallLikeArgDeclarationRector\Fixture;

use SomeNamespace\SomeClass;

SomeClass::someOtherCall(function ($name) {
return $name;
});
@@ -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);
};

0 comments on commit 18a8aec

Please sign in to comment.