Completed
Push — master ( c2c721...9a7aed )
by Michael
02:00
created

ComposerExtra::merge()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
1
<?php
2
3
namespace Schnittstabil\ComposerExtra;
4
5
use function Schnittstabil\Get\getValue;
6
use function Schnittstabil\Get\getValueOrFail;
7
use function Schnittstabil\JsonDecodeFile\jsonDecodeFile;
8
use Schnittstabil\Get\Get;
9
10
/**
11
 * Get namespaced configuration from `composer.json`.
12
 */
13
class ComposerExtra
14
{
15
    use PresetsAwareTrait;
16
17
    protected $config;
18
    protected $namespace;
19
    protected $defaultConfig;
20
    protected $presetsPath;
21
    protected $composerJson;
22
23
    /**
24
     * Merge configurations.
25
     *
26
     * @var callable
27
     */
28
    public $merge = '\Schnittstabil\ConfigMerge\config_merge';
29
30
    /**
31
     * Create a new ComposerExtra.
32
     *
33
     * @see https://github.com/schnittstabil/get Documentation of `Schnittstabil\Get\getValue`
34
     *
35
     * @param string|int|mixed[] $namespace     a `Schnittstabil\Get\getValue` path
36
     * @param mixed              $defaultConfig default configuration
37
     * @param string             $presetsPath   presets path (w/o namespace)
38
     *
39
     * @throws \KHerGe\File\Exception\FileException
40
     * @throws \Seld\JsonLint\ParsingException
41
     *
42
     * @SuppressWarnings(PHPMD.StaticAccess)
43
     */
44
    public function __construct($namespace = array(), $defaultConfig = null, $presetsPath = null)
45
    {
46
        $this->namespace = Get::normalizePath($namespace);
47
        array_unshift($this->namespace, 'extra');
48
        $this->defaultConfig = $defaultConfig === null ? new \stdClass() : $defaultConfig;
49
        $this->presetsPath = $presetsPath;
50
        $this-> = jsonDecodeFile('composer.json');
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected '=', expecting T_STRING or T_VARIABLE or '{' or '$'
Loading history...
51
    }
52
53
    /**
54
     * Merge two configs.
55
     *
56
     * @param mixed $target Target config
57
     * @param mixed $source Source config
58
     *
59
     * @return mixed The merged config
60
     */
61
    protected function merge($target, $source)
62
    {
63
        if ($source === null) {
64
            return $target;
65
        }
66
67
        return call_user_func($this->merge, $target, $source);
68
    }
69
70
    /**
71
     * Get the configuration.
72
     *
73
     * @return mixed the configuration
74
     */
75
    protected function getConfig()
76
    {
77
        if ($this->config !== null) {
78
            return $this->config;
79
        }
80
81
        $config = $this->merge(
82
            $this->defaultConfig,
83
            getValue($this->namespace, $this->composerJson)
84
        );
85
86
        if ($this->presetsPath === null || $config === null) {
87
            return $this->config = $config;
88
        }
89
90
        $presets = $this->loadPresets(getValue($this->presetsPath, $config, []));
91
        $presetsConfig = array_reduce($presets, $this->merge, array_shift($presets));
92
93
        return $this->config = $this->merge($presetsConfig, $config);
94
    }
95
96
    /**
97
     * Get configuration value.
98
     *
99
     * @see https://github.com/schnittstabil/get Documentation of `Schnittstabil\Get\getValue`
100
     *
101
     * @param string|int|mixed[] $path    a `Schnittstabil\Get\getValue` path
102
     * @param mixed              $default default value if $path is not valid
103
     *
104
     * @return mixed the value determined by `$path` or otherwise `$default`
105
     */
106
    public function get($path = array(), $default = null)
107
    {
108
        return getValue($path, $this->getConfig(), $default);
109
    }
110
111
    /**
112
     * Get configuration value.
113
     *
114
     * @see https://github.com/schnittstabil/get Documentation of `Schnittstabil\Get\getValueOrFail`
115
     *
116
     * @param string|int|mixed[] $path    a `Schnittstabil\Get\getValueOrFail` path
117
     * @param mixed              $message exception message
118
     *
119
     * @throws \OutOfBoundsException if `$path` is not valid
120
     *
121
     * @return mixed the value determined by `$path`
122
     */
123
    public function getOrFail($path = array(), $message = null)
124
    {
125
        return getValueOrFail($path, $this->getConfig(), $message);
126
    }
127
}
128