Skip to content

Commit

Permalink
Smarter diffing
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Jul 12, 2024
1 parent 53a989f commit 3ff7c90
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion monorepo/DevTools/src/RefactorConfigCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use function is_string;
use function file_exists;
use function array_udiff;
use function array_filter;
use function str_starts_with;
use function file_put_contents;

Expand Down Expand Up @@ -77,7 +78,7 @@ protected function migrateToYaml(): void
$default = require Hyde::vendorPath('config/hyde.php');

// Todo: Add argument to not diff out defaults
$config = array_udiff($config, $default, fn ($a, $b) => $a === $b ? 0 : 1);
$config = $this->diffConfig($config, $default);

$config = $this->serializePhpData($config);

Expand Down Expand Up @@ -135,4 +136,41 @@ protected function serializePhpValue(mixed $value, string|int $key): array

return [$key => $value];
}

// Remove any default values from the config by iterating the root array keys and comparing the values
protected function diffConfig(array $config, array $default): array
{
$new = [];

foreach ($config as $key => $value) {
if (is_array($value) && isset($default[$key])) {
if ($value === $default[$key]) {
continue;
}
}

if (isset($default[$key]) && $value === $default[$key]) {
continue;
}

$new[$key] = $value;
}

return $this->arrayFilterRecurse($new);
}

protected function arrayFilterRecurse(array $input): array
{
foreach ($input as $key => &$value) {
if (is_array($value)) {
$value = $this->arrayFilterRecurse($value);
if (empty($value)) {
unset($input[$key]);
}
} elseif (blank($value)) {
unset($input[$key]);
}
}
return $input;
}
}

0 comments on commit 3ff7c90

Please sign in to comment.