Skip to content

Commit 71da7d8

Browse files
authored
add array-filter to ArrayMapOnCollectionToArrayRector (#439)
1 parent 8152f51 commit 71da7d8

3 files changed

Lines changed: 94 additions & 5 deletions

File tree

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Doctrine\Tests\TypedCollections\Rector\FuncCall\ArrayMapOnCollectionToArrayRector\Fixture;
6+
7+
use Doctrine\Common\Collections\Collection;
8+
9+
final class ArrayFilterToArray
10+
{
11+
/**
12+
* @var Collection<int, string>
13+
*/
14+
public $items;
15+
16+
public function merge()
17+
{
18+
$items = $this->items;
19+
20+
return array_filter($items, fn ($item) => $item);
21+
}
22+
}
23+
24+
?>
25+
-----
26+
<?php
27+
28+
declare(strict_types=1);
29+
30+
namespace Rector\Doctrine\Tests\TypedCollections\Rector\FuncCall\ArrayMapOnCollectionToArrayRector\Fixture;
31+
32+
use Doctrine\Common\Collections\Collection;
33+
34+
final class ArrayFilterToArray
35+
{
36+
/**
37+
* @var Collection<int, string>
38+
*/
39+
public $items;
40+
41+
public function merge()
42+
{
43+
$items = $this->items;
44+
45+
return array_filter($items->toArray(), fn ($item) => $item);
46+
}
47+
}
48+
49+
?>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Doctrine\Tests\TypedCollections\Rector\FuncCall\ArrayMapOnCollectionToArrayRector\Fixture;
6+
7+
final class SkipNonCollection
8+
{
9+
/**
10+
* @var array<int, string>
11+
*/
12+
public $items;
13+
14+
public function merge()
15+
{
16+
$items = $this->items;
17+
18+
return array_filter($items, fn ($item) => $item);
19+
}
20+
}

rules/TypedCollections/Rector/FuncCall/ArrayMapOnCollectionToArrayRector.php

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function __construct(
2525
public function getRuleDefinition(): RuleDefinition
2626
{
2727
return new RuleDefinition(
28-
'Change array_map on Collection typed property to ->toArray() call, to always provide an array',
28+
'Change array_map() and array_filter() on Collection typed property to ->toArray() call, to always provide an array',
2929
[
3030
new CodeSample(
3131
<<<'CODE_SAMPLE'
@@ -84,18 +84,38 @@ public function refactor(Node $node): ?FuncCall
8484
return null;
8585
}
8686

87-
if (! $this->isName($node->name, 'array_map')) {
88-
return null;
87+
if ($this->isName($node->name, 'array_map')) {
88+
return $this->refactorArrayMap($node);
89+
}
90+
91+
if ($this->isName($node->name, 'array_filter')) {
92+
$this->refactorArrayFilter($node);
8993
}
9094

91-
$secondArg = $node->getArgs()[1];
95+
return null;
96+
}
9297

98+
private function refactorArrayMap(FuncCall $funcCall): null|FuncCall
99+
{
100+
$secondArg = $funcCall->getArgs()[1];
93101
if (! $this->collectionTypeDetector->isCollectionType($secondArg->value)) {
94102
return null;
95103
}
96104

97105
$secondArg->value = new MethodCall($secondArg->value, 'toArray');
98106

99-
return $node;
107+
return $funcCall;
108+
}
109+
110+
private function refactorArrayFilter(FuncCall $funcCall): ?FuncCall
111+
{
112+
$firstArg = $funcCall->getArgs()[0];
113+
if (! $this->collectionTypeDetector->isCollectionType($firstArg->value)) {
114+
return null;
115+
}
116+
117+
$firstArg->value = new MethodCall($firstArg->value, 'toArray');
118+
119+
return $funcCall;
100120
}
101121
}

0 commit comments

Comments
 (0)