framework: TypeError: Argument 1 passed to str_contains() must be of the type string, null given

  • Laravel Version: 6.18.16
  • PHP Version: 7.2.24
  • Database Driver & Version: MYSQL 5.7

Description:

The dependency for Symfony debug recently made a change that causes problem to laravel. str_contains function is been added in PHP v8 and Symfony added polyfill v8 dependency in recent update …but the signature is not same in laravel and if you have str_contains() with one parameter null then an error is thrown TypeError: Argument 1 passed to str_contains() must be of the type string, null given This causes a lot of our tests failing and not only… If you replace the dependency symfony/polyfill-php80 in your composer.json then update fails Uncaught Error: Call to undefined function Symfony\Component\Debug\Exception\get_debug_type() in \vendor\symfony\debug\Exception\FatalThrowableError.php:29 Stack trace: #0 \vendor\laravel\framework\src\Illuminate\Foundation\Bootstrap\HandleExceptions.php(86): Symfony\Component\Debug\Exception\FatalThrowableError->__construct(Object(Error))

Steps To Reproduce:

Use in some code or in example test the str_contains with 1 parameter null… Run the test…

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 4
  • Comments: 17 (7 by maintainers)

Most upvoted comments

Workaround is to forbid symfony/polyfill-php80 installation in composer.json

    "conflict": {
        "symfony/polyfill-php80": "*"
    }

Workaround is to forbid symfony/polyfill-php80 installation in composer.json

    "conflict": {
        "symfony/polyfill-php80": "*"
    }

I perfectly works with this str_contains conflict

I just realized that there’s nothing much we can do here. Since the symfony polyfill package gets loaded first it’ll first register its str_contains function. You’ll have to choose between the helpers package and the symfony one if you want to use str_contains.

The actual solution to your problem btw is to typecast to a string before attempting to use the function. The type error only happens because you’re passing in null. Besides the fact that the newer PHP 8 function doesn’t handles multiple needles they should be complementary.

I am on laravel 5.7. error still occurred. So I followed @ driesvints and @ lcharette suggestions. Updated my str_contains() methods(only the ones which were passing needles as an array) to use Str::contains() instead

This problem occurs too with older versions of laravel. We are working on porting over an L4 version to latest and can’t even get the vagrant dev boxes working right because of this.

As stated here, while providing the proper type is the right solution, using Str::contains might be helpful in the meantime.

Ah, nvm you’re referring to the helpers package I see. Unfortunately these indeed collide. I’ll try to look into a solution to prevent it from being registered when you’re on PHP 8.