Skip to content

Commit a79b9e9

Browse files
committed
Simplified some logic
1 parent cdf6b91 commit a79b9e9

2 files changed

Lines changed: 8 additions & 16 deletions

File tree

Processor.php

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ public function processFile(array $config)
3838
$expectedParams = (array) $expectedValues[$parameterKey];
3939

4040
// find the actual params
41-
$actualValues = array($parameterKey => array());
41+
$actualValues = array_merge(
42+
// Preserve other top-level keys than `$parameterKey` in the file
43+
$expectedValues,
44+
array($parameterKey => array())
45+
);
4246
if ($exists) {
4347
$existingValues = $yamlParser->parse(file_get_contents($realFile));
4448
if ($existingValues === null) {
@@ -52,13 +56,6 @@ public function processFile(array $config)
5256

5357
$actualValues[$parameterKey] = $this->processParams($config, $expectedParams, (array) $actualValues[$parameterKey]);
5458

55-
// Preserve other top-level keys than `$parameterKey` in the file
56-
foreach ($expectedValues as $key => $setting) {
57-
if (!array_key_exists($key, $actualValues)) {
58-
$actualValues[$key] = $setting;
59-
}
60-
}
61-
6259
if (!is_dir($dir = dirname($realFile))) {
6360
mkdir($dir, 0755, true);
6461
}
@@ -87,7 +84,7 @@ private function processConfig(array $config)
8784
return $config;
8885
}
8986

90-
private function processParams(array $config, $expectedParams, $actualParams)
87+
private function processParams(array $config, array $expectedParams, array $actualParams)
9188
{
9289
// Grab values for parameters that were renamed
9390
$renameMap = empty($config['rename-map']) ? array() : (array) $config['rename-map'];
@@ -99,12 +96,7 @@ private function processParams(array $config, $expectedParams, $actualParams)
9996
}
10097

10198
if (!$keepOutdatedParams) {
102-
// Remove the outdated params
103-
foreach ($actualParams as $key => $value) {
104-
if (!array_key_exists($key, $expectedParams)) {
105-
unset($actualParams[$key]);
106-
}
107-
}
99+
$actualParams = array_intersect_key($actualParams, $expectedParams);
108100
}
109101

110102
$envMap = empty($config['env-map']) ? array() : (array) $config['env-map'];

Tests/fixtures/testcases/extra_keys/expected.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
parameters:
33
foo: existing_foo
44
boolean: false
5-
another_key: foo
65
extra_key: 'a new extra key'
6+
another_key: foo

0 commit comments

Comments
 (0)