From 5291b4562ae9478cc6056e4c8baa0e750acbec2c Mon Sep 17 00:00:00 2001 From: guvra Date: Tue, 14 Jan 2025 11:06:57 +0100 Subject: [PATCH] Add type casting to values retrived from the configuration --- .../Processor/DumpOutputProcessor.php | 19 ++++++++++--------- .../Compiler/Processor/EnvVarProcessor.php | 1 + src/Console/Command/DumpCommand.php | 4 ++-- src/Database/DatabaseFactory.php | 2 +- src/Dumper/Config/ConfigProcessor.php | 4 ++-- src/Dumper/Config/DumperConfig.php | 15 ++++++++------- 6 files changed, 24 insertions(+), 21 deletions(-) diff --git a/src/Config/Compiler/Processor/DumpOutputProcessor.php b/src/Config/Compiler/Processor/DumpOutputProcessor.php index 36c74aeb..e8a059b2 100644 --- a/src/Config/Compiler/Processor/DumpOutputProcessor.php +++ b/src/Config/Compiler/Processor/DumpOutputProcessor.php @@ -13,16 +13,17 @@ final class DumpOutputProcessor implements ProcessorInterface */ public function process(ConfigInterface $config): void { - $dumpSettings = $config->get('dump', []); + $dumpSettings = (array) $config->get('dump', []); + if (!array_key_exists('output', $dumpSettings)) { + return; + } - if (array_key_exists('output', $dumpSettings)) { - $dumpSettings['output'] = preg_replace_callback( - '/{([^}]+)}/', - fn (array $matches) => date($matches[1]), - $dumpSettings['output'] - ); + $dumpSettings['output'] = preg_replace_callback( + '/{([^}]+)}/', + fn (array $matches) => date($matches[1]), + $dumpSettings['output'] + ); - $config->set('dump', $dumpSettings); - } + $config->set('dump', $dumpSettings); } } diff --git a/src/Config/Compiler/Processor/EnvVarProcessor.php b/src/Config/Compiler/Processor/EnvVarProcessor.php index b7ebea4e..9da13a4c 100644 --- a/src/Config/Compiler/Processor/EnvVarProcessor.php +++ b/src/Config/Compiler/Processor/EnvVarProcessor.php @@ -84,6 +84,7 @@ private function processValue(mixed $value): mixed /** * Parse "%env($name)%". * + * @return array{0: string, 1: string} * @throws CompileException */ private function parse(string $name): array diff --git a/src/Console/Command/DumpCommand.php b/src/Console/Command/DumpCommand.php index 2bb57283..38fa4a9e 100644 --- a/src/Console/Command/DumpCommand.php +++ b/src/Console/Command/DumpCommand.php @@ -76,7 +76,7 @@ public function execute(InputInterface $input, OutputInterface $output): int } // Prompt for the password if not defined - $database = $config->get('database', []); + $database = (array) $config->get('database', []); if (!array_key_exists('password', $database)) { $database['password'] = $this->promptPassword($input, $output); $config->set('database', $database); @@ -129,7 +129,7 @@ private function loadConfig(InputInterface $input): ConfigInterface */ private function addInputOptionsToConfig(ConfigInterface $config, InputInterface $input): void { - $databaseConfig = $config->get('database', []); + $databaseConfig = (array) $config->get('database', []); foreach (['host', 'port', 'user', 'password', 'database'] as $option) { $value = $input->getOption($option); diff --git a/src/Database/DatabaseFactory.php b/src/Database/DatabaseFactory.php index 19a29783..aa16d837 100644 --- a/src/Database/DatabaseFactory.php +++ b/src/Database/DatabaseFactory.php @@ -13,7 +13,7 @@ final class DatabaseFactory */ public function create(ConfigInterface $config): Database { - $connectionParams = $config->get('database', []); + $connectionParams = (array) $config->get('database', []); // Rename some keys (for compatibility with the Doctrine connection) if (array_key_exists('name', $connectionParams)) { diff --git a/src/Dumper/Config/ConfigProcessor.php b/src/Dumper/Config/ConfigProcessor.php index e900c125..e08c93e3 100644 --- a/src/Dumper/Config/ConfigProcessor.php +++ b/src/Dumper/Config/ConfigProcessor.php @@ -38,7 +38,7 @@ private function processTableLists(ConfigInterface $config): void $configKeys = ['tables_whitelist', 'tables_blacklist']; foreach ($configKeys as $configKey) { - $tableNames = $config->get($configKey, []); + $tableNames = (array) $config->get($configKey, []); if (!empty($tableNames)) { $resolved = $this->resolveTableNames($tableNames); @@ -53,7 +53,7 @@ private function processTableLists(ConfigInterface $config): void */ private function processTablesData(ConfigInterface $config): void { - $tablesData = $config->get('tables', []); + $tablesData = (array) $config->get('tables', []); if (!empty($tablesData)) { $resolved = $this->resolveTablesData($tablesData); $config->set('tables', $resolved); diff --git a/src/Dumper/Config/DumperConfig.php b/src/Dumper/Config/DumperConfig.php index 5126f19e..b9068ca6 100644 --- a/src/Dumper/Config/DumperConfig.php +++ b/src/Dumper/Config/DumperConfig.php @@ -184,7 +184,7 @@ public function getTablesToSort(): array */ private function prepareVarQueries(ConfigInterface $config): void { - $this->varQueries = $config->get('variables', []); + $this->varQueries = (array) $config->get('variables', []); // Allow only "select" statements in queries $selectQueryValidator = new QueryValidator(['select']); @@ -200,7 +200,7 @@ private function prepareVarQueries(ConfigInterface $config): void */ private function prepareDumpSettings(ConfigInterface $config): void { - $settings = $config->get('dump', []); + $settings = (array) $config->get('dump', []); foreach ($settings as $param => $value) { if (!array_key_exists($param, $this->dumpSettings)) { @@ -222,7 +222,7 @@ private function prepareDumpSettings(ConfigInterface $config): void */ private function prepareFakerSettings(ConfigInterface $config): void { - $settings = $config->get('faker', []); + $settings = (array) $config->get('faker', []); $this->fakerSettings = new FakerSettings((string) ($settings['locale'] ?? '')); } @@ -231,7 +231,7 @@ private function prepareFakerSettings(ConfigInterface $config): void */ private function prepareFilterPropagationSettings(ConfigInterface $config): void { - $settings = $config->get('filter_propagation', []); + $settings = (array) $config->get('filter_propagation', []); $this->filterPropagationSettings = new FilterPropagationSettings( $settings['enabled'] ?? true, @@ -244,11 +244,12 @@ private function prepareFilterPropagationSettings(ConfigInterface $config): void */ private function prepareTableSettings(ConfigInterface $config): void { - $this->includedTables = $config->get('tables_whitelist', []); - $this->excludedTables = $config->get('tables_blacklist', []); + $this->includedTables = (array) $config->get('tables_whitelist', []); + $this->excludedTables = (array) $config->get('tables_blacklist', []); $this->tablesConfig = new TableConfigCollection(); + $tablesData = (array) $config->get('tables', []); - foreach ($config->get('tables', []) as $tableName => $tableData) { + foreach ($tablesData as $tableName => $tableData) { $tableConfig = new TableConfig((string) $tableName, $tableData); $this->tablesConfig->add($tableConfig);