Skip to content

Commit c76bb77

Browse files
Merge pull request #17805 from kamil-tekiela/Fix-phpstan-issues3
Fix static analysis issues
2 parents 0f57dff + 32d9328 commit c76bb77

7 files changed

Lines changed: 64 additions & 110 deletions

File tree

libraries/classes/Controllers/Export/ExportController.php

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ public function __invoke(ServerRequest $request): void
7070
$GLOBALS['quick_export'] = $GLOBALS['quick_export'] ?? null;
7171
$GLOBALS['tables'] = $GLOBALS['tables'] ?? null;
7272
$GLOBALS['table_select'] = $GLOBALS['table_select'] ?? null;
73-
$GLOBALS['aliases'] = $GLOBALS['aliases'] ?? null;
7473
$GLOBALS['time_start'] = $GLOBALS['time_start'] ?? null;
7574
$GLOBALS['charset'] = $GLOBALS['charset'] ?? null;
7675
$GLOBALS['remember_template'] = $GLOBALS['remember_template'] ?? null;
@@ -228,13 +227,13 @@ public function __invoke(ServerRequest $request): void
228227
// export page, Export page aliases are given more
229228
// preference over SQL Query aliases.
230229
$parser = new Parser($GLOBALS['sql_query']);
231-
$GLOBALS['aliases'] = [];
230+
$aliases = [];
232231
if (! empty($parser->statements[0]) && ($parser->statements[0] instanceof SelectStatement)) {
233-
$GLOBALS['aliases'] = Misc::getAliases($parser->statements[0], $GLOBALS['db']);
232+
$aliases = Misc::getAliases($parser->statements[0], $GLOBALS['db']);
234233
}
235234

236-
if (! empty($aliasesParam)) {
237-
$GLOBALS['aliases'] = $this->export->mergeAliases($GLOBALS['aliases'], $aliasesParam);
235+
if ($aliasesParam !== null && $aliasesParam !== []) {
236+
$aliases = $this->export->mergeAliases($aliases, $aliasesParam);
238237
$_SESSION['tmpval']['aliases'] = $aliasesParam;
239238
}
240239

@@ -391,7 +390,7 @@ public function __invoke(ServerRequest $request): void
391390
$GLOBALS['do_comments'],
392391
$GLOBALS['do_mime'],
393392
$GLOBALS['do_dates'],
394-
$GLOBALS['aliases'],
393+
$aliases,
395394
$GLOBALS['separate_files']
396395
);
397396
} elseif ($GLOBALS['export_type'] === 'database') {
@@ -424,7 +423,7 @@ public function __invoke(ServerRequest $request): void
424423
$GLOBALS['do_comments'],
425424
$GLOBALS['do_mime'],
426425
$GLOBALS['do_dates'],
427-
$GLOBALS['aliases'],
426+
$aliases,
428427
$GLOBALS['separate_files']
429428
);
430429
} finally {
@@ -444,7 +443,7 @@ public function __invoke(ServerRequest $request): void
444443
$GLOBALS['do_comments'],
445444
$GLOBALS['do_mime'],
446445
$GLOBALS['do_dates'],
447-
$GLOBALS['aliases'],
446+
$aliases,
448447
$GLOBALS['separate_files']
449448
);
450449
}
@@ -489,7 +488,7 @@ public function __invoke(ServerRequest $request): void
489488
$GLOBALS['limit_to'],
490489
$GLOBALS['limit_from'],
491490
$GLOBALS['sql_query'],
492-
$GLOBALS['aliases']
491+
$aliases
493492
);
494493
} finally {
495494
$this->export->unlockTables();
@@ -510,7 +509,7 @@ public function __invoke(ServerRequest $request): void
510509
$GLOBALS['limit_to'],
511510
$GLOBALS['limit_from'],
512511
$GLOBALS['sql_query'],
513-
$GLOBALS['aliases']
512+
$aliases
514513
);
515514
}
516515
}
@@ -1038,14 +1037,9 @@ private function setGlobalsFromRequest(array $postParams): void
10381037
$GLOBALS['latex_data_label'] = $postParams['latex_data_label'];
10391038
}
10401039

1040+
// phpcs:ignore SlevomatCodingStandard.ControlStructures.EarlyExit.EarlyExitNotUsed
10411041
if (isset($postParams['latex_null'])) {
10421042
$GLOBALS['latex_null'] = $postParams['latex_null'];
10431043
}
1044-
1045-
if (! isset($postParams['aliases'])) {
1046-
return;
1047-
}
1048-
1049-
$GLOBALS['aliases'] = $postParams['aliases'];
10501044
}
10511045
}

libraries/classes/Display/Results.php

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,6 @@ private function setDisplayPartsForSelect(DisplayParts $displayParts): DisplayPa
516516
$fieldsMeta = $this->properties['fields_meta'];
517517
$previousTable = '';
518518
$numberOfColumns = $this->properties['fields_cnt'];
519-
$hasTextButton = true;
520519
$hasEditLink = $displayParts->hasEditLink;
521520
$deleteLink = $displayParts->deleteLink;
522521
$hasPrintLink = $displayParts->hasPrintLink;
@@ -555,7 +554,7 @@ private function setDisplayPartsForSelect(DisplayParts $displayParts): DisplayPa
555554
return $displayParts->with([
556555
'hasEditLink' => $hasEditLink,
557556
'deleteLink' => $deleteLink,
558-
'hasTextButton' => $hasTextButton,
557+
'hasTextButton' => true,
559558
'hasPrintLink' => $hasPrintLink,
560559
]);
561560
}
@@ -2339,30 +2338,29 @@ private function getRowValues(
23392338
$transformationPlugin = null;
23402339
$transformOptions = [];
23412340

2342-
if ($relationParameters->browserTransformationFeature !== null && $GLOBALS['cfg']['BrowseMIME']) {
2343-
if (
2344-
isset($mediaTypeMap[$orgFullColName]['mimetype'])
2345-
&& ! empty($mediaTypeMap[$orgFullColName]['transformation'])
2346-
) {
2347-
$file = $mediaTypeMap[$orgFullColName]['transformation'];
2348-
$includeFile = 'libraries/classes/Plugins/Transformations/' . $file;
2349-
2350-
if (@file_exists(ROOT_PATH . $includeFile)) {
2351-
$className = $this->transformations->getClassName($includeFile);
2352-
if (class_exists($className)) {
2353-
$plugin = new $className();
2354-
if ($plugin instanceof TransformationsPlugin) {
2355-
$transformationPlugin = $plugin;
2356-
$transformOptions = $this->transformations->getOptions(
2357-
$mediaTypeMap[$orgFullColName]['transformation_options'] ?? ''
2358-
);
2359-
2360-
$meta->internalMediaType = str_replace(
2361-
'_',
2362-
'/',
2363-
$mediaTypeMap[$orgFullColName]['mimetype']
2364-
);
2365-
}
2341+
if (
2342+
$relationParameters->browserTransformationFeature !== null && $GLOBALS['cfg']['BrowseMIME']
2343+
&& isset($mediaTypeMap[$orgFullColName]['mimetype'])
2344+
&& ! empty($mediaTypeMap[$orgFullColName]['transformation'])
2345+
) {
2346+
$file = $mediaTypeMap[$orgFullColName]['transformation'];
2347+
$includeFile = 'libraries/classes/Plugins/Transformations/' . $file;
2348+
2349+
if (@file_exists(ROOT_PATH . $includeFile)) {
2350+
$className = $this->transformations->getClassName($includeFile);
2351+
if (class_exists($className)) {
2352+
$plugin = new $className();
2353+
if ($plugin instanceof TransformationsPlugin) {
2354+
$transformationPlugin = $plugin;
2355+
$transformOptions = $this->transformations->getOptions(
2356+
$mediaTypeMap[$orgFullColName]['transformation_options'] ?? ''
2357+
);
2358+
2359+
$meta->internalMediaType = str_replace(
2360+
'_',
2361+
'/',
2362+
$mediaTypeMap[$orgFullColName]['mimetype']
2363+
);
23662364
}
23672365
}
23682366
}

libraries/classes/Export.php

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class Export
5858
/** @var DatabaseInterface */
5959
private $dbi;
6060

61-
/** @var mixed */
61+
/** @var string */
6262
public $dumpBuffer = '';
6363

6464
/** @var int */
@@ -126,7 +126,7 @@ public function gzencodeNeeded(): bool
126126
*
127127
* @param string $line the insert statement
128128
*/
129-
public function outputHandler(?string $line): bool
129+
public function outputHandler(string $line): bool
130130
{
131131
$GLOBALS['time_start'] = $GLOBALS['time_start'] ?? null;
132132
$GLOBALS['save_filename'] = $GLOBALS['save_filename'] ?? null;
@@ -140,7 +140,7 @@ public function outputHandler(?string $line): bool
140140
if ($GLOBALS['buffer_needed']) {
141141
$this->dumpBuffer .= $line;
142142
if ($GLOBALS['onfly_compression']) {
143-
$this->dumpBufferLength += strlen((string) $line);
143+
$this->dumpBufferLength += strlen($line);
144144

145145
if ($this->dumpBufferLength > $GLOBALS['memory_limit']) {
146146
if ($GLOBALS['output_charset_conversion']) {
@@ -150,14 +150,14 @@ public function outputHandler(?string $line): bool
150150
if ($GLOBALS['compression'] === 'gzip' && $this->gzencodeNeeded()) {
151151
// as a gzipped file
152152
// without the optional parameter level because it bugs
153-
$this->dumpBuffer = gzencode($this->dumpBuffer);
153+
$this->dumpBuffer = (string) gzencode($this->dumpBuffer);
154154
}
155155

156156
if ($GLOBALS['save_on_server']) {
157-
$writeResult = @fwrite($GLOBALS['file_handle'], (string) $this->dumpBuffer);
157+
$writeResult = @fwrite($GLOBALS['file_handle'], $this->dumpBuffer);
158158
// Here, use strlen rather than mb_strlen to get the length
159159
// in bytes to compare against the number of bytes written.
160-
if ($writeResult != strlen((string) $this->dumpBuffer)) {
160+
if ($writeResult != strlen($this->dumpBuffer)) {
161161
$GLOBALS['message'] = Message::error(
162162
__('Insufficient space to save the file %s.')
163163
);
@@ -184,16 +184,16 @@ public function outputHandler(?string $line): bool
184184
$line = Encoding::convertString('utf-8', $GLOBALS['charset'], $line);
185185
}
186186

187-
if ($GLOBALS['save_on_server'] && mb_strlen((string) $line) > 0) {
187+
if ($GLOBALS['save_on_server'] && mb_strlen($line) > 0) {
188188
if ($GLOBALS['file_handle'] !== null) {
189-
$writeResult = @fwrite($GLOBALS['file_handle'], (string) $line);
189+
$writeResult = @fwrite($GLOBALS['file_handle'], $line);
190190
} else {
191191
$writeResult = false;
192192
}
193193

194194
// Here, use strlen rather than mb_strlen to get the length
195195
// in bytes to compare against the number of bytes written.
196-
if (! $writeResult || $writeResult != strlen((string) $line)) {
196+
if (! $writeResult || $writeResult != strlen($line)) {
197197
$GLOBALS['message'] = Message::error(
198198
__('Insufficient space to save the file %s.')
199199
);
@@ -213,7 +213,7 @@ public function outputHandler(?string $line): bool
213213
}
214214
} else {
215215
// We export as html - replace special chars
216-
echo htmlspecialchars((string) $line, ENT_COMPAT);
216+
echo htmlspecialchars($line, ENT_COMPAT);
217217
}
218218

219219
return true;
@@ -264,7 +264,7 @@ public function getMemoryLimit(): int
264264
$memoryLimitNumber = (int) substr($memoryLimit, 0, -1);
265265
$lowerLastChar = strtolower(substr($memoryLimit, -1));
266266
// 2 MB as default
267-
if (empty($memoryLimit) || $memoryLimit == '-1') {
267+
if ($memoryLimit === '' || $memoryLimit === '0' || $memoryLimit === '-1') {
268268
$memoryLimit = 2 * 1024 * 1024;
269269
} elseif ($lowerLastChar === 'm') {
270270
$memoryLimit = $memoryLimitNumber * 1024 * 1024;
@@ -350,31 +350,31 @@ public function getFilenameAndMimetype(
350350
string $filenameTemplate
351351
): array {
352352
if ($exportType === 'server') {
353-
if (! empty($rememberTemplate)) {
353+
if ($rememberTemplate !== '' && $rememberTemplate !== '0') {
354354
$GLOBALS['config']->setUserValue(
355355
'pma_server_filename_template',
356356
'Export/file_template_server',
357357
$filenameTemplate
358358
);
359359
}
360360
} elseif ($exportType === 'database') {
361-
if (! empty($rememberTemplate)) {
361+
if ($rememberTemplate !== '' && $rememberTemplate !== '0') {
362362
$GLOBALS['config']->setUserValue(
363363
'pma_db_filename_template',
364364
'Export/file_template_database',
365365
$filenameTemplate
366366
);
367367
}
368368
} elseif ($exportType === 'raw') {
369-
if (! empty($rememberTemplate)) {
369+
if ($rememberTemplate !== '' && $rememberTemplate !== '0') {
370370
$GLOBALS['config']->setUserValue(
371371
'pma_raw_filename_template',
372372
'Export/file_template_raw',
373373
$filenameTemplate
374374
);
375375
}
376376
} else {
377-
if (! empty($rememberTemplate)) {
377+
if ($rememberTemplate !== '' && $rememberTemplate !== '0') {
378378
$GLOBALS['config']->setUserValue(
379379
'pma_table_filename_template',
380380
'Export/file_template_table',
@@ -586,7 +586,7 @@ public function exportServer(
586586
array $aliases,
587587
string $separateFiles
588588
): void {
589-
if (! empty($dbSelect) && is_array($dbSelect)) {
589+
if (is_array($dbSelect) && $dbSelect !== []) {
590590
$tmpSelect = implode('|', $dbSelect);
591591
$tmpSelect = '|' . $tmpSelect . '|';
592592
}
@@ -1012,9 +1012,9 @@ public function exportTable(
10121012
// for example, a PDF report
10131013
// if it is a merge table, no data is exported
10141014
if ($whatStrucOrData === 'data' || $whatStrucOrData === 'structure_and_data') {
1015-
if (! empty($sqlQuery)) {
1015+
if ($sqlQuery !== '') {
10161016
// only preg_replace if needed
1017-
if (! empty($addQuery)) {
1017+
if ($addQuery !== '') {
10181018
// remove trailing semicolon before adding a LIMIT
10191019
$sqlQuery = preg_replace('%;\s*$%', '', $sqlQuery);
10201020
}
@@ -1129,7 +1129,7 @@ public function mergeAliases(array $aliases1, array $aliases2): array
11291129
$val1 = $db['alias'][0];
11301130
$val2 = $db['alias'][1];
11311131
// Use aliases2 alias if non empty
1132-
$aliases[$dbName]['alias'] = empty($val2) ? $val1 : $val2;
1132+
$aliases[$dbName]['alias'] = $val2 !== '' && $val2 !== null ? $val2 : $val1;
11331133
}
11341134

11351135
if (! isset($db['tables'])) {
@@ -1141,7 +1141,7 @@ public function mergeAliases(array $aliases1, array $aliases2): array
11411141
$val1 = $tbl['alias'][0];
11421142
$val2 = $tbl['alias'][1];
11431143
// Use aliases2 alias if non empty
1144-
$aliases[$dbName]['tables'][$tableName]['alias'] = empty($val2) ? $val1 : $val2;
1144+
$aliases[$dbName]['tables'][$tableName]['alias'] = $val2 !== '' && $val2 !== null ? $val2 : $val1;
11451145
}
11461146

11471147
if (! isset($tbl['columns'])) {
@@ -1156,7 +1156,7 @@ public function mergeAliases(array $aliases1, array $aliases2): array
11561156
$val1 = $colAs[0];
11571157
$val2 = $colAs[1];
11581158
// Use aliases2 alias if non empty
1159-
$aliases[$dbName]['tables'][$tableName]['columns'][$col] = empty($val2) ? $val1 : $val2;
1159+
$aliases[$dbName]['tables'][$tableName]['columns'][$col] = $val2 !== '' && $val2 !== null ? $val2 : $val1;
11601160
}
11611161
}
11621162
}

libraries/classes/Font.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ public function getStringWidth(
190190
?array $charLists = null
191191
): int {
192192
if (
193-
! isset($charLists[0]['chars'], $charLists[0]['modifier']) || empty($charLists)
193+
! isset($charLists[0]['chars'], $charLists[0]['modifier'])
194194
|| ! is_array($charLists[0]['chars'])
195195
) {
196196
$charLists = $this->getCharLists();

phpstan-baseline.neon

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,23 +1201,8 @@ parameters:
12011201
path: libraries/classes/Controllers/Export/ExportController.php
12021202

12031203
-
1204-
message: "#^Parameter \\#1 \\$dumpBuffer of method PhpMyAdmin\\\\Export\\:\\:compress\\(\\) expects array\\|string, mixed given\\.$#"
1205-
count: 1
1206-
path: libraries/classes/Controllers/Export/ExportController.php
1207-
1208-
-
1209-
message: "#^Parameter \\#1 \\(mixed\\) of echo cannot be converted to string\\.$#"
1210-
count: 1
1211-
path: libraries/classes/Controllers/Export/ExportController.php
1212-
1213-
-
1214-
message: "#^Parameter \\#2 \\$dumpBuffer of method PhpMyAdmin\\\\Export\\:\\:closeFile\\(\\) expects string, mixed given\\.$#"
1215-
count: 1
1216-
path: libraries/classes/Controllers/Export/ExportController.php
1217-
1218-
-
1219-
message: "#^Parameter \\#3 \\$what of static method PhpMyAdmin\\\\Encoding\\:\\:convertString\\(\\) expects string, mixed given\\.$#"
1220-
count: 1
1204+
message: "#^Property PhpMyAdmin\\\\Export\\:\\:\\$dumpBuffer \\(string\\) does not accept array\\|bool\\|string\\.$#"
1205+
count: 2
12211206
path: libraries/classes/Controllers/Export/ExportController.php
12221207

12231208
-
@@ -3335,16 +3320,6 @@ parameters:
33353320
count: 1
33363321
path: libraries/classes/Export.php
33373322

3338-
-
3339-
message: "#^Parameter \\#1 \\$str of static method PhpMyAdmin\\\\Encoding\\:\\:kanjiStrConv\\(\\) expects string, string\\|null given\\.$#"
3340-
count: 1
3341-
path: libraries/classes/Export.php
3342-
3343-
-
3344-
message: "#^Parameter \\#3 \\$what of static method PhpMyAdmin\\\\Encoding\\:\\:convertString\\(\\) expects string, string\\|null given\\.$#"
3345-
count: 1
3346-
path: libraries/classes/Export.php
3347-
33483323
-
33493324
message: "#^Property PhpMyAdmin\\\\Export\\:\\:\\$dumpBufferObjects type has no value type specified in iterable type array\\.$#"
33503325
count: 1
@@ -3460,11 +3435,6 @@ parameters:
34603435
count: 1
34613436
path: libraries/classes/Font.php
34623437

3463-
-
3464-
message: "#^Variable \\$charLists in empty\\(\\) always exists and is not falsy\\.$#"
3465-
count: 1
3466-
path: libraries/classes/Font.php
3467-
34683438
-
34693439
message: "#^Method PhpMyAdmin\\\\Footer\\:\\:removeRecursion\\(\\) has parameter \\$stack with no value type specified in iterable type array\\.$#"
34703440
count: 1

0 commit comments

Comments
 (0)