Skip to content

Commit fe42d34

Browse files
committed
Refactor Node::getData()
Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>
1 parent 9171993 commit fe42d34

8 files changed

Lines changed: 171 additions & 266 deletions

File tree

phpstan-baseline.neon

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7927,19 +7927,7 @@ parameters:
79277927
path: src/Navigation/NavigationTree.php
79287928

79297929
-
7930-
message: '#^Parameter \#2 \$item of class PhpMyAdmin\\Navigation\\Nodes\\NodeColumn constructor expects array\<mixed\>, mixed given\.$#'
7931-
identifier: argument.type
7932-
count: 1
7933-
path: src/Navigation/NavigationTree.php
7934-
7935-
-
7936-
message: '#^Parameter \#2 \$name of class PhpMyAdmin\\Navigation\\Nodes\\NodeDatabase constructor expects string, mixed given\.$#'
7937-
identifier: argument.type
7938-
count: 1
7939-
path: src/Navigation/NavigationTree.php
7940-
7941-
-
7942-
message: '#^Parameter \#2 \$name of class PhpMyAdmin\\Navigation\\Nodes\\NodeIndex constructor expects string, mixed given\.$#'
7930+
message: '#^Parameter \#2 \$name of class PhpMyAdmin\\Navigation\\Nodes\\NodeDatabase constructor expects string, string\|null given\.$#'
79437931
identifier: argument.type
79447932
count: 1
79457933
path: src/Navigation/NavigationTree.php
@@ -7950,36 +7938,18 @@ parameters:
79507938
count: 1
79517939
path: src/Navigation/NavigationTree.php
79527940

7953-
-
7954-
message: '#^Parameter \#2 \$name of class PhpMyAdmin\\Navigation\\Nodes\\NodeTrigger constructor expects string, mixed given\.$#'
7955-
identifier: argument.type
7956-
count: 1
7957-
path: src/Navigation/NavigationTree.php
7958-
79597941
-
79607942
message: '#^Parameter \#2 \$path of method PhpMyAdmin\\Navigation\\NavigationTree\:\:buildPathPart\(\) expects array\<mixed\>, mixed given\.$#'
79617943
identifier: argument.type
79627944
count: 1
79637945
path: src/Navigation/NavigationTree.php
79647946

7965-
-
7966-
message: '#^Parameter \#2 \$table of method PhpMyAdmin\\Navigation\\NavigationTree\:\:addTableContainers\(\) expects PhpMyAdmin\\Navigation\\Nodes\\NodeTable, PhpMyAdmin\\Navigation\\Nodes\\Node given\.$#'
7967-
identifier: argument.type
7968-
count: 1
7969-
path: src/Navigation/NavigationTree.php
7970-
79717947
-
79727948
message: '#^Parameter \#3 \$searchClause of method PhpMyAdmin\\Navigation\\Nodes\\NodeDatabase\:\:getPresence\(\) expects string, mixed given\.$#'
79737949
identifier: argument.type
79747950
count: 1
79757951
path: src/Navigation/NavigationTree.php
79767952

7977-
-
7978-
message: '#^Possibly invalid array key type mixed\.$#'
7979-
identifier: offsetAccess.invalidOffset
7980-
count: 2
7981-
path: src/Navigation/NavigationTree.php
7982-
79837953
-
79847954
message: '#^Property PhpMyAdmin\\Navigation\\NavigationTree\:\:\$pos2Name \(array\<string\>\) does not accept array\<mixed\>\.$#'
79857955
identifier: assign.propertyType
@@ -8127,19 +8097,13 @@ parameters:
81278097
Use dependency injection instead\.$#
81288098
'''
81298099
identifier: staticMethod.deprecated
8130-
count: 2
8131-
path: src/Navigation/Nodes/NodeTable.php
8132-
8133-
-
8134-
message: '#^Call to function in_array\(\) requires parameter \#3 to be set\.$#'
8135-
identifier: function.strict
81368100
count: 1
81378101
path: src/Navigation/Nodes/NodeTable.php
81388102

81398103
-
81408104
message: '#^Cannot access property \$realName on PhpMyAdmin\\Navigation\\Nodes\\Node\|false\.$#'
81418105
identifier: property.nonObject
8142-
count: 2
8106+
count: 4
81438107
path: src/Navigation/Nodes/NodeTable.php
81448108

81458109
-

psalm-baseline.xml

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5198,9 +5198,6 @@
51985198
</RiskyTruthyFalsyComparison>
51995199
</file>
52005200
<file src="src/Navigation/NavigationTree.php">
5201-
<ArgumentTypeCoercion>
5202-
<code><![CDATA[$table]]></code>
5203-
</ArgumentTypeCoercion>
52045201
<DeprecatedMethod>
52055202
<code><![CDATA[Config::getInstance()]]></code>
52065203
</DeprecatedMethod>
@@ -5217,10 +5214,6 @@
52175214
<code><![CDATA[non-empty-list<string>]]></code>
52185215
</InvalidReturnType>
52195216
<MixedArgument>
5220-
<code><![CDATA[$db]]></code>
5221-
<code><![CDATA[$item]]></code>
5222-
<code><![CDATA[$item]]></code>
5223-
<code><![CDATA[$item]]></code>
52245217
<code><![CDATA[$path]]></code>
52255218
<code><![CDATA[$path[0]]]></code>
52265219
<code><![CDATA[$path[0]]]></code>
@@ -5237,8 +5230,6 @@
52375230
<code><![CDATA[Current::$database]]></code>
52385231
</MixedArgument>
52395232
<MixedAssignment>
5240-
<code><![CDATA[$db]]></code>
5241-
<code><![CDATA[$item]]></code>
52425233
<code><![CDATA[$path]]></code>
52435234
<code><![CDATA[$path]]></code>
52445235
<code><![CDATA[$path]]></code>
@@ -5266,6 +5257,7 @@
52665257
<PossiblyNullArgument>
52675258
<code><![CDATA[$database]]></code>
52685259
<code><![CDATA[$database]]></code>
5260+
<code><![CDATA[$db]]></code>
52695261
</PossiblyNullArgument>
52705262
<PossiblyNullPropertyAssignmentValue>
52715263
<code><![CDATA[$container->parent->pos2]]></code>
@@ -5383,11 +5375,12 @@
53835375
<file src="src/Navigation/Nodes/NodeTable.php">
53845376
<DeprecatedMethod>
53855377
<code><![CDATA[DatabaseInterface::getInstance()]]></code>
5386-
<code><![CDATA[DatabaseInterface::getInstance()]]></code>
53875378
</DeprecatedMethod>
53885379
<PossiblyInvalidPropertyFetch>
53895380
<code><![CDATA[$this->getRealParent()->realName]]></code>
53905381
<code><![CDATA[$this->getRealParent()->realName]]></code>
5382+
<code><![CDATA[$this->getRealParent()->realName]]></code>
5383+
<code><![CDATA[$this->getRealParent()->realName]]></code>
53915384
</PossiblyInvalidPropertyFetch>
53925385
<PossiblyNullArgument>
53935386
<code><![CDATA[$arr['Type']]]></code>
@@ -6154,6 +6147,7 @@
61546147
</PossiblyNullReference>
61556148
<PossiblyUnusedReturnValue>
61566149
<code><![CDATA[bool]]></code>
6150+
<code><![CDATA[bool]]></code>
61576151
</PossiblyUnusedReturnValue>
61586152
<RiskyTruthyFalsyComparison>
61596153
<code><![CDATA[empty($column->collation)]]></code>

src/Navigation/NavigationTree.php

Lines changed: 11 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,13 @@
2323
use PhpMyAdmin\Navigation\Nodes\NodeColumnContainer;
2424
use PhpMyAdmin\Navigation\Nodes\NodeDatabase;
2525
use PhpMyAdmin\Navigation\Nodes\NodeDatabaseContainer;
26-
use PhpMyAdmin\Navigation\Nodes\NodeEvent;
2726
use PhpMyAdmin\Navigation\Nodes\NodeEventContainer;
28-
use PhpMyAdmin\Navigation\Nodes\NodeFunction;
2927
use PhpMyAdmin\Navigation\Nodes\NodeFunctionContainer;
30-
use PhpMyAdmin\Navigation\Nodes\NodeIndex;
3128
use PhpMyAdmin\Navigation\Nodes\NodeIndexContainer;
32-
use PhpMyAdmin\Navigation\Nodes\NodeProcedure;
3329
use PhpMyAdmin\Navigation\Nodes\NodeProcedureContainer;
3430
use PhpMyAdmin\Navigation\Nodes\NodeTable;
3531
use PhpMyAdmin\Navigation\Nodes\NodeTableContainer;
36-
use PhpMyAdmin\Navigation\Nodes\NodeTrigger;
3732
use PhpMyAdmin\Navigation\Nodes\NodeTriggerContainer;
38-
use PhpMyAdmin\Navigation\Nodes\NodeView;
3933
use PhpMyAdmin\Navigation\Nodes\NodeViewContainer;
4034
use PhpMyAdmin\ResponseRenderer;
4135
use PhpMyAdmin\Template;
@@ -310,13 +304,7 @@ private function buildPath(UserPrivileges $userPrivileges): Node|bool
310304
$retval = $this->tree;
311305

312306
// Add all databases unconditionally
313-
$data = $this->tree->getData(
314-
$userPrivileges,
315-
$this->relationParameters,
316-
'databases',
317-
$this->pos,
318-
$this->searchClause,
319-
);
307+
$data = $this->tree->getData($userPrivileges, $this->pos, $this->searchClause);
320308
$hiddenCounts = $this->tree->getNavigationHidingData($this->relationParameters->navigationItemsHidingFeature);
321309
foreach ($data as $db) {
322310
$node = new NodeDatabase($this->config, $db);
@@ -397,27 +385,13 @@ private function buildPathPart(
397385
}
398386

399387
if (count($container->children) <= 1) {
400-
$dbData = $db->getData(
401-
$userPrivileges,
388+
$dbData = $db->getDatabaseObjects(
402389
$this->relationParameters,
403390
$container->realName,
404391
$pos2,
405392
$this->searchClause2,
406393
);
407-
foreach ($dbData as $item) {
408-
$node = match ($container->realName) {
409-
'events' => new NodeEvent($this->config, $item),
410-
'functions' => new NodeFunction($this->config, $item),
411-
'procedures' => new NodeProcedure($this->config, $item),
412-
'tables' => new NodeTable($this->config, $item),
413-
'views' => new NodeView($this->config, $item),
414-
default => null,
415-
};
416-
417-
if ($node === null) {
418-
continue;
419-
}
420-
394+
foreach ($dbData as $node) {
421395
if ($type2 === $container->realName) {
422396
$node->pos2 = $pos2;
423397
}
@@ -448,6 +422,7 @@ private function buildPathPart(
448422
}
449423

450424
$container->addChild($node);
425+
/** @var NodeTable|null $table */
451426
$table = $container->getChild($path[0], true);
452427
if ($table === null) {
453428
return false;
@@ -465,19 +440,13 @@ private function buildPathPart(
465440
return false;
466441
}
467442

468-
$tableData = $table->getData($userPrivileges, $this->relationParameters, $container->realName, $pos3);
469-
foreach ($tableData as $item) {
470-
$node = match ($container->realName) {
471-
'indexes' => new NodeIndex($this->config, $item),
472-
'columns' => new NodeColumn($this->config, $item),
473-
'triggers' => new NodeTrigger($this->config, $item),
474-
default => null,
475-
};
476-
477-
if ($node === null) {
478-
continue;
479-
}
480-
443+
$tableData = match ($container->realName) {
444+
'indexes' => $table->getIndexes($this->dbi, $pos3),
445+
'columns' => $table->getColumns($this->dbi, $pos3),
446+
'triggers' => $table->getTriggers($this->dbi, $pos3),
447+
default => [],
448+
};
449+
foreach ($tableData as $node) {
481450
$node->pos2 = $container->parent->pos2;
482451
if ($type3 === $container->realName) {
483452
$node->pos3 = $pos3;

src/Navigation/Nodes/Node.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
use PhpMyAdmin\Config;
1111
use PhpMyAdmin\ConfigStorage\Features\NavigationItemsHidingFeature;
12-
use PhpMyAdmin\ConfigStorage\RelationParameters;
1312
use PhpMyAdmin\Dbal\ConnectionType;
1413
use PhpMyAdmin\Dbal\DatabaseInterface;
1514
use PhpMyAdmin\Html\Generator;
@@ -307,21 +306,15 @@ public function getPaths(): array
307306
}
308307

309308
/**
310-
* Returns the names of children of type $type present inside this container
311-
* This method is overridden by the PhpMyAdmin\Navigation\Nodes\NodeDatabase
312-
* and PhpMyAdmin\Navigation\Nodes\NodeTable classes
309+
* Returns the names of children present inside this container
313310
*
314-
* @param string $type The type of item we are looking for
315-
* ('tables', 'views', etc)
316311
* @param int $pos The offset of the list within the results
317312
* @param string $searchClause A string used to filter the results of the query
318313
*
319-
* @return mixed[]
314+
* @return list<string|null>
320315
*/
321-
public function getData(
316+
final public function getData(
322317
UserPrivileges $userPrivileges,
323-
RelationParameters $relationParameters,
324-
string $type,
325318
int $pos,
326319
string $searchClause = '',
327320
): array {

src/Navigation/Nodes/NodeDatabase.php

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -89,54 +89,56 @@ public function getPresence(UserPrivileges $userPrivileges, string $type = '', s
8989
};
9090
}
9191

92-
/**
93-
* Returns the names of children of type $type present inside this container
94-
* This method is overridden by the PhpMyAdmin\Navigation\Nodes\NodeDatabase
95-
* and PhpMyAdmin\Navigation\Nodes\NodeTable classes
96-
*
97-
* @param string $type The type of item we are looking for
98-
* ('tables', 'views', etc)
99-
* @param int $pos The offset of the list within the results
100-
* @param string $searchClause A string used to filter the results of the query
101-
*
102-
* @return string[]
103-
*/
104-
public function getData(
105-
UserPrivileges $userPrivileges,
92+
/** @return NodeDatabaseChild[] */
93+
public function getDatabaseObjects(
10694
RelationParameters $relationParameters,
10795
string $type,
10896
int $pos,
10997
string $searchClause = '',
11098
): array {
111-
$retval = match ($type) {
99+
if (! in_array($type, ['tables', 'views', 'procedures', 'functions', 'events'], true)) {
100+
return [];
101+
}
102+
103+
$names = match ($type) {
112104
'tables' => $this->objectFetcher->getTables($this->realName, $searchClause),
113105
'views' => $this->objectFetcher->getViews($this->realName, $searchClause),
114106
'procedures' => $this->objectFetcher->getProcedures($this->realName, $searchClause),
115107
'functions' => $this->objectFetcher->getFunctions($this->realName, $searchClause),
116108
'events' => $this->objectFetcher->getEvents($this->realName, $searchClause),
117-
default => [],
118109
};
119110

120111
$maxItems = $this->config->settings['MaxNavigationItems'];
121112

122113
if ($this->config->settings['NaturalOrder']) {
123-
usort($retval, strnatcasecmp(...));
114+
usort($names, strnatcasecmp(...));
124115
}
125116

126-
$retval = array_slice($retval, $pos, $maxItems);
117+
$names = array_slice($names, $pos, $maxItems);
127118

128119
// Remove hidden items so that they are not displayed in navigation tree
129120
if ($relationParameters->navigationItemsHidingFeature !== null) {
130121
$hiddenItems = $this->getHiddenItems($relationParameters, substr($type, 0, -1));
131-
foreach ($retval as $key => $item) {
122+
foreach ($names as $key => $item) {
132123
if (! in_array($item, $hiddenItems, true)) {
133124
continue;
134125
}
135126

136-
unset($retval[$key]);
127+
unset($names[$key]);
137128
}
138129
}
139130

131+
$retval = [];
132+
foreach ($names as $item) {
133+
$retval[] = match ($type) {
134+
'tables' => new NodeTable($this->config, $item),
135+
'views' => new NodeView($this->config, $item),
136+
'procedures' => new NodeProcedure($this->config, $item),
137+
'functions' => new NodeFunction($this->config, $item),
138+
'events' => new NodeEvent($this->config, $item),
139+
};
140+
}
141+
140142
return $retval;
141143
}
142144

0 commit comments

Comments
 (0)