Skip to content

Conversation

rodrigopedra
Copy link
Contributor

@rodrigopedra rodrigopedra commented Sep 1, 2025

Recently some Illuminate\Collection\Arr helpers were changed to use the new array_first and array_last functions introduced in PHP 8.5.

These changes were introduced on PR laravel/framework#56706

Although PHP 8.5 is yet to be released, these changes were possible by requiring the symfony/polyfill-php85 package.

The issue is, if a package requires the laravel/helpers package, it also defines array_first and array_last functions.

Then if laravel/helpers gets loaded before symfony/polyfill-php85 the functions defined in laravel/helpers will take precedence.

And currently the definitions of these functions will create an infinite loop, as they both are just an alias to Arr::first() and Arr::last(), and those call array_first and array_last within their implementation.

helpers/src/helpers.php

Lines 84 to 87 in bc28464

function array_first($array, ?callable $callback = null, $default = null)
{
return Arr::first($array, $callback, $default);
}

helpers/src/helpers.php

Lines 156 to 159 in bc28464

function array_last($array, ?callable $callback = null, $default = null)
{
return Arr::last($array, $callback, $default);
}

During the assessment for this PR, I noted that the str_contains() helper can also result in an infinite loop.

helpers/src/helpers.php

Lines 410 to 413 in bc28464

function str_contains($haystack, $needles)
{
return Str::contains($haystack, $needles);
}

Although the native str_contains function was first introduced in PHP 8.0, the laravel/helpers package supports PHP versions as old as PHP 7.2, so I thought of also handling this helper.

This PR

  • Copies the previous implementation of Arr::first(), Arr::last() and Str::contains() directly into the array_first, array_last and str_contains helpers to avoid an infinite loop.
  • Deprecation notes were added to these helpers' docblocks.
  • No tests were added as no new tests are being accepted.

@rodrigopedra
Copy link
Contributor Author

The infinite loop was first spotted in this comment:

laravel/framework#56706 (comment)

The user @momala454 uses the thibaud-dauce/laravel-mattermost-logger package, which requires the laravel/helpers package.

@AhmedAlaa4611
Copy link

Hello @rodrigopedra, As you said:

maybe the package doesn't need it anymore

I think the thibaud-dauce/laravel-mattermost-logger package should no longer require the laravel/helpers package. I think in this PR, we should only add the deprecated annotations or alternatively consider archiving the repository.

@rodrigopedra
Copy link
Contributor Author

I am for archiving it.

But these changes are still needed, as they are actually bug fixes for anyone using this package on recent Laravel versions.

As illuminate\support requires symfony/polyfill-php85 and relies on the newly added functions, these changes are actually bug fixes and not improvements.

@rodrigopedra
Copy link
Contributor Author

I also sent an MR to https://gitlab.com/thibauddauce/laravel-mattermost-logger to remove its dependency on laravel/helpers

@taylorotwell taylorotwell merged commit 97738f7 into laravel:master Sep 1, 2025
10 checks passed
@erikverbeek
Copy link

The array_find_key() function used in this fix breaks the array_first() helper function for PHP 8.2 & 8.3

@rodrigopedra
Copy link
Contributor Author

@erikverbeek I am working on that.

@rodrigopedra rodrigopedra mentioned this pull request Sep 2, 2025
@rodrigopedra
Copy link
Contributor Author

@erikverbeek I sent PR #38 to address this issue.

Thanks for spotting it!

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.

4 participants