Skip to content

Commit 2232dee

Browse files
authored
Register FunctionLikeToFirstClassCallableRector to PHP 8.1 set (#7497)
* add array map closure fixture * add usort fixture * register FirstClassCallableRector in PHP 8.1 set * apply first class callables
1 parent 8aa7a3d commit 2232dee

File tree

11 files changed

+70
-11
lines changed

11 files changed

+70
-11
lines changed

config/set/php81.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
declare(strict_types=1);
44

5+
use Rector\CodingStyle\Rector\FunctionLike\FunctionLikeToFirstClassCallableRector;
56
use Rector\Config\RectorConfig;
67
use Rector\Php81\Rector\Array_\FirstClassCallableRector;
78
use Rector\Php81\Rector\Class_\MyCLabsClassToEnumRector;
@@ -26,7 +27,10 @@
2627
SpatieEnumMethodCallToEnumConstRector::class,
2728
NullToStrictStringFuncCallArgRector::class,
2829
NullToStrictIntPregSlitFuncCallLimitArgRector::class,
30+
// array of local method call
2931
FirstClassCallableRector::class,
32+
// closure/arrow function
33+
FunctionLikeToFirstClassCallableRector::class,
3034
RemoveReflectionSetAccessibleCallsRector::class,
3135
]);
3236
};
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\CodingStyle\Rector\FunctionLike\FunctionLikeToFirstClassCallableRector\Fixture;
4+
5+
final class ArrayMap
6+
{
7+
public function call($items)
8+
{
9+
array_map(fn($x) => strlen($x), $items);
10+
}
11+
}
12+
13+
?>
14+
-----
15+
<?php
16+
17+
namespace Rector\Tests\CodingStyle\Rector\FunctionLike\FunctionLikeToFirstClassCallableRector\Fixture;
18+
19+
final class ArrayMap
20+
{
21+
public function call($items)
22+
{
23+
array_map(strlen(...), $items);
24+
}
25+
}
26+
27+
?>
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\CodingStyle\Rector\FunctionLike\FunctionLikeToFirstClassCallableRector\Fixture;
4+
5+
final class MultipleParams
6+
{
7+
public function call($items)
8+
{
9+
usort($items, fn($a, $b) => strcmp($a, $b));
10+
}
11+
}
12+
13+
?>
14+
-----
15+
<?php
16+
17+
namespace Rector\Tests\CodingStyle\Rector\FunctionLike\FunctionLikeToFirstClassCallableRector\Fixture;
18+
19+
final class MultipleParams
20+
{
21+
public function call($items)
22+
{
23+
usort($items, strcmp(...));
24+
}
25+
}
26+
27+
?>

rules/CodingStyle/Rector/FunctionLike/FunctionLikeToFirstClassCallableRector.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ final class FunctionLikeToFirstClassCallableRector extends AbstractRector
3131
public function getRuleDefinition(): RuleDefinition
3232
{
3333
return new RuleDefinition(
34-
'converts function like to first class callable',
34+
'Converts arrow function and closures to first class callable',
3535
[new CodeSample(
3636
<<<'CODE_SAMPLE'
37-
function ($parameter) { return Call::to($parameter); }
37+
function ($parameter) {
38+
return Call::to($parameter);
39+
}
3840
CODE_SAMPLE
3941
,
4042
<<<'CODE_SAMPLE'

rules/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ private function hasCallLikeInAssignExpr(Expr $expr): bool
139139
{
140140
return (bool) $this->betterNodeFinder->findFirst(
141141
$expr,
142-
fn (Node $subNode): bool => $this->sideEffectNodeDetector->detectCallExpr($subNode)
142+
$this->sideEffectNodeDetector->detectCallExpr(...)
143143
);
144144
}
145145

rules/DeadCode/SideEffect/SideEffectNodeDetector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function detect(Expr $expr): bool
5050

5151
return (bool) $this->betterNodeFinder->findFirst(
5252
$expr,
53-
fn (Node $subNode): bool => $this->detectCallExpr($subNode)
53+
$this->detectCallExpr(...)
5454
);
5555
}
5656

rules/Php81/NodeFactory/EnumFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ private function collectMappings(array $items, array $mapping): array
221221
*/
222222
private function getIdentifierTypeFromMappings(array $mapping): string
223223
{
224-
$callableGetType = static fn ($value): string => gettype($value);
224+
$callableGetType = gettype(...);
225225
$valueTypes = array_map($callableGetType, $mapping);
226226
$uniqueValueTypes = array_unique($valueTypes);
227227
if (count($uniqueValueTypes) === 1) {

src/Configuration/RectorConfigBuilder.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,7 @@ public function __invoke(RectorConfig $rectorConfig): void
191191
{
192192
if ($this->setGroups !== [] || $this->setProviders !== []) {
193193
$setProviderCollector = new SetProviderCollector(array_map(
194-
static fn (string $setProvider): SetProviderInterface =>
195-
$rectorConfig->make($setProvider),
194+
$rectorConfig->make(...),
196195
\array_keys($this->setProviders)
197196
));
198197

src/FileSystem/FileAndDirectoryFilter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ final class FileAndDirectoryFilter
1515
*/
1616
public function filterDirectories(array $filesAndDirectories): array
1717
{
18-
$directories = array_filter($filesAndDirectories, static fn (string $path): bool => is_dir($path));
18+
$directories = array_filter($filesAndDirectories, is_dir(...));
1919

2020
return array_values($directories);
2121
}
@@ -26,7 +26,7 @@ public function filterDirectories(array $filesAndDirectories): array
2626
*/
2727
public function filterFiles(array $filesAndDirectories): array
2828
{
29-
$files = array_filter($filesAndDirectories, static fn (string $path): bool => is_file($path));
29+
$files = array_filter($filesAndDirectories, is_file(...));
3030

3131
return array_values($files);
3232
}

src/FileSystem/FilesystemTweaker.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,6 @@ private function foundInGlob(string $path): array
5656
/** @var string[] $paths */
5757
$paths = (array) glob($path);
5858

59-
return array_filter($paths, static fn (string $path): bool => file_exists($path));
59+
return array_filter($paths, file_exists(...));
6060
}
6161
}

0 commit comments

Comments
 (0)