Skip to content

Commit cb51f65

Browse files
authored
[CodingStyle] Handle namespaced function string on FunctionFirstClassCallableRector (#8007)
* [CodingStyle] Handle namespaced function string on FunctionFirstClassCallableRector * fix
1 parent c2be58d commit cb51f65

2 files changed

Lines changed: 36 additions & 1 deletion

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodingStyle\Rector\FuncCall\FunctionFirstClassCallableRector\Fixture;
4+
5+
final class NamespacedFunction
6+
{
7+
public function run(): array
8+
{
9+
// String callable: resolved from the global namespace at runtime -> works.
10+
return array_map('Cake\Utility\Inflector::underscore', ['FooBar', 'BazQux']);
11+
}
12+
}
13+
14+
?>
15+
-----
16+
<?php
17+
18+
namespace Rector\Tests\CodingStyle\Rector\FuncCall\FunctionFirstClassCallableRector\Fixture;
19+
20+
final class NamespacedFunction
21+
{
22+
public function run(): array
23+
{
24+
// String callable: resolved from the global namespace at runtime -> works.
25+
return array_map(\Cake\Utility\Inflector::underscore(...), ['FooBar', 'BazQux']);
26+
}
27+
}
28+
29+
?>

rules/CodingStyle/Rector/FuncCall/FunctionFirstClassCallableRector.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PhpParser\Node\Arg;
99
use PhpParser\Node\Expr\FuncCall;
1010
use PhpParser\Node\Name;
11+
use PhpParser\Node\Name\FullyQualified;
1112
use PhpParser\Node\Scalar\String_;
1213
use PhpParser\Node\VariadicPlaceholder;
1314
use Rector\Rector\AbstractRector;
@@ -101,7 +102,12 @@ public function refactor(Node $node): ?FuncCall
101102
}
102103

103104
$node->args[$key] = new Arg(
104-
new FuncCall(new Name($arg->value->value), [new VariadicPlaceholder()]),
105+
new FuncCall(
106+
str_contains($arg->value->value, '\\')
107+
? new FullyQualified($arg->value->value)
108+
: new Name($arg->value->value),
109+
[new VariadicPlaceholder()]
110+
),
105111
name: $arg->name
106112
);
107113
$hasChanged = true;

0 commit comments

Comments
 (0)