From c739c3d92530c3a5e7a1382ee160489ec6116e4c Mon Sep 17 00:00:00 2001 From: Enzo Innocenzi Date: Sun, 11 Jan 2026 21:44:34 +0100 Subject: [PATCH] refactor(support)!: rename `Arr\map_iterable` to `Arr\map` --- .../QueryBuilders/InsertQueryBuilder.php | 4 +- .../DataTransferObjectSerializer.php | 2 +- .../PsrRequestToGenericRequestMapper.php | 2 +- packages/mail/src/Testing/MailTester.php | 8 ++-- .../src/Transports/RoundRobinMailerConfig.php | 2 +- .../src/Exceptions/HtmlExceptionRenderer.php | 2 +- .../src/Exceptions/JsonExceptionRenderer.php | 4 +- packages/support/src/Arr/ManipulatesArray.php | 2 +- packages/support/src/Arr/functions.php | 4 +- packages/upgrade/config/sets/tempest30.php | 2 + .../Tempest3/UpdateArrMapFunctionRector.php | 40 +++++++++++++++++++ .../FullyQualifiedMapIterableCall.input.php | 11 +++++ .../MapIterableNamespaceChange.input.php | 11 +++++ .../tests/Tempest3/Tempest3RectorTest.php | 16 ++++++++ packages/validation/src/Validator.php | 6 +-- 15 files changed, 98 insertions(+), 18 deletions(-) create mode 100644 packages/upgrade/src/Tempest3/UpdateArrMapFunctionRector.php create mode 100644 packages/upgrade/tests/Tempest3/Fixtures/FullyQualifiedMapIterableCall.input.php create mode 100644 packages/upgrade/tests/Tempest3/Fixtures/MapIterableNamespaceChange.input.php diff --git a/packages/database/src/Builder/QueryBuilders/InsertQueryBuilder.php b/packages/database/src/Builder/QueryBuilders/InsertQueryBuilder.php index 3710eae0e..a438e9035 100644 --- a/packages/database/src/Builder/QueryBuilders/InsertQueryBuilder.php +++ b/packages/database/src/Builder/QueryBuilders/InsertQueryBuilder.php @@ -222,7 +222,7 @@ private function addHasManyRelationCallback(string $relationName, array $relatio ? $this->removeTablePrefix($hasMany->ownerJoin) : $this->getDefaultForeignKeyName(); - $insert = Arr\map_iterable( + $insert = Arr\map( array: $relations, map: fn ($item) => $this->prepareRelationItem($item, $foreignKey, $parentId), ); @@ -306,7 +306,7 @@ private function handleStandardHasOneRelation(HasOne $hasOne, object|array $rela private function resolveData(): array { - return Arr\map_iterable( + return Arr\map( array: $this->rows, map: fn (object|iterable $model) => $this->resolveModelData($model), ); diff --git a/packages/database/src/Serializers/DataTransferObjectSerializer.php b/packages/database/src/Serializers/DataTransferObjectSerializer.php index 3f0d5f670..60162dfcf 100644 --- a/packages/database/src/Serializers/DataTransferObjectSerializer.php +++ b/packages/database/src/Serializers/DataTransferObjectSerializer.php @@ -85,7 +85,7 @@ private function serializeWithType(mixed $input): mixed } if (is_array($input)) { - return Arr\map_iterable($input, $this->serializeWithType(...)); + return Arr\map($input, $this->serializeWithType(...)); } return $input; diff --git a/packages/http/src/Mappers/PsrRequestToGenericRequestMapper.php b/packages/http/src/Mappers/PsrRequestToGenericRequestMapper.php index 98b23d1e8..0dd427855 100644 --- a/packages/http/src/Mappers/PsrRequestToGenericRequestMapper.php +++ b/packages/http/src/Mappers/PsrRequestToGenericRequestMapper.php @@ -63,7 +63,7 @@ public function map(mixed $from, mixed $to): GenericRequest 'path' => $from->getUri()->getPath(), 'query' => $query, 'files' => $uploads, - 'cookies' => Arr\filter(Arr\map_iterable( + 'cookies' => Arr\filter(Arr\map( array: $_COOKIE, map: function (string $value, string $key) { try { diff --git a/packages/mail/src/Testing/MailTester.php b/packages/mail/src/Testing/MailTester.php index a5b302362..c534347ab 100644 --- a/packages/mail/src/Testing/MailTester.php +++ b/packages/mail/src/Testing/MailTester.php @@ -92,21 +92,21 @@ public function assertNotSent(string $email): self } public array $from { - get => Arr\map_iterable( + get => Arr\map( array: $this->sentSymfonyEmail->getFrom(), map: fn (SymfonyAddress $address) => new EmailAddress($address->getAddress(), $address->getName()), ); } public array $to { - get => Arr\map_iterable( + get => Arr\map( array: $this->sentSymfonyEmail->getTo(), map: fn (SymfonyAddress $address) => new EmailAddress($address->getAddress(), $address->getName()), ); } public array $attachments { - get => Arr\map_iterable( + get => Arr\map( array: $this->sentSymfonyEmail->getAttachments(), map: fn (DataPart $attachment) => new Attachment( resolve: fn () => $attachment->getBody(), @@ -383,7 +383,7 @@ public function assertAttached(string $filename, ?Closure $callback = null): sel Assert::fail(sprintf( 'Failed asserting that the email has an attachment named `%s`. Existing attachments: %s.', $filename, - Arr\join(Arr\map_iterable($attachments, fn (DataPart $attachment) => $attachment->getName())), + Arr\join(Arr\map($attachments, fn (DataPart $attachment) => $attachment->getName())), )); return $this; diff --git a/packages/mail/src/Transports/RoundRobinMailerConfig.php b/packages/mail/src/Transports/RoundRobinMailerConfig.php index 2eaa9898a..59f4b78f4 100644 --- a/packages/mail/src/Transports/RoundRobinMailerConfig.php +++ b/packages/mail/src/Transports/RoundRobinMailerConfig.php @@ -42,6 +42,6 @@ public function createTransport(): TransportInterface /** @return TransportInterface[] */ private function buildTransports(): array { - return Arr\map_iterable($this->transports, fn (MailerConfig $config) => $config->createTransport()); + return Arr\map($this->transports, fn (MailerConfig $config) => $config->createTransport()); } } diff --git a/packages/router/src/Exceptions/HtmlExceptionRenderer.php b/packages/router/src/Exceptions/HtmlExceptionRenderer.php index af5c40582..01a2c570c 100644 --- a/packages/router/src/Exceptions/HtmlExceptionRenderer.php +++ b/packages/router/src/Exceptions/HtmlExceptionRenderer.php @@ -130,7 +130,7 @@ private function renderValidationFailedResponse(ValidationFailed $exception): Re $this->container->get(FormSession::class)->setOriginalValues($this->filterSensitiveFields($this->request, $exception->targetClass)); } - $errors = Arr\map_iterable($exception->failingRules, fn (array $failingRulesForField, string $field) => Arr\map_iterable( + $errors = Arr\map($exception->failingRules, fn (array $failingRulesForField, string $field) => Arr\map( array: $failingRulesForField, map: fn (FailingRule $rule) => $this->validator->getErrorMessage($rule, $field), )); diff --git a/packages/router/src/Exceptions/JsonExceptionRenderer.php b/packages/router/src/Exceptions/JsonExceptionRenderer.php index 6e402684c..9a3c247be 100644 --- a/packages/router/src/Exceptions/JsonExceptionRenderer.php +++ b/packages/router/src/Exceptions/JsonExceptionRenderer.php @@ -63,7 +63,7 @@ private function renderHttpRequestFailed(HttpRequestFailed $exception): Response private function renderValidationFailedResponse(ValidationFailed $exception): Response { - $errors = Arr\map_iterable($exception->failingRules, fn (array $failingRulesForField, string $field) => Arr\map_iterable( + $errors = Arr\map($exception->failingRules, fn (array $failingRulesForField, string $field) => Arr\map( array: $failingRulesForField, map: fn (FailingRule $rule) => $this->validator->getErrorMessage($rule, $field), )); @@ -94,7 +94,7 @@ private function renderErrorResponse(Status $status, ?string $message = null, ?T 'exception' => get_class($throwable), 'file' => $throwable->getFile(), 'line' => $throwable->getLine(), - 'trace' => Arr\map_iterable( + 'trace' => Arr\map( array: $throwable->getTrace(), map: fn (array $trace) => Arr\remove_keys($trace, 'args'), ), diff --git a/packages/support/src/Arr/ManipulatesArray.php b/packages/support/src/Arr/ManipulatesArray.php index 5cebb8c6b..633f4819a 100644 --- a/packages/support/src/Arr/ManipulatesArray.php +++ b/packages/support/src/Arr/ManipulatesArray.php @@ -494,7 +494,7 @@ public function each(Closure $each): self */ public function map(Closure $map): self { - return $this->createOrModify(namespace\map_iterable($this->value, $map)); + return $this->createOrModify(map($this->value, $map)); } /** diff --git a/packages/support/src/Arr/functions.php b/packages/support/src/Arr/functions.php index f320b1d4b..4a919e2c2 100644 --- a/packages/support/src/Arr/functions.php +++ b/packages/support/src/Arr/functions.php @@ -737,7 +737,7 @@ function each(iterable $array, Closure $each): array * * @return array */ -function map_iterable(iterable $array, Closure $map): array +function map(iterable $array, Closure $map): array { $result = []; @@ -1086,7 +1086,7 @@ function group_by(iterable $array, Closure $keyExtracor): array */ function flat_map(iterable $array, Closure $map, int|float $depth = 1): array { - return namespace\flatten(namespace\map_iterable(to_array($array), $map), $depth); + return namespace\flatten(map(to_array($array), $map), $depth); } /** diff --git a/packages/upgrade/config/sets/tempest30.php b/packages/upgrade/config/sets/tempest30.php index de863211f..ae7af048d 100644 --- a/packages/upgrade/config/sets/tempest30.php +++ b/packages/upgrade/config/sets/tempest30.php @@ -3,6 +3,7 @@ use Rector\Config\RectorConfig; use Rector\Configuration\Option; use Rector\Configuration\Parameter\SimpleParameterProvider; +use Tempest\Upgrade\Tempest3\UpdateArrMapFunctionRector; use Tempest\Upgrade\Tempest3\UpdateCommandFunctionImportsRector; use Tempest\Upgrade\Tempest3\UpdateContainerFunctionImportsRector; use Tempest\Upgrade\Tempest3\UpdateEventFunctionImportsRector; @@ -16,6 +17,7 @@ SimpleParameterProvider::setParameter(Option::AUTO_IMPORT_NAMES, value: true); SimpleParameterProvider::setParameter(Option::IMPORT_SHORT_CLASSES, value: true); + $config->rule(UpdateArrMapFunctionRector::class); $config->rule(UpdateCommandFunctionImportsRector::class); $config->rule(UpdateContainerFunctionImportsRector::class); $config->rule(UpdateEventFunctionImportsRector::class); diff --git a/packages/upgrade/src/Tempest3/UpdateArrMapFunctionRector.php b/packages/upgrade/src/Tempest3/UpdateArrMapFunctionRector.php new file mode 100644 index 000000000..1433c62e4 --- /dev/null +++ b/packages/upgrade/src/Tempest3/UpdateArrMapFunctionRector.php @@ -0,0 +1,40 @@ +name->toString() === 'Tempest\Support\Arr\map_iterable') { + $node->name = new Node\Name('Tempest\Support\Arr\map'); + } + + return null; + } + + if ($node instanceof Node\Expr\FuncCall && $node->name instanceof Node\Name) { + $functionName = $node->name->toString(); + + if ($functionName === 'Tempest\Support\Arr\map_iterable') { + $node->name = new Node\Name\FullyQualified('Tempest\Support\Arr\map'); + + return null; + } + } + + return null; + } +} diff --git a/packages/upgrade/tests/Tempest3/Fixtures/FullyQualifiedMapIterableCall.input.php b/packages/upgrade/tests/Tempest3/Fixtures/FullyQualifiedMapIterableCall.input.php new file mode 100644 index 000000000..f563b7bf3 --- /dev/null +++ b/packages/upgrade/tests/Tempest3/Fixtures/FullyQualifiedMapIterableCall.input.php @@ -0,0 +1,11 @@ + $item * 2); + } +} diff --git a/packages/upgrade/tests/Tempest3/Fixtures/MapIterableNamespaceChange.input.php b/packages/upgrade/tests/Tempest3/Fixtures/MapIterableNamespaceChange.input.php new file mode 100644 index 000000000..2027726c3 --- /dev/null +++ b/packages/upgrade/tests/Tempest3/Fixtures/MapIterableNamespaceChange.input.php @@ -0,0 +1,11 @@ + $item * 2); + } +} diff --git a/packages/upgrade/tests/Tempest3/Tempest3RectorTest.php b/packages/upgrade/tests/Tempest3/Tempest3RectorTest.php index d401e199d..6d938d841 100644 --- a/packages/upgrade/tests/Tempest3/Tempest3RectorTest.php +++ b/packages/upgrade/tests/Tempest3/Tempest3RectorTest.php @@ -100,4 +100,20 @@ public function test_fully_qualified_view_call(): void ->assertContains('use Tempest\View\view;') ->assertContains('return view($template);'); } + + public function test_map_iterable_namespace_change(): void + { + $this->rector + ->runFixture(__DIR__ . '/Fixtures/MapIterableNamespaceChange.input.php') + ->assertContains('use function Tempest\Support\Arr\map;') + ->assertNotContains('use function Tempest\Support\Arr\map_iterable;'); + } + + public function test_fully_qualified_map_iterable_call(): void + { + $this->rector + ->runFixture(__DIR__ . '/Fixtures/FullyQualifiedMapIterableCall.input.php') + ->assertContains('use Tempest\Support\Arr\map;') + ->assertContains('return map($data, fn ($item) => $item * 2);'); + } } diff --git a/packages/validation/src/Validator.php b/packages/validation/src/Validator.php index f753b73f4..eca86d251 100644 --- a/packages/validation/src/Validator.php +++ b/packages/validation/src/Validator.php @@ -64,8 +64,8 @@ public function createValidationFailureException(array $failingRules, null|objec return new ValidationFailed( failingRules: $failingRules, subject: $subject, - errorMessages: Arr\map_iterable($failingRules, function (array $rules, string $field) { - return Arr\map_iterable($rules, fn (FailingRule $rule) => $this->getErrorMessage($rule, $field)); + errorMessages: Arr\map($failingRules, function (array $rules, string $field) { + return Arr\map($rules, fn (FailingRule $rule) => $this->getErrorMessage($rule, $field)); }), targetClass: $targetClass, ); @@ -151,7 +151,7 @@ public function validateValueForProperty(PropertyReflector $property, mixed $val $key = $property->getAttribute(TranslationKey::class)?->key; - return Arr\map_iterable( + return Arr\map( array: $this->validateValue($value, $rules), map: fn (FailingRule $rule) => $rule->withKey($key), );