Skip to content

Commit 674e73a

Browse files
Merge pull request #20012 from kamil-tekiela/Refactor-Node-getData
Refactor Node::getData()
2 parents 0745735 + fe42d34 commit 674e73a

File tree

8 files changed

+171
-266
lines changed

8 files changed

+171
-266
lines changed

phpstan-baseline.neon

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

77527752
-
7753-
message: '#^Parameter \#2 \$item of class PhpMyAdmin\\Navigation\\Nodes\\NodeColumn constructor expects array\<mixed\>, mixed given\.$#'
7754-
identifier: argument.type
7755-
count: 1
7756-
path: src/Navigation/NavigationTree.php
7757-
7758-
-
7759-
message: '#^Parameter \#2 \$name of class PhpMyAdmin\\Navigation\\Nodes\\NodeDatabase constructor expects string, mixed given\.$#'
7760-
identifier: argument.type
7761-
count: 1
7762-
path: src/Navigation/NavigationTree.php
7763-
7764-
-
7765-
message: '#^Parameter \#2 \$name of class PhpMyAdmin\\Navigation\\Nodes\\NodeIndex constructor expects string, mixed given\.$#'
7753+
message: '#^Parameter \#2 \$name of class PhpMyAdmin\\Navigation\\Nodes\\NodeDatabase constructor expects string, string\|null given\.$#'
77667754
identifier: argument.type
77677755
count: 1
77687756
path: src/Navigation/NavigationTree.php
@@ -7773,36 +7761,18 @@ parameters:
77737761
count: 1
77747762
path: src/Navigation/NavigationTree.php
77757763

7776-
-
7777-
message: '#^Parameter \#2 \$name of class PhpMyAdmin\\Navigation\\Nodes\\NodeTrigger constructor expects string, mixed given\.$#'
7778-
identifier: argument.type
7779-
count: 1
7780-
path: src/Navigation/NavigationTree.php
7781-
77827764
-
77837765
message: '#^Parameter \#2 \$path of method PhpMyAdmin\\Navigation\\NavigationTree\:\:buildPathPart\(\) expects array\<mixed\>, mixed given\.$#'
77847766
identifier: argument.type
77857767
count: 1
77867768
path: src/Navigation/NavigationTree.php
77877769

7788-
-
7789-
message: '#^Parameter \#2 \$table of method PhpMyAdmin\\Navigation\\NavigationTree\:\:addTableContainers\(\) expects PhpMyAdmin\\Navigation\\Nodes\\NodeTable, PhpMyAdmin\\Navigation\\Nodes\\Node given\.$#'
7790-
identifier: argument.type
7791-
count: 1
7792-
path: src/Navigation/NavigationTree.php
7793-
77947770
-
77957771
message: '#^Parameter \#3 \$searchClause of method PhpMyAdmin\\Navigation\\Nodes\\NodeDatabase\:\:getPresence\(\) expects string, mixed given\.$#'
77967772
identifier: argument.type
77977773
count: 1
77987774
path: src/Navigation/NavigationTree.php
77997775

7800-
-
7801-
message: '#^Possibly invalid array key type mixed\.$#'
7802-
identifier: offsetAccess.invalidOffset
7803-
count: 2
7804-
path: src/Navigation/NavigationTree.php
7805-
78067776
-
78077777
message: '#^Property PhpMyAdmin\\Navigation\\NavigationTree\:\:\$pos2Name \(array\<string\>\) does not accept array\<mixed\>\.$#'
78087778
identifier: assign.propertyType
@@ -7950,19 +7920,13 @@ parameters:
79507920
Use dependency injection instead\.$#
79517921
'''
79527922
identifier: staticMethod.deprecated
7953-
count: 2
7954-
path: src/Navigation/Nodes/NodeTable.php
7955-
7956-
-
7957-
message: '#^Call to function in_array\(\) requires parameter \#3 to be set\.$#'
7958-
identifier: function.strict
79597923
count: 1
79607924
path: src/Navigation/Nodes/NodeTable.php
79617925

79627926
-
79637927
message: '#^Cannot access property \$realName on PhpMyAdmin\\Navigation\\Nodes\\Node\|false\.$#'
79647928
identifier: property.nonObject
7965-
count: 2
7929+
count: 4
79667930
path: src/Navigation/Nodes/NodeTable.php
79677931

79687932
-

psalm-baseline.xml

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5178,9 +5178,6 @@
51785178
</RiskyTruthyFalsyComparison>
51795179
</file>
51805180
<file src="src/Navigation/NavigationTree.php">
5181-
<ArgumentTypeCoercion>
5182-
<code><![CDATA[$table]]></code>
5183-
</ArgumentTypeCoercion>
51845181
<DeprecatedMethod>
51855182
<code><![CDATA[Config::getInstance()]]></code>
51865183
</DeprecatedMethod>
@@ -5197,10 +5194,6 @@
51975194
<code><![CDATA[non-empty-list<string>]]></code>
51985195
</InvalidReturnType>
51995196
<MixedArgument>
5200-
<code><![CDATA[$db]]></code>
5201-
<code><![CDATA[$item]]></code>
5202-
<code><![CDATA[$item]]></code>
5203-
<code><![CDATA[$item]]></code>
52045197
<code><![CDATA[$path]]></code>
52055198
<code><![CDATA[$path[0]]]></code>
52065199
<code><![CDATA[$path[0]]]></code>
@@ -5217,8 +5210,6 @@
52175210
<code><![CDATA[Current::$database]]></code>
52185211
</MixedArgument>
52195212
<MixedAssignment>
5220-
<code><![CDATA[$db]]></code>
5221-
<code><![CDATA[$item]]></code>
52225213
<code><![CDATA[$path]]></code>
52235214
<code><![CDATA[$path]]></code>
52245215
<code><![CDATA[$path]]></code>
@@ -5246,6 +5237,7 @@
52465237
<PossiblyNullArgument>
52475238
<code><![CDATA[$database]]></code>
52485239
<code><![CDATA[$database]]></code>
5240+
<code><![CDATA[$db]]></code>
52495241
</PossiblyNullArgument>
52505242
<PossiblyNullPropertyAssignmentValue>
52515243
<code><![CDATA[$container->parent->pos2]]></code>
@@ -5363,11 +5355,12 @@
53635355
<file src="src/Navigation/Nodes/NodeTable.php">
53645356
<DeprecatedMethod>
53655357
<code><![CDATA[DatabaseInterface::getInstance()]]></code>
5366-
<code><![CDATA[DatabaseInterface::getInstance()]]></code>
53675358
</DeprecatedMethod>
53685359
<PossiblyInvalidPropertyFetch>
53695360
<code><![CDATA[$this->getRealParent()->realName]]></code>
53705361
<code><![CDATA[$this->getRealParent()->realName]]></code>
5362+
<code><![CDATA[$this->getRealParent()->realName]]></code>
5363+
<code><![CDATA[$this->getRealParent()->realName]]></code>
53715364
</PossiblyInvalidPropertyFetch>
53725365
<PossiblyNullArgument>
53735366
<code><![CDATA[$arr['Type']]]></code>
@@ -6121,6 +6114,7 @@
61216114
</PossiblyNullReference>
61226115
<PossiblyUnusedReturnValue>
61236116
<code><![CDATA[bool]]></code>
6117+
<code><![CDATA[bool]]></code>
61246118
</PossiblyUnusedReturnValue>
61256119
<RiskyTruthyFalsyComparison>
61266120
<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)