Skip to content

Commit 1c22cfb

Browse files
committed
Add and use InputMerger
1 parent 3a3b8a8 commit 1c22cfb

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

src/PHPSemVerChecker/Console/Command/CompareCommand.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PHPSemVerChecker\Analyzer\Analyzer;
66
use PHPSemVerChecker\Configuration\Configuration;
77
use PHPSemVerChecker\Configuration\LevelMapping;
8+
use PHPSemVerChecker\Console\InputMerger;
89
use PHPSemVerChecker\Filter\SourceFilter;
910
use PHPSemVerChecker\Finder\Finder;
1011
use PHPSemVerChecker\Reporter\JsonReporter;
@@ -40,8 +41,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
4041
{
4142
$startTime = microtime(true);
4243

43-
$config = $input->getOption('config');
44-
$configuration = $config ? Configuration::fromFile($config) : Configuration::defaults();
44+
$configPath = $input->getOption('config');
45+
$configuration = $configPath ? Configuration::fromFile($configPath) : Configuration::defaults();
46+
$im = new InputMerger();
47+
$im->merge($input, $configuration);
4548

4649
// Set overrides
4750
LevelMapping::setOverrides($configuration->getLevelMapping());
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace PHPSemVerChecker\Console;
4+
5+
use PHPSemVerChecker\Configuration\Configuration;
6+
use Symfony\Component\Console\Input\InputInterface;
7+
8+
/**
9+
* Merges CLI input with existing configuration values.
10+
*
11+
* This is to ensure that CLI input has priority and is prepared for validation
12+
* by symfony/console commands.
13+
*/
14+
class InputMerger
15+
{
16+
/**
17+
* @param \Symfony\Component\Console\Input\InputInterface $input
18+
* @param \PHPSemVerChecker\Configuration\Configuration $config
19+
*/
20+
public function merge(InputInterface $input, Configuration $config)
21+
{
22+
$missingArguments = $this->getKeysOfNullValues($input->getArguments());
23+
foreach ($missingArguments as $key) {
24+
$input->setArgument($key, $config->get($key));
25+
}
26+
$missingOptions = $this->getKeysOfNullValues($input->getOptions());
27+
foreach ($missingOptions as $key) {
28+
$input->setOption($key, $config->get($key));
29+
}
30+
$config->merge(array_merge($input->getArguments(), $input->getOptions()));
31+
}
32+
33+
/**
34+
* @param array $array
35+
* @return array
36+
*/
37+
private function getKeysOfNullValues(array $array)
38+
{
39+
return array_keys(array_filter($array, 'is_null'));
40+
}
41+
}

0 commit comments

Comments
 (0)