1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PHPSemVerChecker\Configuration; |
4
|
|
|
|
5
|
|
|
use Noodlehaus\Config; |
6
|
|
|
use PHPSemVerChecker\SemanticVersioning\Level; |
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
8
|
|
|
|
9
|
|
|
class Configuration extends Config |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var int[] |
13
|
|
|
*/ |
14
|
|
|
protected $mapping = []; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param string|array $file |
18
|
|
|
* @return \PHPSemVerChecker\Configuration\Configuration |
19
|
|
|
*/ |
20
|
|
|
public static function fromFile($file) |
21
|
|
|
{ |
22
|
|
|
return new Configuration($file); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @return \PHPSemVerChecker\Configuration\Configuration |
27
|
|
|
*/ |
28
|
|
|
public static function defaults() |
29
|
|
|
{ |
30
|
|
|
return self::fromFile(['?php-semver-checker.yml.dist', '?php-semver-checker.yml']); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string|array $path |
35
|
|
|
*/ |
36
|
1 |
|
public function __construct($path) |
37
|
|
|
{ |
38
|
1 |
|
parent::__construct($path); |
39
|
1 |
|
$this->extractMapping($this->get('level.mapping', [])); |
40
|
1 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return array |
44
|
|
|
*/ |
45
|
|
|
public function getLevelMapping() |
46
|
|
|
{ |
47
|
|
|
return $this->mapping; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Merges command line arguments and file configuration. |
52
|
|
|
* |
53
|
|
|
* This way Console\Command can validate the InputDefinition and we end up with a nice, valid Configuration before |
54
|
|
|
* execute() is called. |
55
|
|
|
* |
56
|
|
|
* @param InputInterface $input |
57
|
|
|
* @return $this |
58
|
|
|
*/ |
59
|
1 |
|
public function mergeCommandInput(InputInterface $input) |
60
|
|
|
{ |
61
|
1 |
|
foreach ($input->getArguments() as $key => $value) { |
62
|
|
|
// Add missing arguments to input if available from file configuration. |
63
|
1 |
View Code Duplication |
if ($value === null) { |
|
|
|
|
64
|
1 |
|
$input->setArgument($key, $this[$key]); |
65
|
1 |
|
} else { |
66
|
|
|
// Otherwise CLI arguments overrides the existing configuration |
67
|
1 |
|
$this[$key] = $value; |
68
|
|
|
} |
69
|
1 |
|
} |
70
|
1 |
|
foreach ($input->getOptions() as $key => $value) { |
71
|
1 |
View Code Duplication |
if ($value === null) { |
|
|
|
|
72
|
1 |
|
$input->setOption($key, $this[$key]); |
73
|
1 |
|
} else { |
74
|
1 |
|
$this[$key] = $value; |
75
|
|
|
} |
76
|
1 |
|
} |
77
|
1 |
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param array $mapping |
82
|
|
|
*/ |
83
|
1 |
|
protected function extractMapping(array $mapping) |
84
|
|
|
{ |
85
|
1 |
|
foreach ($mapping as &$level) { |
86
|
|
|
$level = Level::fromString($level); |
87
|
1 |
|
} |
88
|
1 |
|
$this->mapping = $mapping; |
89
|
1 |
|
} |
90
|
|
|
} |
91
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.