This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Wikibase\Lib\Tests; |
||
4 | |||
5 | use MWException; |
||
6 | use Wikibase\Lib\SettingsArray; |
||
7 | use Wikibase\Lib\WikibaseSettings; |
||
8 | use Wikimedia\TestingAccessWrapper; |
||
9 | |||
10 | /** |
||
11 | * @covers \Wikibase\Lib\WikibaseSettings |
||
12 | * |
||
13 | * @group Wikibase |
||
14 | * |
||
15 | * @license GPL-2.0-or-later |
||
16 | * @author Daniel Kinzler <[email protected]> |
||
17 | */ |
||
18 | class WikibaseSettingsTest extends \PHPUnit\Framework\TestCase { |
||
19 | |||
20 | public function testGetRepoSettings() { |
||
21 | if ( WikibaseSettings::isRepoEnabled() ) { |
||
22 | $this->assertNotNull( WikibaseSettings::getRepoSettings() ); |
||
23 | } else { |
||
24 | $this->expectException( MWException::class ); |
||
25 | WikibaseSettings::getRepoSettings(); |
||
26 | } |
||
27 | } |
||
28 | |||
29 | public function testGetClientSettings() { |
||
30 | if ( WikibaseSettings::isClientEnabled() ) { |
||
31 | $this->assertNotNull( WikibaseSettings::getClientSettings() ); |
||
32 | } else { |
||
33 | $this->expectException( MWException::class ); |
||
34 | WikibaseSettings::getClientSettings(); |
||
35 | } |
||
36 | } |
||
37 | |||
38 | /** @dataProvider provideSettingsTriples */ |
||
39 | public function testMergeSettings( array $default, array $custom, array $expected ) { |
||
40 | /** @var SettingsArray $actual */ |
||
41 | $actual = TestingAccessWrapper::newFromClass( WikibaseSettings::class ) |
||
42 | ->mergeSettings( $default, $custom ); |
||
43 | |||
44 | $this->assertSame( $expected, $actual->getArrayCopy() ); |
||
45 | } |
||
46 | |||
47 | public function provideSettingsTriples() { |
||
48 | $default = [ 'key' => 'x' ]; |
||
49 | $custom['key'] = 'y'; |
||
0 ignored issues
–
show
|
|||
50 | $expected = [ 'key' => 'y' ]; |
||
51 | yield "['key'], scalar" => [ $default, $custom, $expected ]; |
||
52 | unset( $custom ); |
||
53 | |||
54 | $default = [ 'key' => [] ]; |
||
55 | $custom['key'][] = 'one'; |
||
56 | $expected = [ 'key' => [ 'one' ] ]; |
||
57 | yield "['key'][], default empty" => [ $default, $custom, $expected ]; |
||
58 | unset( $custom ); |
||
59 | |||
60 | $default = [ 'key' => [ 'one', 'two' ] ]; |
||
61 | $custom['key'][] = 'three'; |
||
62 | $expected = [ 'key' => [ 'one', 'two', 'three' ] ]; |
||
63 | yield "['key'][], default nonempty" => [ $default, $custom, $expected ]; |
||
64 | unset( $custom ); |
||
65 | |||
66 | $default = [ 'key' => [ 'a' => 'A', 'b' => 'B' ] ]; |
||
67 | $custom['key']['a'] = 'Ä'; |
||
68 | $expected = [ 'key' => [ 'a' => 'Ä', 'b' => 'B' ] ]; |
||
69 | yield "['key']['a'], scalar" => [ $default, $custom, $expected ]; |
||
70 | unset( $custom ); |
||
71 | |||
72 | $default = [ 'key' => [ 'one' ] ]; |
||
73 | $custom['key'] = 'two'; |
||
74 | $expected = [ 'key' => 'two' ]; |
||
75 | yield "['key'], nonempty->scalar" => [ $default, $custom, $expected ]; |
||
76 | unset( $custom ); |
||
77 | |||
78 | $default = [ 'key' => 'value' ]; |
||
79 | $custom = []; |
||
80 | $expected = [ 'key' => 'value' ]; |
||
81 | yield 'no custom setting' => [ $default, $custom, $expected ]; |
||
82 | unset( $custom ); |
||
83 | |||
84 | $default = []; |
||
85 | $custom['key'] = 'value'; |
||
86 | $expected = [ 'key' => 'value' ]; |
||
87 | yield 'no default setting, scalar' => [ $default, $custom, $expected ]; |
||
88 | unset( $custom ); |
||
89 | |||
90 | $default = []; |
||
91 | $custom['key'] = [ 'one', 'two' ]; |
||
92 | $expected = [ 'key' => [ 'one', 'two' ] ]; |
||
93 | yield 'no default setting, array' => [ $default, $custom, $expected ]; |
||
94 | unset( $custom ); |
||
95 | |||
96 | $default = [ 'key' => function () { |
||
97 | return 'value'; |
||
98 | } ]; |
||
99 | $custom['key'] = 'custom value'; |
||
100 | $expected = [ 'key' => 'custom value' ]; |
||
101 | yield "['key'], callable->scalar" => [ $default, $custom, $expected ]; |
||
102 | unset( $custom ); |
||
103 | |||
104 | $default = [ 'key' => function () { |
||
105 | return [ 'one', 'two' ]; |
||
106 | } ]; |
||
107 | $custom['key'] = [ 'ONE', 'TWO' ]; |
||
108 | $expected = [ 'key' => [ 'ONE', 'TWO' ] ]; |
||
109 | yield "['key'], callable->array" => [ $default, $custom, $expected ]; |
||
110 | unset( $custom ); |
||
111 | } |
||
112 | |||
113 | /** @dataProvider provideSettingsComplexMerges */ |
||
114 | public function testMergeSettingsComplexMerges( |
||
115 | array $default, |
||
116 | array $custom, |
||
117 | array $expected, |
||
118 | array $overrideArrays = [], |
||
119 | array $twoDArrayMerge = [], |
||
120 | array $falseMeansRemove = [] |
||
121 | ) { |
||
122 | /** @var SettingsArray $actual */ |
||
123 | $actual = TestingAccessWrapper::newFromClass( WikibaseSettings::class ) |
||
124 | ->mergeSettings( $default, $custom, $overrideArrays, $twoDArrayMerge, $falseMeansRemove ); |
||
125 | |||
126 | $this->assertSame( $expected, $actual->getArrayCopy() ); |
||
127 | } |
||
128 | |||
129 | public function provideSettingsComplexMerges() { |
||
130 | $default = [ 'key' => [ 'one', 'two' ] ]; |
||
131 | $custom['key']['two'] = false; |
||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
$custom was never initialized. Although not strictly required by PHP, it is generally a good practice to add $custom = array(); before regardless.
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code. Let’s take a look at an example: foreach ($collection as $item) {
$myArray['foo'] = $item->getFoo();
if ($item->hasBar()) {
$myArray['bar'] = $item->getBar();
}
// do something with $myArray
}
As you can see in this example, the array This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop. ![]() |
|||
132 | $expected = [ 'key' => [ 'one' ] ]; |
||
133 | yield "['key'][], false removing the key" => [ $default, $custom, $expected, [], [], [ 'key' ] ]; |
||
134 | unset( $custom ); |
||
135 | |||
136 | $default = [ 'key' => [ 'a' => 'A', 'b' => 'B' ] ]; |
||
137 | $custom['key']['a'] = 'Ä'; |
||
138 | $expected = [ 'key' => [ 'a' => 'Ä' ] ]; |
||
139 | yield "['key']['a'], override all config" => [ $default, $custom, $expected, [ 'key' ], [], [] ]; |
||
140 | unset( $custom ); |
||
141 | |||
142 | $default = [ 'key' => [ 'a' => [ 'a' => 'A' ], 'b' => [ 'B' ] ] ]; |
||
143 | $custom['key']['a']['b'] = 'Ä'; |
||
144 | $expected = [ 'key' => [ 'a' => [ 'b' => 'Ä', 'a' => 'A' ], 'b' => [ 'B' ] ] ]; |
||
145 | yield "['key']['a'], 2d merge (add value)" => [ $default, $custom, $expected, [], [ 'key' ], [] ]; |
||
146 | unset( $custom ); |
||
147 | |||
148 | $default = [ 'key' => [ 'a' => [ 'a' => 'A' ], 'b' => [ 'B' ] ] ]; |
||
149 | $custom['key']['a']['a'] = 'C'; |
||
150 | $expected = [ 'key' => [ 'a' => [ 'a' => 'C' ], 'b' => [ 'B' ] ] ]; |
||
151 | yield "['key']['a'], 2d merge (update value)" => [ $default, $custom, $expected, [], [ 'key' ], [] ]; |
||
152 | unset( $custom ); |
||
153 | } |
||
154 | |||
155 | } |
||
156 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.