diff --git a/rules-tests/TypedCollections/Rector/FuncCall/ArrayMapOnCollectionToArrayRector/Fixture/array_filter_to_array.php.inc b/rules-tests/TypedCollections/Rector/FuncCall/ArrayMapOnCollectionToArrayRector/Fixture/array_filter_to_array.php.inc new file mode 100644 index 00000000..1c44236e --- /dev/null +++ b/rules-tests/TypedCollections/Rector/FuncCall/ArrayMapOnCollectionToArrayRector/Fixture/array_filter_to_array.php.inc @@ -0,0 +1,49 @@ + + */ + public $items; + + public function merge() + { + $items = $this->items; + + return array_filter($items, fn ($item) => $item); + } +} + +?> +----- + + */ + public $items; + + public function merge() + { + $items = $this->items; + + return array_filter($items->toArray(), fn ($item) => $item); + } +} + +?> diff --git a/rules-tests/TypedCollections/Rector/FuncCall/ArrayMapOnCollectionToArrayRector/Fixture/skip_non_collection.php.inc b/rules-tests/TypedCollections/Rector/FuncCall/ArrayMapOnCollectionToArrayRector/Fixture/skip_non_collection.php.inc new file mode 100644 index 00000000..355b89ff --- /dev/null +++ b/rules-tests/TypedCollections/Rector/FuncCall/ArrayMapOnCollectionToArrayRector/Fixture/skip_non_collection.php.inc @@ -0,0 +1,20 @@ + + */ + public $items; + + public function merge() + { + $items = $this->items; + + return array_filter($items, fn ($item) => $item); + } +} diff --git a/rules/TypedCollections/Rector/FuncCall/ArrayMapOnCollectionToArrayRector.php b/rules/TypedCollections/Rector/FuncCall/ArrayMapOnCollectionToArrayRector.php index f3a1cda8..ae07e51a 100644 --- a/rules/TypedCollections/Rector/FuncCall/ArrayMapOnCollectionToArrayRector.php +++ b/rules/TypedCollections/Rector/FuncCall/ArrayMapOnCollectionToArrayRector.php @@ -25,7 +25,7 @@ public function __construct( public function getRuleDefinition(): RuleDefinition { return new RuleDefinition( - 'Change array_map on Collection typed property to ->toArray() call, to always provide an array', + 'Change array_map() and array_filter() on Collection typed property to ->toArray() call, to always provide an array', [ new CodeSample( <<<'CODE_SAMPLE' @@ -84,18 +84,38 @@ public function refactor(Node $node): ?FuncCall return null; } - if (! $this->isName($node->name, 'array_map')) { - return null; + if ($this->isName($node->name, 'array_map')) { + return $this->refactorArrayMap($node); + } + + if ($this->isName($node->name, 'array_filter')) { + $this->refactorArrayFilter($node); } - $secondArg = $node->getArgs()[1]; + return null; + } + private function refactorArrayMap(FuncCall $funcCall): null|FuncCall + { + $secondArg = $funcCall->getArgs()[1]; if (! $this->collectionTypeDetector->isCollectionType($secondArg->value)) { return null; } $secondArg->value = new MethodCall($secondArg->value, 'toArray'); - return $node; + return $funcCall; + } + + private function refactorArrayFilter(FuncCall $funcCall): ?FuncCall + { + $firstArg = $funcCall->getArgs()[0]; + if (! $this->collectionTypeDetector->isCollectionType($firstArg->value)) { + return null; + } + + $firstArg->value = new MethodCall($firstArg->value, 'toArray'); + + return $funcCall; } }