Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
ArrayConfigBuilder
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
6 / 6
19
100.00% covered (success)
100.00%
1 / 1
 has
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 update
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setMulti
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
8
 build
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setMultiDefault
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
7
1<?php
2
3namespace MediaWiki\Settings\Config;
4
5use MediaWiki\Config\HashConfig;
6use MediaWiki\Config\IterableConfig;
7use function array_key_exists;
8
9class ArrayConfigBuilder extends ConfigBuilderBase {
10
11    /** @var array */
12    protected $config = [];
13
14    protected function has( string $key ): bool {
15        return array_key_exists( $key, $this->config );
16    }
17
18    public function get( string $key ) {
19        return $this->config[$key] ?? null;
20    }
21
22    protected function update( string $key, $value ) {
23        $this->config[$key] = $value;
24    }
25
26    public function setMulti( array $values, array $mergeStrategies = [] ): ConfigBuilder {
27        if ( !$mergeStrategies ) {
28            $this->config = array_merge( $this->config, $values );
29            return $this;
30        }
31
32        foreach ( $values as $key => $newValue ) {
33            // Optimization: Inlined logic from set() for performance
34            if ( array_key_exists( $key, $this->config ) ) {
35                $mergeStrategy = $mergeStrategies[$key] ?? null;
36                if ( $mergeStrategy && is_array( $newValue ) ) {
37                    $oldValue = $this->config[$key];
38                    if ( $oldValue && is_array( $oldValue ) ) {
39                        $newValue = $mergeStrategy->merge( $oldValue, $newValue );
40                    }
41                }
42            }
43            $this->config[$key] = $newValue;
44        }
45
46        return $this;
47    }
48
49    /**
50     * Build the configuration.
51     *
52     * @return IterableConfig
53     */
54    public function build(): IterableConfig {
55        return new HashConfig( $this->config );
56    }
57
58    public function setMultiDefault( $defaults, $mergeStrategies ): ConfigBuilder {
59        foreach ( $defaults as $key => $defaultValue ) {
60            // Optimization: Inlined logic from setDefault() for performance
61            if ( array_key_exists( $key, $this->config ) ) {
62                $mergeStrategy = $mergeStrategies[$key] ?? null;
63                if ( $mergeStrategy && $defaultValue && is_array( $defaultValue ) ) {
64                    $customValue = $this->config[$key];
65                    if ( is_array( $customValue ) ) {
66                        $newValue = $mergeStrategy->merge( $defaultValue, $customValue );
67                        $this->config[$key] = $newValue;
68                    }
69                }
70            } else {
71                $this->config[$key] = $defaultValue;
72            }
73        }
74        return $this;
75    }
76
77}