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 62b99d4
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions monorepo/DevTools/src/RefactorConfigCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
use function is_array;
use function is_string;
use function file_exists;
use function array_udiff;
use function str_starts_with;
use function file_put_contents;

Expand Down Expand Up @@ -77,7 +76,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 +134,42 @@ 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 62b99d4

Please sign in to comment.