Skip to content

Commit 5304774

Browse files
committed
Split Advisory\Advisor::$runResult into different properties
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
1 parent 9706a78 commit 5304774

4 files changed

Lines changed: 104 additions & 31 deletions

File tree

libraries/classes/Advisory/Advisor.php

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,17 @@ class Advisor
4343
*/
4444
private array $rules = [];
4545

46-
/** @var array{fired:mixed[], notfired:mixed[], unchecked:mixed[], errors:mixed[]} */
47-
private array $runResult = ['fired' => [], 'notfired' => [], 'unchecked' => [], 'errors' => []];
46+
/** @psalm-var list<RuleType> */
47+
private array $firedRules = [];
48+
49+
/** @psalm-var list<RuleType> */
50+
private array $notFiredRules = [];
51+
52+
/** @psalm-var list<RuleType> */
53+
private array $uncheckedRules = [];
54+
55+
/** @psalm-var list<string> */
56+
private array $errors = [];
4857

4958
public function __construct(private DatabaseInterface $dbi, private ExpressionLanguage $expression)
5059
{
@@ -99,7 +108,7 @@ static function (): void {
99108
},
100109
function (array $arguments, int|string $value) {
101110
// Did matching rule fire?
102-
foreach ($this->runResult['fired'] as $rule) {
111+
foreach ($this->firedRules as $rule) {
103112
if ($rule['id'] == $value) {
104113
return '1';
105114
}
@@ -154,7 +163,12 @@ private function setRules(): void
154163
/** @return array{fired: mixed[], notfired: mixed[], unchecked: mixed[], errors: mixed[]} */
155164
public function getRunResult(): array
156165
{
157-
return $this->runResult;
166+
return [
167+
'fired' => $this->firedRules,
168+
'notfired' => $this->notFiredRules,
169+
'unchecked' => $this->uncheckedRules,
170+
'errors' => $this->errors,
171+
];
158172
}
159173

160174
/** @psalm-return array{fired:mixed[], notfired:mixed[], unchecked:mixed[], errors:mixed[]} */
@@ -164,7 +178,12 @@ public function run(): array
164178
$this->setRules();
165179
$this->runRules();
166180

167-
return $this->runResult;
181+
return [
182+
'fired' => $this->firedRules,
183+
'notfired' => $this->notFiredRules,
184+
'unchecked' => $this->uncheckedRules,
185+
'errors' => $this->errors,
186+
];
168187
}
169188

170189
/**
@@ -175,7 +194,7 @@ public function run(): array
175194
*/
176195
private function storeError(string $description, Throwable $exception): void
177196
{
178-
$this->runResult['errors'][] = $description . ' ' . sprintf(
197+
$this->errors[] = $description . ' ' . sprintf(
179198
__('Error when evaluating: %s'),
180199
$exception->getMessage(),
181200
);
@@ -186,7 +205,10 @@ private function storeError(string $description, Throwable $exception): void
186205
*/
187206
private function runRules(): void
188207
{
189-
$this->runResult = ['fired' => [], 'notfired' => [], 'unchecked' => [], 'errors' => []];
208+
$this->firedRules = [];
209+
$this->notFiredRules = [];
210+
$this->uncheckedRules = [];
211+
$this->errors = [];
190212

191213
foreach ($this->rules as $rule) {
192214
$this->variables['value'] = 0;
@@ -251,12 +273,13 @@ private function runRules(): void
251273
*
252274
* @param string $type type of rule
253275
* @param mixed[] $rule rule itself
254-
* @psalm-param 'notfired'|'fired'|'unchecked'|'errors' $type
276+
* @psalm-param 'notfired'|'fired'|'unchecked' $type
277+
* @psalm-param RuleType $rule
255278
*/
256279
public function addRule(string $type, array $rule): void
257280
{
258-
if ($type !== 'notfired' && $type !== 'fired') {
259-
$this->runResult[$type][] = $rule;
281+
if ($type === 'unchecked') {
282+
$this->uncheckedRules[] = $rule;
260283

261284
return;
262285
}
@@ -296,7 +319,13 @@ public function addRule(string $type, array $rule): void
296319
$rule['recommendation'],
297320
);
298321

299-
$this->runResult[$type][] = $rule;
322+
if ($type === 'notfired') {
323+
$this->notFiredRules[] = $rule;
324+
325+
return;
326+
}
327+
328+
$this->firedRules[] = $rule;
300329
}
301330

302331
/**

phpstan-baseline.neon

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
parameters:
22
ignoreErrors:
3-
-
4-
message: "#^Cannot access offset 'id' on mixed\\.$#"
5-
count: 1
6-
path: libraries/classes/Advisory/Advisor.php
7-
83
-
94
message: "#^Parameter \\#1 \\$haystack of function str_contains expects string, mixed given\\.$#"
105
count: 1
@@ -26,7 +21,17 @@ parameters:
2621
path: libraries/classes/Advisory/Advisor.php
2722

2823
-
29-
message: "#^Parameter \\#3 \\$subject of function preg_replace_callback expects array\\|string, \\(array\\<int, string\\>\\|string\\|null\\) given\\.$#"
24+
message: "#^Parameter \\#3 \\$subject of function preg_replace_callback expects array\\|string, string\\|null given\\.$#"
25+
count: 1
26+
path: libraries/classes/Advisory/Advisor.php
27+
28+
-
29+
message: "#^Property PhpMyAdmin\\\\Advisory\\\\Advisor\\:\\:\\$firedRules \\(array\\<int, array\\{id\\: non\\-empty\\-string, name\\: string, precondition\\?\\: non\\-empty\\-string, formula\\: non\\-empty\\-string, test\\: non\\-empty\\-string, issue\\: string, recommendation\\: string, justification\\: string, \\.\\.\\.\\}\\>\\) does not accept non\\-empty\\-array\\<int, array\\{id\\: non\\-empty\\-string, name\\: string, precondition\\?\\: non\\-empty\\-string, formula\\: non\\-empty\\-string, test\\: non\\-empty\\-string, issue\\: string\\|null, recommendation\\: string\\|null, justification\\: string, \\.\\.\\.\\}\\>\\.$#"
30+
count: 1
31+
path: libraries/classes/Advisory/Advisor.php
32+
33+
-
34+
message: "#^Property PhpMyAdmin\\\\Advisory\\\\Advisor\\:\\:\\$notFiredRules \\(array\\<int, array\\{id\\: non\\-empty\\-string, name\\: string, precondition\\?\\: non\\-empty\\-string, formula\\: non\\-empty\\-string, test\\: non\\-empty\\-string, issue\\: string, recommendation\\: string, justification\\: string, \\.\\.\\.\\}\\>\\) does not accept non\\-empty\\-array\\<int, array\\{id\\: non\\-empty\\-string, name\\: string, precondition\\?\\: non\\-empty\\-string, formula\\: non\\-empty\\-string, test\\: non\\-empty\\-string, issue\\: string\\|null, recommendation\\: string\\|null, justification\\: string, \\.\\.\\.\\}\\>\\.$#"
3035
count: 1
3136
path: libraries/classes/Advisory/Advisor.php
3237

psalm-baseline.xml

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,13 @@
55
<code>$matches[1]</code>
66
<code>$matches[2]</code>
77
<code>$params</code>
8-
<code><![CDATA[$rule['issue']]]></code>
9-
<code><![CDATA[$rule['justification']]]></code>
10-
<code><![CDATA[$rule['name']]]></code>
11-
<code><![CDATA[$rule['recommendation']]]></code>
128
<code><![CDATA[$this->variables['version']]]></code>
139
</MixedArgument>
14-
<MixedArrayAccess>
15-
<code><![CDATA[$rule['id']]]></code>
16-
</MixedArrayAccess>
1710
<MixedAssignment>
1811
<code>$params</code>
1912
<code>$precondition</code>
20-
<code>$rule</code>
2113
<code>$value</code>
2214
</MixedAssignment>
23-
<MixedOperand>
24-
<code><![CDATA[$rule['justification_formula']]]></code>
25-
</MixedOperand>
2615
</file>
2716
<file src="libraries/classes/Application.php">
2817
<InvalidArrayOffset>

test/classes/Advisory/AdvisorTest.php

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
namespace PhpMyAdmin\Tests\Advisory;
66

77
use PhpMyAdmin\Advisory\Advisor;
8+
use PhpMyAdmin\Advisory\Rules;
89
use PhpMyAdmin\Tests\AbstractTestCase;
910
use PHPUnit\Framework\Attributes\CoversClass;
1011
use PHPUnit\Framework\Attributes\DataProvider;
1112
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
1213

14+
/** @psalm-import-type RuleType from Rules */
1315
#[CoversClass(Advisor::class)]
1416
class AdvisorTest extends AbstractTestCase
1517
{
@@ -54,6 +56,8 @@ public static function advisorTimes(): array
5456
* @param mixed[] $rule Rule to test
5557
* @param mixed[] $expected Expected rendered rule in fired/errors list
5658
* @param string|null $error Expected error string (null if none error expected)
59+
* @psalm-param RuleType $rule
60+
* @psalm-param RuleType|array<empty> $expected
5761
*/
5862
#[DataProvider('rulesProvider')]
5963
public function testAddRule(array $rule, array $expected, string|null $error): void
@@ -75,7 +79,10 @@ public function testAddRule(array $rule, array $expected, string|null $error): v
7579
$this->assertEquals([$expected], $runResult['fired']);
7680
}
7781

78-
/** @return mixed[][] */
82+
/**
83+
* @return mixed[][]
84+
* @psalm-return array<int, array{RuleType, RuleType|array<empty>, string|null}>
85+
*/
7986
public static function rulesProvider(): array
8087
{
8188
return [
@@ -86,13 +93,17 @@ public static function rulesProvider(): array
8693
'name' => 'Basic',
8794
'issue' => 'issue',
8895
'recommendation' => 'Recommend',
96+
'formula' => 'formula',
97+
'test' => 'test',
8998
],
9099
[
91100
'justification' => 'foo',
92101
'id' => 'Basic',
93102
'name' => 'Basic',
94103
'issue' => 'issue',
95104
'recommendation' => 'Recommend',
105+
'formula' => 'formula',
106+
'test' => 'test',
96107
],
97108
null,
98109
],
@@ -103,14 +114,18 @@ public static function rulesProvider(): array
103114
'name' => 'Variable',
104115
'issue' => 'issue',
105116
'recommendation' => 'Recommend {status_var}',
117+
'formula' => 'formula',
118+
'test' => 'test',
106119
],
107120
[
108121
'justification' => 'foo',
109122
'id' => 'Variable',
110123
'name' => 'Variable',
111124
'issue' => 'issue',
112-
'recommendation' => 'Recommend <a href="index.php?route=/server/variables&' .
113-
'filter=status_var&lang=en">status_var</a>',
125+
'recommendation' => 'Recommend <a href="index.php?route=/server/variables&'
126+
. 'filter=status_var&lang=en">status_var</a>',
127+
'formula' => 'formula',
128+
'test' => 'test',
114129
],
115130
null,
116131
],
@@ -122,6 +137,8 @@ public static function rulesProvider(): array
122137
'name' => 'Format',
123138
'issue' => 'issue',
124139
'recommendation' => 'Recommend',
140+
'formula' => 'formula',
141+
'test' => 'test',
125142
],
126143
[
127144
'justification' => '0 foo',
@@ -130,6 +147,8 @@ public static function rulesProvider(): array
130147
'name' => 'Format',
131148
'issue' => 'issue',
132149
'recommendation' => 'Recommend',
150+
'formula' => 'formula',
151+
'test' => 'test',
133152
],
134153
null,
135154
],
@@ -141,6 +160,8 @@ public static function rulesProvider(): array
141160
'name' => 'Percent',
142161
'issue' => 'issue',
143162
'recommendation' => 'Recommend',
163+
'formula' => 'formula',
164+
'test' => 'test',
144165
],
145166
[
146167
'justification' => '0% foo',
@@ -149,6 +170,8 @@ public static function rulesProvider(): array
149170
'name' => 'Percent',
150171
'issue' => 'issue',
151172
'recommendation' => 'Recommend',
173+
'formula' => 'formula',
174+
'test' => 'test',
152175
],
153176
null,
154177
],
@@ -160,6 +183,8 @@ public static function rulesProvider(): array
160183
'name' => 'Double',
161184
'issue' => 'issue',
162185
'recommendation' => 'Recommend',
186+
'formula' => 'formula',
187+
'test' => 'test',
163188
],
164189
[
165190
'justification' => '0% 0 foo',
@@ -168,6 +193,8 @@ public static function rulesProvider(): array
168193
'name' => 'Double',
169194
'issue' => 'issue',
170195
'recommendation' => 'Recommend',
196+
'formula' => 'formula',
197+
'test' => 'test',
171198
],
172199
null,
173200
],
@@ -178,23 +205,30 @@ public static function rulesProvider(): array
178205
'name' => 'Quotes',
179206
'issue' => 'issue',
180207
'recommendation' => 'Recommend"\'',
208+
'formula' => 'formula',
209+
'test' => 'test',
181210
],
182211
[
183212
'justification' => '"\'foo',
184213
'id' => 'Quotes',
185214
'name' => 'Quotes',
186215
'issue' => 'issue',
187216
'recommendation' => 'Recommend"\'',
217+
'formula' => 'formula',
218+
'test' => 'test',
188219
],
189220
null,
190221
],
191222
[
192223
[
224+
'id' => 'Failure',
193225
'justification' => 'foo',
194226
'justification_formula' => 'fsafdsa',
195227
'name' => 'Failure',
196228
'issue' => 'issue',
197229
'recommendation' => 'Recommend',
230+
'formula' => 'formula',
231+
'test' => 'test',
198232
],
199233
[],
200234
'Failed formatting string for rule \'Failure\'. ' .
@@ -209,6 +243,8 @@ public static function rulesProvider(): array
209243
'name' => 'Distribution',
210244
'issue' => 'official MySQL binaries.',
211245
'recommendation' => 'See <a href="https://example.com/">web</a>',
246+
'formula' => 'formula',
247+
'test' => 'test',
212248
],
213249
[
214250
'justification' => 'Version string (0)',
@@ -218,6 +254,8 @@ public static function rulesProvider(): array
218254
'recommendation' => 'See <a href="index.php?route=/url&url=https%3A%2F%2F' .
219255
'example.com%2F" target="_blank" rel="noopener noreferrer">web</a>',
220256
'id' => 'Distribution',
257+
'formula' => 'formula',
258+
'test' => 'test',
221259
],
222260
null,
223261
],
@@ -229,6 +267,8 @@ public static function rulesProvider(): array
229267
'name' => 'Distribution',
230268
'issue' => 'official MySQL binaries.',
231269
'recommendation' => 'See <a href="https://example.com/">web</a>',
270+
'formula' => 'formula',
271+
'test' => 'test',
232272
],
233273
[
234274
'justification' => 'Timestamp (15 days, 22 hours, 30 minutes and 27 seconds)',
@@ -238,6 +278,8 @@ public static function rulesProvider(): array
238278
'recommendation' => 'See <a href="index.php?route=/url&url=https%3A%2F%2F' .
239279
'example.com%2F" target="_blank" rel="noopener noreferrer">web</a>',
240280
'id' => 'Distribution',
281+
'formula' => 'formula',
282+
'test' => 'test',
241283
],
242284
null,
243285
],
@@ -250,6 +292,8 @@ public static function rulesProvider(): array
250292
'issue' => 'official MySQL binaries.',
251293
'recommendation' => 'See <a href="https://example.com/">web</a> and'
252294
. ' <a href="https://example.com/">web2</a>',
295+
'formula' => 'formula',
296+
'test' => 'test',
253297
],
254298
[
255299
'justification' => 'Memory: 0.95 MiB',
@@ -261,6 +305,8 @@ public static function rulesProvider(): array
261305
. ' and <a href="index.php?route=/url&url=https%3A%2F%2Fexample.com%2F" target="_blank"'
262306
. ' rel="noopener noreferrer">web2</a>',
263307
'id' => 'Distribution',
308+
'formula' => 'formula',
309+
'test' => 'test',
264310
],
265311
null,
266312
],
@@ -273,6 +319,8 @@ public static function rulesProvider(): array
273319
'issue' => '{long_query_time} is set to 10 seconds or more',
274320
'recommendation' => 'See <a href=\'https://example.com/\'>web</a> and'
275321
. ' <a href=\'https://example.com/\'>web2</a>',
322+
'formula' => 'formula',
323+
'test' => 'test',
276324
],
277325
[
278326
'justification' => 'Time: 1.2 per minute',
@@ -285,6 +333,8 @@ public static function rulesProvider(): array
285333
. ' and <a href="index.php?route=/url&url=https%3A%2F%2Fexample.com%2F" target="_blank"'
286334
. ' rel="noopener noreferrer">web2</a>',
287335
'id' => 'Distribution',
336+
'formula' => 'formula',
337+
'test' => 'test',
288338
],
289339
null,
290340
],

0 commit comments

Comments
 (0)