Skip to content

Commit

Permalink
Add type casting to values retrieved from the configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
guvra committed Jan 14, 2025
1 parent 39d6da5 commit 8c50e72
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 21 deletions.
19 changes: 10 additions & 9 deletions src/Config/Compiler/Processor/DumpOutputProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
1 change: 1 addition & 0 deletions src/Config/Compiler/Processor/EnvVarProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/Console/Command/DumpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Database/DatabaseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
4 changes: 2 additions & 2 deletions src/Dumper/Config/ConfigProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
15 changes: 8 additions & 7 deletions src/Dumper/Config/DumperConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand All @@ -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)) {
Expand All @@ -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'] ?? ''));
}

Expand All @@ -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,
Expand All @@ -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);

Expand Down

0 comments on commit 8c50e72

Please sign in to comment.