Skip to content

Commit bcd2b94

Browse files
Merge pull request #19461 from kamil-tekiela/fetchResult
Creation of fetchResultSimple and fetchSingleColumn
2 parents 0390fd2 + a07292e commit bcd2b94

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+362
-681
lines changed

phpstan-baseline.neon

Lines changed: 18 additions & 240 deletions
Large diffs are not rendered by default.

psalm-baseline.xml

Lines changed: 43 additions & 149 deletions
Large diffs are not rendered by default.

src/Bookmarks/BookmarkRepository.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public function getList(
9595

9696
$query .= ' ORDER BY label ASC';
9797

98-
$result = $this->dbi->fetchResult($query, null, null, ConnectionType::ControlUser);
98+
$result = $this->dbi->fetchResultSimple($query, ConnectionType::ControlUser);
9999

100100
$bookmarks = [];
101101
foreach ($result as $row) {
@@ -134,7 +134,7 @@ public function get(
134134
$query .= ' LIMIT 1';
135135

136136
$result = $this->dbi->fetchSingleRow($query, DatabaseInterface::FETCH_ASSOC, ConnectionType::ControlUser);
137-
if ($result !== null) {
137+
if ($result !== []) {
138138
return $this->createFromRow($result);
139139
}
140140

@@ -162,7 +162,7 @@ public function getByLabel(
162162
. ' LIMIT 1';
163163

164164
$result = $this->dbi->fetchSingleRow($query, DatabaseInterface::FETCH_ASSOC, ConnectionType::ControlUser);
165-
if ($result !== null) {
165+
if ($result !== []) {
166166
return $this->createFromRow($result);
167167
}
168168

src/ConfigStorage/Relation.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ public function getDisplayField(string $db, string $table): string
478478
. ' AND `table_name` = ' . $this->dbi->quoteString($table);
479479

480480
$row = $this->dbi->fetchSingleRow($dispQuery, DatabaseInterface::FETCH_ASSOC, ConnectionType::ControlUser);
481-
if (isset($row['display_field'])) {
481+
if ($row['display_field'] !== null) {
482482
return $row['display_field'];
483483
}
484484
}
@@ -662,9 +662,9 @@ public function setHistory(string $db, string $table, string $username, string $
662662
*
663663
* @param string $username the username
664664
*
665-
* @return mixed[]|bool list of history items
665+
* @return mixed[]|false list of history items
666666
*/
667-
public function getHistory(string $username): array|bool
667+
public function getHistory(string $username): array|false
668668
{
669669
$sqlHistoryFeature = $this->getRelationParameters()->sqlHistoryFeature;
670670
if ($sqlHistoryFeature === null) {
@@ -693,7 +693,7 @@ public function getHistory(string $username): array|bool
693693
WHERE `username` = ' . $this->dbi->quoteString($username) . '
694694
ORDER BY `id` DESC';
695695

696-
return $this->dbi->fetchResult($histQuery, null, null, ConnectionType::ControlUser);
696+
return $this->dbi->fetchResultSimple($histQuery, ConnectionType::ControlUser);
697697
}
698698

699699
/**

src/Controllers/Database/MultiTableQuery/TablesController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function __invoke(ServerRequest $request): Response
2929

3030
$tablesListForQuery = array_map($this->dbi->quoteString(...), $tables);
3131

32-
$constrains = $this->dbi->fetchResult(
32+
$constrains = $this->dbi->fetchResultSimple(
3333
QueryGenerator::getInformationSchemaForeignKeyConstraintsRequest(
3434
$this->dbi->quoteString($db),
3535
implode(',', $tablesListForQuery),

src/Controllers/Table/SearchController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,9 +304,9 @@ private function rangeSearchAction(): void
304304
*
305305
* @param string $column Column name
306306
*
307-
* @return mixed[]|null
307+
* @return array<string|null>
308308
*/
309-
private function getColumnMinMax(string $column): array|null
309+
private function getColumnMinMax(string $column): array
310310
{
311311
$sqlQuery = 'SELECT MIN(' . Util::backquote($column) . ') AS `min`, '
312312
. 'MAX(' . Util::backquote($column) . ') AS `max` '

src/Database/CentralColumns.php

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
use function in_array;
3232
use function is_array;
3333
use function is_bool;
34+
use function is_string;
3435
use function mb_strtoupper;
3536
use function sprintf;
3637
use function trim;
@@ -100,7 +101,7 @@ public function getParams(): array|false
100101
* @param int $from starting offset of first result
101102
* @param int $num maximum number of results to return
102103
*
103-
* @return mixed[] list of $num columns present in central columns list
104+
* @return list<array<string|null>> list of $num columns present in central columns list
104105
* starting at offset $from for the given database
105106
*/
106107
public function getColumnsList(string $db, int $from = 0, int $num = 25): array
@@ -122,10 +123,9 @@ public function getColumnsList(string $db, int $from = 0, int $num = 25): array
122123
. 'LIMIT ' . $from . ', ' . $num . ';';
123124
}
124125

125-
$hasList = $this->dbi->fetchResult($query, null, null, ConnectionType::ControlUser);
126-
$this->handleColumnExtra($hasList);
126+
$hasList = $this->dbi->fetchResultSimple($query, ConnectionType::ControlUser);
127127

128-
return $hasList;
128+
return $this->handleColumnExtra($hasList);
129129
}
130130

131131
/**
@@ -147,12 +147,8 @@ public function getCount(string $db): int
147147
$query = 'SELECT count(db_name) FROM '
148148
. Util::backquote($pmadb) . '.' . Util::backquote($centralListTable) . ' '
149149
. 'WHERE db_name = ' . $this->dbi->quoteString($db, ConnectionType::ControlUser) . ';';
150-
$res = $this->dbi->fetchResult($query, null, null, ConnectionType::ControlUser);
151-
if (isset($res[0])) {
152-
return (int) $res[0];
153-
}
154150

155-
return 0;
151+
return (int) $this->dbi->fetchValue($query, 0, ConnectionType::ControlUser);
156152
}
157153

158154
/**
@@ -180,7 +176,7 @@ public function findExistingColNames(
180176
. Util::backquote($pmadb) . '.' . Util::backquote($centralListTable) . ' WHERE db_name = '
181177
. $this->dbi->quoteString($db, ConnectionType::ControlUser) . ' AND col_name IN (' . $cols . ');';
182178

183-
return $this->dbi->fetchResult($query, null, null, ConnectionType::ControlUser);
179+
return $this->dbi->fetchSingleColumn($query, ConnectionType::ControlUser);
184180
}
185181

186182
/**
@@ -207,10 +203,9 @@ private function findExistingColumns(
207203
$query = 'SELECT * FROM '
208204
. Util::backquote($pmadb) . '.' . Util::backquote($centralListTable) . ' WHERE db_name = '
209205
. $this->dbi->quoteString($db, ConnectionType::ControlUser) . ' AND col_name IN (' . $cols . ');';
210-
$hasList = $this->dbi->fetchResult($query, null, null, ConnectionType::ControlUser);
211-
$this->handleColumnExtra($hasList);
206+
$hasList = $this->dbi->fetchResultSimple($query, ConnectionType::ControlUser);
212207

213-
return $hasList;
208+
return $this->handleColumnExtra($hasList);
214209
}
215210

216211
/**
@@ -667,7 +662,7 @@ private function getHtmlForEditTableRow(array $row, int $rowNum): string
667662
* @param string $db selected database
668663
* @param string $table current table name
669664
*
670-
* @return mixed[] encoded list of columns present in central list for the given database
665+
* @return list<array<string|null>> encoded list of columns present in central list for the given database
671666
*/
672667
public function getListRaw(string $db, string $table): array
673668
{
@@ -692,19 +687,20 @@ public function getListRaw(string $db, string $table): array
692687
$query .= ';';
693688
}
694689

695-
$columnsList = $this->dbi->fetchResult($query, null, null, ConnectionType::ControlUser);
696-
$this->handleColumnExtra($columnsList);
690+
$columnsList = $this->dbi->fetchResultSimple($query, ConnectionType::ControlUser);
697691

698-
return $columnsList;
692+
return $this->handleColumnExtra($columnsList);
699693
}
700694

701695
/**
702696
* Column `col_extra` is used to store both extra and attributes for a column.
703697
* This method separates them.
704698
*
705-
* @param mixed[] $columnsList columns list
699+
* @param list<array<string|null>> $columnsList columns list
700+
*
701+
* @return list<array<string|null>>
706702
*/
707-
private function handleColumnExtra(array &$columnsList): void
703+
private function handleColumnExtra(array $columnsList): array
708704
{
709705
foreach ($columnsList as &$row) {
710706
$vals = explode(',', $row['col_extra']);
@@ -723,6 +719,8 @@ private function handleColumnExtra(array &$columnsList): void
723719

724720
$row['col_extra'] = in_array('auto_increment', $vals, true) ? 'auto_increment' : '';
725721
}
722+
723+
return $columnsList;
726724
}
727725

728726
/**
@@ -771,10 +769,10 @@ public function getColumnsCount(string $db, int $from = 0, int $num = 25): int
771769
$query = 'SELECT COUNT(db_name) FROM ' . Util::backquote($pmadb) . '.' . Util::backquote($centralListTable)
772770
. ' WHERE db_name = ' . $this->dbi->quoteString($db, ConnectionType::ControlUser)
773771
. ($num === 0 ? '' : 'LIMIT ' . $from . ', ' . $num) . ';';
774-
$result = $this->dbi->fetchResult($query, null, null, ConnectionType::ControlUser);
772+
$result = $this->dbi->fetchValue($query, 0, ConnectionType::ControlUser);
775773

776-
if (isset($result[0])) {
777-
return (int) $result[0];
774+
if (is_string($result)) {
775+
return (int) $result;
778776
}
779777

780778
return -1;

src/Database/Designer.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use stdClass;
1818

1919
use function __;
20-
use function is_array;
2120
use function json_decode;
2221
use function str_contains;
2322

@@ -146,7 +145,7 @@ private function getSideMenuParamsArray(): array
146145
. ';';
147146

148147
$result = $this->dbi->fetchSingleRow($query);
149-
if (is_array($result)) {
148+
if ($result !== []) {
150149
$params = json_decode((string) $result['settings_data'], true);
151150
}
152151
}

src/Database/Designer/Common.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ public function getPageExists(string $pg): bool
330330
. ' FROM ' . Util::backquote($pdfFeature->database)
331331
. '.' . Util::backquote($pdfFeature->pdfPages)
332332
. ' WHERE `page_descr` = ' . $this->dbi->quoteString($pg, ConnectionType::ControlUser);
333-
$pageNos = $this->dbi->fetchResult($query, null, null, ConnectionType::ControlUser);
333+
$pageNos = $this->dbi->fetchResultSimple($query, ConnectionType::ControlUser);
334334

335335
return $pageNos !== [];
336336
}
@@ -669,7 +669,7 @@ public function saveSetting(string $index, string $value): bool
669669
ConnectionType::ControlUser,
670670
);
671671

672-
if ($origData !== null && $origData !== []) {
672+
if ($origData !== []) {
673673
$origData = json_decode($origData['settings_data'], true);
674674
$origData[$index] = $value;
675675
$origData = json_encode($origData);

src/Database/Events.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ public function getDataFromName(string $name): array|null
223223
. ' AND EVENT_NAME=' . $this->dbi->quoteString($name);
224224
$query = 'SELECT ' . $columns . ' FROM `INFORMATION_SCHEMA`.`EVENTS` WHERE ' . $where . ';';
225225
$item = $this->dbi->fetchSingleRow($query);
226-
if ($item === null || $item === []) {
226+
if ($item === []) {
227227
return null;
228228
}
229229

@@ -367,7 +367,7 @@ public function getDetails(string $db, string $name = ''): array
367367
}
368368

369369
$result = [];
370-
$events = $this->dbi->fetchResult($query);
370+
$events = $this->dbi->fetchResultSimple($query);
371371

372372
/** @var string[] $event */
373373
foreach ($events as $event) {

0 commit comments

Comments
 (0)