Skip to content

Commit 8123a81

Browse files
committed
Remove $GLOBALS['cfg'] from BrowseForeigners class
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
1 parent e38a46f commit 8123a81

6 files changed

Lines changed: 115 additions & 69 deletions

File tree

‎libraries/classes/BrowseForeigners.php‎

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
namespace PhpMyAdmin;
99

10+
use PhpMyAdmin\Config\Settings;
11+
1012
use function __;
1113
use function array_keys;
1214
use function asort;
@@ -22,17 +24,11 @@
2224
*/
2325
class BrowseForeigners
2426
{
25-
private int $limitChars;
26-
private int $maxRows;
27-
private int $repeatCells;
28-
private bool $showAll;
27+
private Settings $settings;
2928

30-
public function __construct(public Template $template)
29+
public function __construct(public Template $template, Config $config)
3130
{
32-
$this->limitChars = (int) $GLOBALS['cfg']['LimitChars'];
33-
$this->maxRows = (int) $GLOBALS['cfg']['MaxRows'];
34-
$this->repeatCells = (int) $GLOBALS['cfg']['RepeatCells'];
35-
$this->showAll = (bool) $GLOBALS['cfg']['ShowAll'];
31+
$this->settings = $config->getSettings();
3632
}
3733

3834
/**
@@ -66,7 +62,7 @@ private function getHtmlForOneKey(
6662
$rightKeynameIsSelected = false;
6763
$leftKeynameIsSelected = false;
6864

69-
if ($this->repeatCells > 0 && $horizontalCount > $this->repeatCells) {
65+
if ($this->settings->repeatCells > 0 && $horizontalCount > $this->settings->repeatCells) {
7066
$output .= $header;
7167
$horizontalCount = 0;
7268
}
@@ -160,8 +156,8 @@ public function getHtmlForRelationalFieldSelection(
160156
$gotoPage = $this->getHtmlForGotoPage($foreignData);
161157
$foreignShowAll = $this->template->render('table/browse_foreigners/show_all', [
162158
'foreign_data' => $foreignData,
163-
'show_all' => $this->showAll,
164-
'max_rows' => $this->maxRows,
159+
'show_all' => $this->settings->showAll,
160+
'max_rows' => $this->settings->maxRows,
165161
]);
166162

167163
$output = '<form class="ajax" '
@@ -263,12 +259,11 @@ public function getHtmlForRelationalFieldSelection(
263259
*/
264260
private function getDescriptionAndTitle(string $description): array
265261
{
266-
if (mb_strlen($description) <= $this->limitChars) {
262+
if (mb_strlen($description) <= $this->settings->limitChars) {
267263
$descriptionTitle = '';
268264
} else {
269265
$descriptionTitle = $description;
270-
$description = mb_substr($description, 0, $this->limitChars)
271-
. '...';
266+
$description = mb_substr($description, 0, $this->settings->limitChars) . '...';
272267
}
273268

274269
return [
@@ -289,13 +284,13 @@ private function getHtmlForGotoPage(array|null $foreignData): string
289284
return '';
290285
}
291286

292-
$pageNow = (int) floor($pos / $this->maxRows) + 1;
293-
$nbTotalPage = (int) ceil($foreignData['the_total'] / $this->maxRows);
287+
$pageNow = (int) floor($pos / $this->settings->maxRows) + 1;
288+
$nbTotalPage = (int) ceil($foreignData['the_total'] / $this->settings->maxRows);
294289

295-
if ($foreignData['the_total'] > $this->maxRows) {
290+
if ($foreignData['the_total'] > $this->settings->maxRows) {
296291
return Util::pageselector(
297292
'pos',
298-
$this->maxRows,
293+
$this->settings->maxRows,
299294
$pageNow,
300295
$nbTotalPage,
301296
200,
@@ -323,6 +318,6 @@ public function getForeignLimit(string|null $foreignShowAll): string|null
323318

324319
isset($_POST['pos']) ? $pos = $_POST['pos'] : $pos = 0;
325320

326-
return 'LIMIT ' . $pos . ', ' . $this->maxRows . ' ';
321+
return 'LIMIT ' . $pos . ', ' . $this->settings->maxRows . ' ';
327322
}
328323
}

‎libraries/classes/Config/Settings.php‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,7 +1182,7 @@ final class Settings
11821182
*
11831183
* @link https://docs.phpmyadmin.net/en/latest/config.html#cfg_ShowAll
11841184
*/
1185-
public bool $ShowAll;
1185+
public bool $showAll;
11861186

11871187
/**
11881188
* Number of rows displayed when browsing a result set. If the result
@@ -1197,7 +1197,7 @@ final class Settings
11971197
*
11981198
* @psalm-var positive-int
11991199
*/
1200-
public int $MaxRows;
1200+
public int $maxRows;
12011201

12021202
/**
12031203
* default for 'ORDER BY' clause (valid values are 'ASC', 'DESC' or 'SMART' -ie
@@ -1809,7 +1809,7 @@ final class Settings
18091809
*
18101810
* @psalm-var positive-int
18111811
*/
1812-
public int $LimitChars;
1812+
public int $limitChars;
18131813

18141814
/**
18151815
* Where to show the edit/copy/delete links in browse mode
@@ -1894,7 +1894,7 @@ final class Settings
18941894
*
18951895
* @psalm-var 0|positive-int
18961896
*/
1897-
public int $RepeatCells;
1897+
public int $repeatCells;
18981898

18991899
/**
19001900
* Set to true if you want DB-based query history.If false, this utilizes
@@ -2503,8 +2503,8 @@ public function __construct(array $settings)
25032503
$this->HideStructureActions = $this->setHideStructureActions($settings);
25042504
$this->ShowColumnComments = $this->setShowColumnComments($settings);
25052505
$this->TableNavigationLinksMode = $this->setTableNavigationLinksMode($settings);
2506-
$this->ShowAll = $this->setShowAll($settings);
2507-
$this->MaxRows = $this->setMaxRows($settings);
2506+
$this->showAll = $this->setShowAll($settings);
2507+
$this->maxRows = $this->setMaxRows($settings);
25082508
$this->Order = $this->setOrder($settings);
25092509
$this->SaveCellsAtOnce = $this->setSaveCellsAtOnce($settings);
25102510
$this->GridEditing = $this->setGridEditing($settings);
@@ -2551,14 +2551,14 @@ public function __construct(array $settings)
25512551
$this->TextareaAutoSelect = $this->setTextareaAutoSelect($settings);
25522552
$this->CharTextareaCols = $this->setCharTextareaCols($settings);
25532553
$this->CharTextareaRows = $this->setCharTextareaRows($settings);
2554-
$this->LimitChars = $this->setLimitChars($settings);
2554+
$this->limitChars = $this->setLimitChars($settings);
25552555
$this->RowActionLinks = $this->setRowActionLinks($settings);
25562556
$this->RowActionLinksWithoutUnique = $this->setRowActionLinksWithoutUnique($settings);
25572557
$this->TablePrimaryKeyOrder = $this->setTablePrimaryKeyOrder($settings);
25582558
$this->RememberSorting = $this->setRememberSorting($settings);
25592559
$this->ShowBrowseComments = $this->setShowBrowseComments($settings);
25602560
$this->ShowPropertyComments = $this->setShowPropertyComments($settings);
2561-
$this->RepeatCells = $this->setRepeatCells($settings);
2561+
$this->repeatCells = $this->setRepeatCells($settings);
25622562
$this->QueryHistoryDB = $this->setQueryHistoryDB($settings);
25632563
$this->QueryHistoryMax = $this->setQueryHistoryMax($settings);
25642564
$this->BrowseMIME = $this->setBrowseMIME($settings);
@@ -2700,8 +2700,8 @@ public function asArray(): array
27002700
'HideStructureActions' => $this->HideStructureActions,
27012701
'ShowColumnComments' => $this->ShowColumnComments,
27022702
'TableNavigationLinksMode' => $this->TableNavigationLinksMode,
2703-
'ShowAll' => $this->ShowAll,
2704-
'MaxRows' => $this->MaxRows,
2703+
'ShowAll' => $this->showAll,
2704+
'MaxRows' => $this->maxRows,
27052705
'Order' => $this->Order,
27062706
'SaveCellsAtOnce' => $this->SaveCellsAtOnce,
27072707
'GridEditing' => $this->GridEditing,
@@ -2748,14 +2748,14 @@ public function asArray(): array
27482748
'TextareaAutoSelect' => $this->TextareaAutoSelect,
27492749
'CharTextareaCols' => $this->CharTextareaCols,
27502750
'CharTextareaRows' => $this->CharTextareaRows,
2751-
'LimitChars' => $this->LimitChars,
2751+
'LimitChars' => $this->limitChars,
27522752
'RowActionLinks' => $this->RowActionLinks,
27532753
'RowActionLinksWithoutUnique' => $this->RowActionLinksWithoutUnique,
27542754
'TablePrimaryKeyOrder' => $this->TablePrimaryKeyOrder,
27552755
'RememberSorting' => $this->RememberSorting,
27562756
'ShowBrowseComments' => $this->ShowBrowseComments,
27572757
'ShowPropertyComments' => $this->ShowPropertyComments,
2758-
'RepeatCells' => $this->RepeatCells,
2758+
'RepeatCells' => $this->repeatCells,
27592759
'QueryHistoryDB' => $this->QueryHistoryDB,
27602760
'QueryHistoryMax' => $this->QueryHistoryMax,
27612761
'BrowseMIME' => $this->BrowseMIME,

‎libraries/services.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
],
1818
'browse_foreigners' => [
1919
'class' => PhpMyAdmin\BrowseForeigners::class,
20-
'arguments' => ['@template'],
20+
'arguments' => ['@template', '@config'],
2121
],
2222
'config' => [
2323
'class' => PhpMyAdmin\Config::class,

‎psalm-baseline.xml‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
<MixedArgument>
7272
<code>$descriptions[$indexByDescription]</code>
7373
<code>$descriptions[$indexByKeyname]</code>
74-
<code><![CDATA[$foreignData['the_total'] / $this->maxRows]]></code>
74+
<code><![CDATA[$foreignData['the_total'] / $this->settings->maxRows]]></code>
7575
<code>$horizontalCount</code>
7676
<code>$indexByDescription</code>
7777
</MixedArgument>
@@ -97,23 +97,18 @@
9797
</MixedOperand>
9898
<PossiblyInvalidArgument>
9999
<code><![CDATA[$_POST['foreign_filter']]]></code>
100+
<code><![CDATA[$pos / $this->settings->maxRows]]></code>
100101
</PossiblyInvalidArgument>
101102
<PossiblyInvalidCast>
102103
<code><![CDATA[$_POST['rownumber']]]></code>
103104
</PossiblyInvalidCast>
104105
<PossiblyInvalidOperand>
105106
<code>$pos</code>
106-
<code><![CDATA[$this->maxRows]]></code>
107+
<code><![CDATA[$this->settings->maxRows]]></code>
107108
</PossiblyInvalidOperand>
108109
<PossiblyUndefinedArrayOffset>
109110
<code><![CDATA[$foreignData['foreign_display']]]></code>
110111
</PossiblyUndefinedArrayOffset>
111-
<RedundantCast>
112-
<code><![CDATA[(bool) $GLOBALS['cfg']['ShowAll']]]></code>
113-
<code><![CDATA[(int) $GLOBALS['cfg']['LimitChars']]]></code>
114-
<code><![CDATA[(int) $GLOBALS['cfg']['MaxRows']]]></code>
115-
<code><![CDATA[(int) $GLOBALS['cfg']['RepeatCells']]]></code>
116-
</RedundantCast>
117112
</file>
118113
<file src="libraries/classes/Cache.php">
119114
<MixedAssignment>

‎test/classes/BrowseForeignersTest.php‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace PhpMyAdmin\Tests;
66

77
use PhpMyAdmin\BrowseForeigners;
8+
use PhpMyAdmin\Config;
89
use PhpMyAdmin\Template;
910

1011
/** @covers \PhpMyAdmin\BrowseForeigners */
@@ -21,11 +22,7 @@ protected function setUp(): void
2122

2223
parent::setTheme();
2324

24-
$GLOBALS['cfg']['LimitChars'] = 50;
25-
$GLOBALS['cfg']['MaxRows'] = 25;
26-
$GLOBALS['cfg']['RepeatCells'] = 100;
27-
$GLOBALS['cfg']['ShowAll'] = false;
28-
$this->browseForeigners = new BrowseForeigners(new Template());
25+
$this->browseForeigners = new BrowseForeigners(new Template(), new Config());
2926
}
3027

3128
/**
@@ -49,8 +46,9 @@ public function testGetForeignLimit(): void
4946
$this->browseForeigners->getForeignLimit(null),
5047
);
5148

52-
$GLOBALS['cfg']['MaxRows'] = 50;
53-
$browseForeigners = new BrowseForeigners(new Template());
49+
$config = new Config();
50+
$config->settings['MaxRows'] = 50;
51+
$browseForeigners = new BrowseForeigners(new Template(), $config);
5452

5553
$this->assertEquals(
5654
'LIMIT 10, 50 ',
@@ -132,8 +130,9 @@ public function testGetDescriptionAndTitle(): void
132130
),
133131
);
134132

135-
$GLOBALS['cfg']['LimitChars'] = 5;
136-
$browseForeigners = new BrowseForeigners(new Template());
133+
$config = new Config();
134+
$config->settings['LimitChars'] = 5;
135+
$browseForeigners = new BrowseForeigners(new Template(), $config);
137136

138137
$this->assertEquals(
139138
[

0 commit comments

Comments
 (0)