diff --git a/core/modules/file/src/FileUsage/DatabaseFileUsageBackend.php b/core/modules/file/src/FileUsage/DatabaseFileUsageBackend.php index 7004a59845..3577eb25a2 100644 --- a/core/modules/file/src/FileUsage/DatabaseFileUsageBackend.php +++ b/core/modules/file/src/FileUsage/DatabaseFileUsageBackend.php @@ -28,29 +28,29 @@ class DatabaseFileUsageBackend extends FileUsageBase { /** * Construct the DatabaseFileUsageBackend. * - * @param \Drupal\Core\Config\ConfigFactoryInterface|mixed $config_factory + * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory * The config factory. - * @param \Drupal\Core\Database\Connection|mixed $connection + * @param \Drupal\Core\Database\Connection $connection * The database connection which will be used to store the file usage * information. - * @param string|mixed $table + * @param string $table * (optional) The table to store file usage info. Defaults to 'file_usage'. * - * @todo Remove |mixed from the docblock above and properly type-hint the - * constructor arguments in + * @todo Properly type-hint the constructor arguments in * https://www.drupal.org/project/drupal/issues/3070114 when the * drupal:9.0.x branch is opened. */ + // @codingStandardsIgnoreLine public function __construct($config_factory, $connection = NULL, $table = 'file_usage') { // @todo Remove below conditional when the drupal:9.0.x branch is opened. // @see https://www.drupal.org/project/drupal/issues/3070114 - if ($config_factory instanceof Connection) { + if (!$config_factory instanceof ConfigFactoryInterface) { @trigger_error('Passing the database connection as the first argument to ' . __METHOD__ . ' is deprecated in drupal:8.8.0 and will throw a fatal error in drupal:9.0.0. Pass the config factory first. See https://www.drupal.org/node/3070148', E_USER_DEPRECATED); - $temp = $table; - $table = $connection; - $connection = $config_factory; - $config_factory = $temp; + if (!$config_factory instanceof Connection) { + throw new \InvalidArgumentException("The first argument to " . __METHOD__ . " should be an instance of \Drupal\Core\Config\ConfigFactoryInterface, " . gettype($config_factory) . " given."); + } + list($connection, $table, $config_factory) = array_pad(func_get_args(), 3, NULL); if (NULL === $table) { $table = 'file_usage'; } @@ -60,7 +60,6 @@ public function __construct($config_factory, $connection = NULL, $table = 'file_ } parent::__construct($config_factory); - $this->connection = $connection; $this->tableName = $table; } diff --git a/core/modules/file/tests/src/Unit/LegacyFileTest.php b/core/modules/file/tests/src/Unit/LegacyFileTest.php index 3da63c614c..0c412d1626 100644 --- a/core/modules/file/tests/src/Unit/LegacyFileTest.php +++ b/core/modules/file/tests/src/Unit/LegacyFileTest.php @@ -71,6 +71,12 @@ public function testDatabaseFileUsageBackendConstruct() { $this->assertSame($this->configFactory, $reflection_config->getValue($database_file_usage)); $this->assertSame($connection, $reflection_connection->getValue($database_file_usage)); $this->assertSame('file_usage', $reflection_table_name->getValue($database_file_usage)); + + $database_file_usage_test_table = new DatabaseFileUsageBackend($connection, 'test_table'); + $this->assertSame('test_table', $reflection_table_name->getValue($database_file_usage_test_table)); + + $this->expectException(\InvalidArgumentException::class); + $database_file_usage_exception = new DatabaseFileUsageBackend('Invalid Argument'); } }