implode(): Passing glue string after array is deprecated. Swap the parameters
This error message appears when you run your application locally on PHP 7.4 or later, but it passes CI tests running PHP 7.3 or 7.2. The discrepancy happens because PHP 7.4 introduced a deprecation warning for implode() calls that pass the glue string after the array.
The stack trace points to /vendor/fzaninotto/faker/src/Faker/Provider/Lorem.php:95, where the code looks like this:
return implode($words, ' ') . '.';
In previous PHP versions, implode() accepted both argument orders. The function could take an array followed by a glue string, or vice versa. However, starting in PHP 7.4, passing the glue string as the second argument is deprecated. The correct order is to pass the glue string first, then the array.
Why This Happens
The fzaninotto/faker package generates fake data for testing and seeding. Its internal implementation used the older argument order in several places. When you upgraded your local PHP version to 7.4, those deprecated calls started throwing warnings. In many environments, warnings are ignored during normal execution, but they become fatal errors when error_reporting is set to include E_DEPRECATED.
Your CI environment likely runs with an older PHP version or a different error reporting configuration, which is why the same seeders work without issues there.
Fixing the Issue
You have three main options: upgrade the Faker package, patch the vendor file, or switch away from the deprecated method entirely.
Upgrade the Faker Package
The simplest solution is to update the fzaninotto/faker package to a version that has already addressed this deprecation. Check if a newer release exists that fixes the issue in its codebase. If so, run:
composer update fzaninotto/faker --with-dependencies
After updating, clear your cache and re-run your seeders. This ensures you're using updated vendor files that comply with PHP 7.4+ requirements.
Patch the Vendor File
If upgrading isn't an option or if you need to keep using an older version of Faker for compatibility reasons, you can manually patch the file causing the issue. Open vendor/fzaninotto/faker/src/Faker/Provider/Lorem.php and change line 95 from:
return implode($words, ' ') . '.';
to:
return implode(' ', $words) . '.';
You may find similar patterns elsewhere in the file or in other providers within the faker package. A quick search for implode( followed by an array variable can help you locate all instances that need fixing.
Alternative Approaches
If you're comfortable modifying your codebase, consider replacing calls to sentence() with custom logic that doesn't rely on deprecated methods. For example, you could generate a random name directly without using Faker's built-in sentence generator:
use Illuminate\Support\Str;
use Faker\Generator as Faker;
$factory->define(Category::class, function (Faker $faker) {
$name = Str::random(10); // Or use another method to generate a name
return [
'name' => $name,
'slug' => sluggify($name),
];
});
This approach avoids the deprecated implode() call altogether and gives you full control over how names are generated.
Preventing Future Issues
Always check your PHP version against the minimum requirements listed in your package's documentation. Upgrading PHP versions is a common practice for improving security and performance, but it can expose deprecated behaviors that were previously silent.
When running locally, ensure your php.ini settings do not treat deprecation warnings as fatal errors unless you're specifically debugging them. However, for production environments, always enable strict error handling to catch these issues early.
By updating dependencies or patching vendor files, you can resolve the implode() deprecation error and ensure your seeder runs smoothly across all PHP versions.
