镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

[php 8.4] Add array_find / array_any / array_all function upgrade rules #9153

Closed

Description

@TomasVotruba
$animals = ['dog', 'cat', 'cow', 'duck', 'goose'];
-$found = null;
-foreach ($animals as $animal) {
-    if (str_starts_with($animal, 'c')) {
-        $found = $animal;
-        break;
-    }
-}
+$found = array_find($animals, fn($animal) => str_starts_with($animal, 'c'));

Activity

  1. samsonasik commented on May 12, 2025

    @samsonasik
    Member

    There are other functions that exist in php 8.4:

    • array_any
    • array_all
  2. changed the title [-][php 8.4] Add array_find function upgrade rule[/-] [+][php 8.4] Add array_find / array_any / array_all function upgrade rules[/+] on Jun 20, 2025
  3. xHeaven commented on Jun 21, 2025

    @xHeaven

    array_find - rectorphp/rector-src#7009
    array_find_key - rectorphp/rector-src#7008
    array_any - rectorphp/rector-src#7010
    array_all - rectorphp/rector-src#7011

    Let me know if you need changes with either.

  4. samsonasik commented on Jun 21, 2025

    @samsonasik
    Member

    Downgrade rule(s) is needed for this so new transformed to php 8.4 array_ code can be downgraded

    https://github.com/rectorphp/rector-downgrade-php

  5. xHeaven commented on Jun 21, 2025

    @xHeaven

    Downgrade rule(s) is needed for this so new transformed to php 8.4 array_ code can be downgraded

    rectorphp/rector-downgrade-php

    I didn't even know this existed - I might file a PR with the changes. 👀

  6. samsonasik commented on Jun 21, 2025

    @samsonasik
    Member
  7. gharlan commented on Jun 26, 2025

    @gharlan

    ForeachToArrayFindKeyRector replaces code using iterable objects (instead of arrays).

    $animals = new \ArrayIterator(['dog', 'cat', 'cow', 'duck', 'goose']);
    
    $found = null;
    foreach ($animals as $idx => $animal) {
        if (str_starts_with($animal, 'c')) {
            $found = $idx;
            break;
        }
    }

    is replaced by:

    $animals = new \ArrayIterator(['dog', 'cat', 'cow', 'duck', 'goose']);
    $found = array_find_key($animals, fn($animal) => str_starts_with($animal, 'c'));

    which is invalid, because array_find_key only supports arrays.

    Did not check the other rules.

    (Note that it is not reproducible on getrector.com. But the example from ForeachToArrayFindKeyRector.com is also not working there).

  8. samsonasik commented on Jun 26, 2025

    @samsonasik
    Member

    @gharlan php version 80400 need to be set on demo page to be able to reproduce https://getrector.com/demo/89a7b249-f374-4868-b328-30a883b833be

  9. samsonasik commented on Jun 26, 2025

    @samsonasik
    Member
  10. xHeaven commented on Jun 26, 2025

    @xHeaven

    Nice catch @gharlan, I completely forgot about the existence of iterables. Oh well. Thanks @samsonasik for the quick fix!

  11. TomasVotruba commented on Jun 26, 2025

    @TomasVotruba
    MemberAuthor

    Thanks everyone for quick work 💪

  12. samsonasik commented on Jun 26, 2025

    @samsonasik
    Member

    @TomasVotruba the downgrade rules still needed, I think this can be re-open to avoid user upgrade to php 8.4, and can't downgrade to php < 8.4

  13. github-actions commented on Nov 28, 2025

    @github-actions
    Contributor

    This issue has been automatically locked because it has been closed for 150 days. Please open a new issue if you have a similar problem.

  14. locked as resolved and limited conversation to collaborators on Nov 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions