From 8765ffe59b6929f44f8b94d3286d1b914a9dfbbf Mon Sep 17 00:00:00 2001 From: Jeremiasz Major Date: Fri, 23 Apr 2021 14:41:34 +0200 Subject: [PATCH] [8.x] Fixes for PHP 8.1 (#37101) * fix PHP 8.1 deprecations (broadcasting) * fix PHP 8.1 deprecations (cache) * remove returns after never-return method call * fix PHP 8.1 deprecations (bus) * fix PHP 8.1 deprecations (database) * fix PHP 8.1 deprecations (validation) * use strict comparison * fix PHP 8.1 deprecations (mail) * fix PHP 8.1 deprecations (routing) --- .../Broadcasters/RedisBroadcaster.php | 6 ++--- src/Illuminate/Cache/Console/ClearCommand.php | 2 +- .../Testing/Concerns/InteractsWithRedis.php | 4 ---- src/Illuminate/Mail/MailManager.php | 2 +- .../Routing/AbstractRouteCollection.php | 7 ++++-- .../Validation/Concerns/FormatsMessages.php | 2 +- .../Concerns/ReplacesAttributes.php | 2 +- .../Validation/ValidationRuleParser.php | 2 +- tests/Bus/BusBatchTest.php | 2 +- .../DatabaseMigrationMakeCommandTest.php | 24 ++++++++++++++----- tests/Validation/ValidationValidatorTest.php | 8 +++---- 11 files changed, 36 insertions(+), 25 deletions(-) diff --git a/src/Illuminate/Broadcasting/Broadcasters/RedisBroadcaster.php b/src/Illuminate/Broadcasting/Broadcasters/RedisBroadcaster.php index 2fea183fb6a0..cf786955aeb7 100644 --- a/src/Illuminate/Broadcasting/Broadcasters/RedisBroadcaster.php +++ b/src/Illuminate/Broadcasting/Broadcasters/RedisBroadcaster.php @@ -20,16 +20,16 @@ class RedisBroadcaster extends Broadcaster /** * The Redis connection to use for broadcasting. * - * @var string + * @var ?string */ - protected $connection; + protected $connection = null; /** * The Redis key prefix. * * @var string */ - protected $prefix; + protected $prefix = ''; /** * Create a new broadcaster instance. diff --git a/src/Illuminate/Cache/Console/ClearCommand.php b/src/Illuminate/Cache/Console/ClearCommand.php index aa88964d729f..8a37b8b29c7e 100755 --- a/src/Illuminate/Cache/Console/ClearCommand.php +++ b/src/Illuminate/Cache/Console/ClearCommand.php @@ -116,7 +116,7 @@ protected function cache() */ protected function tags() { - return array_filter(explode(',', $this->option('tags'))); + return array_filter(explode(',', $this->option('tags') ?? '')); } /** diff --git a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithRedis.php b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithRedis.php index 1c679fb60197..6b17a72d8f2d 100644 --- a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithRedis.php +++ b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithRedis.php @@ -36,14 +36,10 @@ public function setUpRedis() if (! extension_loaded('redis')) { $this->markTestSkipped('The redis extension is not installed. Please install the extension to enable '.__CLASS__); - - return; } if (static::$connectionFailedOnceWithDefaultsSkip) { $this->markTestSkipped('Trying default host/port failed, please set environment variable REDIS_HOST & REDIS_PORT to enable '.__CLASS__); - - return; } foreach ($this->redisDriverProvider() as $driver) { diff --git a/src/Illuminate/Mail/MailManager.php b/src/Illuminate/Mail/MailManager.php index 7437e699c5e0..0893b0af5cfb 100644 --- a/src/Illuminate/Mail/MailManager.php +++ b/src/Illuminate/Mail/MailManager.php @@ -168,7 +168,7 @@ public function createTransport(array $config) return call_user_func($this->customCreators[$transport], $config); } - if (trim($transport) === '' || ! method_exists($this, $method = 'create'.ucfirst($transport).'Transport')) { + if (trim($transport ?? '') === '' || ! method_exists($this, $method = 'create'.ucfirst($transport).'Transport')) { throw new InvalidArgumentException("Unsupported mail transport [{$transport}]."); } diff --git a/src/Illuminate/Routing/AbstractRouteCollection.php b/src/Illuminate/Routing/AbstractRouteCollection.php index 47b418ac3ca5..7c8425e47613 100644 --- a/src/Illuminate/Routing/AbstractRouteCollection.php +++ b/src/Illuminate/Routing/AbstractRouteCollection.php @@ -199,8 +199,11 @@ protected function addToSymfonyRoutesCollection(SymfonyRouteCollection $symfonyR { $name = $route->getName(); - if (Str::endsWith($name, '.') && - ! is_null($symfonyRoutes->get($name))) { + if ( + ! is_null($name) + && Str::endsWith($name, '.') + && ! is_null($symfonyRoutes->get($name)) + ) { $name = null; } diff --git a/src/Illuminate/Validation/Concerns/FormatsMessages.php b/src/Illuminate/Validation/Concerns/FormatsMessages.php index f433a5361eec..217a67de6791 100644 --- a/src/Illuminate/Validation/Concerns/FormatsMessages.php +++ b/src/Illuminate/Validation/Concerns/FormatsMessages.php @@ -54,7 +54,7 @@ protected function getMessage($attribute, $rule) // messages out of the translator service for this validation rule. $key = "validation.{$lowerRule}"; - if ($key != ($value = $this->translator->get($key))) { + if ($key !== ($value = $this->translator->get($key))) { return $value; } diff --git a/src/Illuminate/Validation/Concerns/ReplacesAttributes.php b/src/Illuminate/Validation/Concerns/ReplacesAttributes.php index d4a47af146c4..8062ab9579f3 100644 --- a/src/Illuminate/Validation/Concerns/ReplacesAttributes.php +++ b/src/Illuminate/Validation/Concerns/ReplacesAttributes.php @@ -115,7 +115,7 @@ protected function replaceMax($message, $attribute, $rule, $parameters) */ protected function replaceMultipleOf($message, $attribute, $rule, $parameters) { - return str_replace(':value', $parameters[0], $message); + return str_replace(':value', $parameters[0] ?? '', $message); } /** diff --git a/src/Illuminate/Validation/ValidationRuleParser.php b/src/Illuminate/Validation/ValidationRuleParser.php index 3fea4005aa28..8711a2c26837 100644 --- a/src/Illuminate/Validation/ValidationRuleParser.php +++ b/src/Illuminate/Validation/ValidationRuleParser.php @@ -214,7 +214,7 @@ public static function parse($rule) */ protected static function parseArrayRule(array $rule) { - return [Str::studly(trim(Arr::get($rule, 0))), array_slice($rule, 1)]; + return [Str::studly(trim(Arr::get($rule, 0, ''))), array_slice($rule, 1)]; } /** diff --git a/tests/Bus/BusBatchTest.php b/tests/Bus/BusBatchTest.php index d502c04da2b3..36172311fb2b 100644 --- a/tests/Bus/BusBatchTest.php +++ b/tests/Bus/BusBatchTest.php @@ -377,7 +377,7 @@ public function test_options_unserialize_on_postgres($serialize, $options) 'failed_jobs' => '', 'failed_job_ids' => '[]', 'options' => $serialize, - 'created_at' => null, + 'created_at' => now()->timestamp, 'cancelled_at' => null, 'finished_at' => null, ]); diff --git a/tests/Database/DatabaseMigrationMakeCommandTest.php b/tests/Database/DatabaseMigrationMakeCommandTest.php index 432cdc245d20..d655a20927ae 100755 --- a/tests/Database/DatabaseMigrationMakeCommandTest.php +++ b/tests/Database/DatabaseMigrationMakeCommandTest.php @@ -27,7 +27,9 @@ public function testBasicCreateDumpsAutoload() $app = new Application; $app->useDatabasePath(__DIR__); $command->setLaravel($app); - $creator->shouldReceive('create')->once()->with('create_foo', __DIR__.DIRECTORY_SEPARATOR.'migrations', 'foo', true); + $creator->shouldReceive('create')->once() + ->with('create_foo', __DIR__.DIRECTORY_SEPARATOR.'migrations', 'foo', true) + ->andReturn(__DIR__.'/migrations/2021_04_23_110457_create_foo.php'); $composer->shouldReceive('dumpAutoloads')->once(); $this->runCommand($command, ['name' => 'create_foo']); @@ -42,7 +44,9 @@ public function testBasicCreateGivesCreatorProperArguments() $app = new Application; $app->useDatabasePath(__DIR__); $command->setLaravel($app); - $creator->shouldReceive('create')->once()->with('create_foo', __DIR__.DIRECTORY_SEPARATOR.'migrations', 'foo', true); + $creator->shouldReceive('create')->once() + ->with('create_foo', __DIR__.DIRECTORY_SEPARATOR.'migrations', 'foo', true) + ->andReturn(__DIR__.'/migrations/2021_04_23_110457_create_foo.php'); $this->runCommand($command, ['name' => 'create_foo']); } @@ -56,7 +60,9 @@ public function testBasicCreateGivesCreatorProperArgumentsWhenNameIsStudlyCase() $app = new Application; $app->useDatabasePath(__DIR__); $command->setLaravel($app); - $creator->shouldReceive('create')->once()->with('create_foo', __DIR__.DIRECTORY_SEPARATOR.'migrations', 'foo', true); + $creator->shouldReceive('create')->once() + ->with('create_foo', __DIR__.DIRECTORY_SEPARATOR.'migrations', 'foo', true) + ->andReturn(__DIR__.'/migrations/2021_04_23_110457_create_foo.php'); $this->runCommand($command, ['name' => 'CreateFoo']); } @@ -70,7 +76,9 @@ public function testBasicCreateGivesCreatorProperArgumentsWhenTableIsSet() $app = new Application; $app->useDatabasePath(__DIR__); $command->setLaravel($app); - $creator->shouldReceive('create')->once()->with('create_foo', __DIR__.DIRECTORY_SEPARATOR.'migrations', 'users', true); + $creator->shouldReceive('create')->once() + ->with('create_foo', __DIR__.DIRECTORY_SEPARATOR.'migrations', 'users', true) + ->andReturn(__DIR__.'/migrations/2021_04_23_110457_create_foo.php'); $this->runCommand($command, ['name' => 'create_foo', '--create' => 'users']); } @@ -84,7 +92,9 @@ public function testBasicCreateGivesCreatorProperArgumentsWhenCreateTablePattern $app = new Application; $app->useDatabasePath(__DIR__); $command->setLaravel($app); - $creator->shouldReceive('create')->once()->with('create_users_table', __DIR__.DIRECTORY_SEPARATOR.'migrations', 'users', true); + $creator->shouldReceive('create')->once() + ->with('create_users_table', __DIR__.DIRECTORY_SEPARATOR.'migrations', 'users', true) + ->andReturn(__DIR__.'/migrations/2021_04_23_110457_create_users_table.php'); $this->runCommand($command, ['name' => 'create_users_table']); } @@ -98,7 +108,9 @@ public function testCanSpecifyPathToCreateMigrationsIn() $app = new Application; $command->setLaravel($app); $app->setBasePath('/home/laravel'); - $creator->shouldReceive('create')->once()->with('create_foo', '/home/laravel/vendor/laravel-package/migrations', 'users', true); + $creator->shouldReceive('create')->once() + ->with('create_foo', '/home/laravel/vendor/laravel-package/migrations', 'users', true) + ->andReturn('/home/laravel/vendor/laravel-package/migrations/2021_04_23_110457_create_foo.php'); $this->runCommand($command, ['name' => 'create_foo', '--path' => 'vendor/laravel-package/migrations', '--create' => 'users']); } diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index 7d592324e47c..ca66126fb5d0 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -770,7 +770,7 @@ public function testValidatePassword() $container->shouldReceive('make')->with('hash')->andReturn($hasher); $trans = $this->getTranslator(); - $trans->shouldReceive('get'); + $trans->shouldReceive('get')->andReturnArg(0); $v = new Validator($trans, ['password' => 'foo'], ['password' => 'password']); $v->setContainer($container); @@ -794,7 +794,7 @@ public function testValidatePassword() $container->shouldReceive('make')->with('hash')->andReturn($hasher); $trans = $this->getTranslator(); - $trans->shouldReceive('get'); + $trans->shouldReceive('get')->andReturnArg(0); $v = new Validator($trans, ['password' => 'foo'], ['password' => 'password']); $v->setContainer($container); @@ -818,7 +818,7 @@ public function testValidatePassword() $container->shouldReceive('make')->with('hash')->andReturn($hasher); $trans = $this->getTranslator(); - $trans->shouldReceive('get'); + $trans->shouldReceive('get')->andReturnArg(0); $v = new Validator($trans, ['password' => 'foo'], ['password' => 'password']); $v->setContainer($container); @@ -842,7 +842,7 @@ public function testValidatePassword() $container->shouldReceive('make')->with('hash')->andReturn($hasher); $trans = $this->getTranslator(); - $trans->shouldReceive('get'); + $trans->shouldReceive('get')->andReturnArg(0); $v = new Validator($trans, ['password' => 'foo'], ['password' => 'password:custom']); $v->setContainer($container);