Modernize array utilities and JSON validation using PHP 8.4+ functions with polyfills by huangdijia · Pull Request #7523 · hyperf/hyperf · GitHub
Skip to content

Modernize array utilities and JSON validation using PHP 8.4+ functions with polyfills#7523

Merged
limingxinleo merged 13 commits into
hyperf:3.2from
huangdijia:3.2-add-polyfill-php
Mar 7, 2026
Merged

Modernize array utilities and JSON validation using PHP 8.4+ functions with polyfills#7523
limingxinleo merged 13 commits into
hyperf:3.2from
huangdijia:3.2-add-polyfill-php

Conversation

@huangdijia

Copy link
Copy Markdown
Member

No description provided.

@huangdijia huangdijia added this to the v3.2 milestone Sep 4, 2025
Replaced custom JSON validation logic with direct calls to json_validate in Str and ValidatesAttributes. Updated composer.json files to require symfony/polyfill-php83, php84, and php85 for improved PHP compatibility. Refactored array utility usage in Arr and Collection for consistency and performance.
Replaces array_key_exists with isset in the has and hasAny methods for improved performance and to ensure only non-null keys are considered present.
Simplifies the Arr::first method by delegating to array_first directly. Also corrects the locale string in CollectionTest from 'en_US.utf8' to 'en_US.UTF-8' for consistency.
@huangdijia huangdijia force-pushed the 3.2-add-polyfill-php branch from cec432f to 46a1de3 Compare November 4, 2025 10:14
huangdijia and others added 2 commits November 4, 2025 18:15
# Conflicts:
#	CHANGELOG-3.2.md
#	src/collection/tests/ArrTest.php
#	src/collection/tests/CollectionTest.php
Copilot AI review requested due to automatic review settings March 7, 2026 13:27

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates multiple components to rely on newer PHP built-in array/JSON helper functions and introduces Symfony polyfills so those functions are available on supported runtimes (PHP >= 8.2) without local fallback implementations.

Changes:

  • Replace manual JSON validation fallbacks with json_validate() in validation and string utilities.
  • Modernize collection/array helpers to use array_* built-ins (with new Arr helpers and accompanying tests).
  • Add Symfony polyfill dependencies and adjust CI workflow and changelog entries accordingly.

Reviewed changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/validation/src/Concerns/ValidatesAttributes.php Switch JSON validation to json_validate() only.
src/validation/composer.json Add symfony/polyfill-php83 dependency for json_validate().
src/stringable/src/Str.php Switch Str::isJson() to json_validate() only and remove old fallback code.
src/stringable/composer.json Add symfony/polyfill-php83 dependency for json_validate().
src/collection/tests/CollectionTest.php Adjust locale setup for SORT_LOCALE_STRING sort testing.
src/collection/tests/ArrTest.php Add tests for new Arr::hasAll, Arr::some, and Arr::every.
src/collection/src/Functions.php Update head() / last() helpers to use array_first() / array_last().
src/collection/src/Collection.php Use array_any / array_all / array_find_key in common collection operations.
src/collection/src/Arr.php Use array_first / array_find_key / array_any / array_all / array_is_list; add some() / every() helpers.
src/collection/composer.json Add PHP polyfill dependencies for newer array helpers.
composer.json Add Symfony polyfill dependencies at the root.
CHANGELOG-3.2.md Add PR entry and modify ## Changed section contents.
.github/workflows/test-components.yml Add steps to require Symfony packages and polyfills in the Symfony matrix job.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

run: |
composer remove friendsofphp/php-cs-fixer --dev --no-update
# symfony/*
for package in `cat composer.json|grep "symfony/"| awk -F '"' '{print $2}'`; do composer require "${package}:${{ matrix.symfony-version }}" --dev --no-update; done

Copilot AI Mar 7, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The # symfony/* loop greps all symfony/ packages from composer.json, which now includes symfony/polyfill-php*. That will attempt composer require symfony/polyfill-php83:^6.0|^7.0 and fail before the later polyfill loop runs. Filter polyfills out of the first loop (e.g., grep '"symfony/"' | grep -v 'symfony/polyfill-php') or build the package list in a way that excludes polyfills.

Suggested change
for package in `cat composer.json|grep "symfony/"| awk -F '"' '{print $2}'`; do composer require "${package}:${{ matrix.symfony-version }}" --dev --no-update; done
for package in `cat composer.json|grep "symfony/"|grep -v "symfony/polyfill-php"| awk -F '"' '{print $2}'`; do composer require "${package}:${{ matrix.symfony-version }}" --dev --no-update; done

Copilot uses AI. Check for mistakes.
// ensure the locale set successfully.
$this->assertTrue(in_array($locale, $localeArray));

setlocale(LC_COLLATE, 'en_US.UTF-8');

Copilot AI Mar 7, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setlocale(LC_COLLATE, ...$localeArray) already selects an available locale variant and the test asserts it succeeded. Immediately forcing setlocale(LC_COLLATE, 'en_US.UTF-8') can fail on systems where only en_US.utf8 is installed, potentially breaking subsequent SORT_LOCALE_STRING expectations. Use the $locale returned from the first call (or conditionally set to the available value) instead of hard-coding a specific variant.

Suggested change
setlocale(LC_COLLATE, 'en_US.UTF-8');
setlocale(LC_COLLATE, $locale);

Copilot uses AI. Check for mistakes.
Comment thread CHANGELOG-3.2.md Outdated
- [#7615](https://github.com/hyperf/hyperf/pull/7615) Renamed `$queue` property to `$pool` in `ConsumerProcess` for async-queue component.
- [#7625](https://github.com/hyperf/hyperf/pull/7625) Renamed `getQueueName()` to `getPoolName()` in async-queue component for terminology consistency.
- [#7714](https://github.com/hyperf/hyperf/pull/7714) Don't support Monolog 2.x, only 3.x is supported.
- [#7563](https://github.com/hyperf/hyperf/pull/7563) Changed the `logger` configuration structure. No newline at end of file

Copilot AI Mar 7, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change removes several existing entries from the ## Changed section (e.g., the async-queue rename and Monolog 2->3 note shown in the diff context), and they are not present elsewhere in the changelog. Unless this is intentional cleanup, it looks like an accidental deletion unrelated to the PR’s purpose; consider restoring the removed bullet items or moving them to the correct section.

Suggested change

Copilot uses AI. Check for mistakes.
@limingxinleo limingxinleo merged commit bb3a2a4 into hyperf:3.2 Mar 7, 2026
77 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants