Skip to content

Commit ff5603d

Browse files
authored
Add closure/arrow support to AddParamStringTypeFromSprintfUseRector (#7494)
* add closure support to AddParamStringTypeFromSprintfUseRector * add clarrow function fixxture * add arrow support to AddParamStringTypeFromSprintfUseRector
1 parent 1901bcb commit ff5603d

File tree

5 files changed

+85
-8
lines changed

5 files changed

+85
-8
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddParamStringTypeFromSprintfUseRector\Fixture;
4+
5+
final class AddArrowFunction
6+
{
7+
public function test()
8+
{
9+
$someClosure = fn ($name) => sprintf('Hello %s', $name);
10+
}
11+
}
12+
13+
?>
14+
-----
15+
<?php
16+
17+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddParamStringTypeFromSprintfUseRector\Fixture;
18+
19+
final class AddArrowFunction
20+
{
21+
public function test()
22+
{
23+
$someClosure = fn (string $name) => sprintf('Hello %s', $name);
24+
}
25+
}
26+
27+
?>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddParamStringTypeFromSprintfUseRector\Fixture;
4+
5+
final class CoverClosure
6+
{
7+
public function run()
8+
{
9+
$someClosure = function ($name) {
10+
return sprintf('Hello %s', $name);
11+
};
12+
13+
}
14+
}
15+
16+
?>
17+
-----
18+
<?php
19+
20+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddParamStringTypeFromSprintfUseRector\Fixture;
21+
22+
final class CoverClosure
23+
{
24+
public function run()
25+
{
26+
$someClosure = function (string $name) {
27+
return sprintf('Hello %s', $name);
28+
};
29+
30+
}
31+
}
32+
33+
?>

rules/TypeDeclaration/NodeAnalyzer/VariableInSprintfMaskMatcher.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
use Nette\Utils\Strings;
88
use PhpParser\Node\Arg;
9+
use PhpParser\Node\Expr\ArrowFunction;
10+
use PhpParser\Node\Expr\Closure;
911
use PhpParser\Node\Expr\FuncCall;
1012
use PhpParser\Node\Expr\Variable;
1113
use PhpParser\Node\Stmt\ClassMethod;
@@ -28,9 +30,18 @@ public function __construct(
2830

2931
}
3032

31-
public function matchMask(ClassMethod|Function_ $functionLike, string $variableName, string $mask): bool
32-
{
33-
$funcCalls = $this->betterNodeFinder->findInstancesOfScoped((array) $functionLike->stmts, FuncCall::class);
33+
public function matchMask(
34+
ClassMethod|Function_|Closure|ArrowFunction $functionLike,
35+
string $variableName,
36+
string $mask
37+
): bool {
38+
if ($functionLike instanceof ArrowFunction) {
39+
$stmts = [$functionLike->expr];
40+
} else {
41+
$stmts = (array) $functionLike->stmts;
42+
}
43+
44+
$funcCalls = $this->betterNodeFinder->findInstancesOfScoped($stmts, FuncCall::class);
3445
$funcCalls = array_values(
3546
array_filter($funcCalls, fn (FuncCall $funcCall): bool => $this->nodeNameResolver->isName(
3647
$funcCall->name,

rules/TypeDeclaration/Rector/ClassMethod/AddParamStringTypeFromSprintfUseRector.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
use PhpParser\Node;
88
use PhpParser\Node\Expr;
9+
use PhpParser\Node\Expr\ArrowFunction;
10+
use PhpParser\Node\Expr\Closure;
911
use PhpParser\Node\Identifier;
1012
use PhpParser\Node\Scalar\String_;
1113
use PhpParser\Node\Stmt\ClassMethod;
@@ -61,15 +63,15 @@ public function formatMessage(string $name)
6163
*/
6264
public function getNodeTypes(): array
6365
{
64-
return [ClassMethod::class, Function_::class];
66+
return [ClassMethod::class, Function_::class, Closure::class, ArrowFunction::class];
6567
}
6668

6769
/**
68-
* @param ClassMethod|Function_ $node
70+
* @param ClassMethod|Function_|Closure|ArrowFunction $node
6971
*/
70-
public function refactor(Node $node): ClassMethod|Function_|null
72+
public function refactor(Node $node): ClassMethod|Function_|Closure|ArrowFunction|null
7173
{
72-
if ($node->stmts === null) {
74+
if ($node instanceof ClassMethod && $node->stmts === null) {
7375
return null;
7476
}
7577

src/ChangesReporting/Output/GitHubOutputFormatter.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,11 @@ private function sanitizeAnnotationProperties(array $annotationProperties): stri
131131
);
132132

133133
$sanitizedProperties = array_map(
134-
fn (string $key, string|int|null $value): string => sprintf('%s=%s', $key, $this->sanitizeAnnotationProperty($value)),
134+
fn (string $key, string|int|null $value): string => sprintf(
135+
'%s=%s',
136+
$key,
137+
$this->sanitizeAnnotationProperty($value)
138+
),
135139
array_keys($nonNullProperties),
136140
$nonNullProperties
137141
);

0 commit comments

Comments
 (0)