Skip to content

Commit e1491d4

Browse files
Merge pull request #19441 from kamil-tekiela/Node-refactoring
Refactoring of Navigation Node
2 parents 5f7957c + 003bc41 commit e1491d4

11 files changed

Lines changed: 80 additions & 180 deletions

File tree

phpstan-baseline.neon

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -10758,7 +10758,7 @@ parameters:
1075810758
-
1075910759
message: '#^Access to an undefined property PhpMyAdmin\\Navigation\\Nodes\\Node\:\:\$secondIcon\.$#'
1076010760
identifier: property.notFound
10761-
count: 2
10761+
count: 1
1076210762
path: src/Navigation/NavigationTree.php
1076310763

1076410764
-
@@ -10767,36 +10767,6 @@ parameters:
1076710767
count: 1
1076810768
path: src/Navigation/NavigationTree.php
1076910769

10770-
-
10771-
message: '#^Binary operation "\." between mixed and string results in an error\.$#'
10772-
identifier: binaryOp.invalid
10773-
count: 2
10774-
path: src/Navigation/NavigationTree.php
10775-
10776-
-
10777-
message: '#^Binary operation "\." between string and mixed results in an error\.$#'
10778-
identifier: binaryOp.invalid
10779-
count: 1
10780-
path: src/Navigation/NavigationTree.php
10781-
10782-
-
10783-
message: '#^Call to an undefined method PhpMyAdmin\\Navigation\\Nodes\\Node\:\:setHiddenCount\(\)\.$#'
10784-
identifier: method.notFound
10785-
count: 1
10786-
path: src/Navigation/NavigationTree.php
10787-
10788-
-
10789-
message: '#^Cannot access offset ''image'' on mixed\.$#'
10790-
identifier: offsetAccess.nonOffsetAccessible
10791-
count: 1
10792-
path: src/Navigation/NavigationTree.php
10793-
10794-
-
10795-
message: '#^Cannot access offset ''title'' on mixed\.$#'
10796-
identifier: offsetAccess.nonOffsetAccessible
10797-
count: 1
10798-
path: src/Navigation/NavigationTree.php
10799-
1080010770
-
1080110771
message: '#^Cannot access offset \(int\|string\) on mixed\.$#'
1080210772
identifier: offsetAccess.nonOffsetAccessible
@@ -10875,12 +10845,6 @@ parameters:
1087510845
count: 5
1087610846
path: src/Navigation/NavigationTree.php
1087710847

10878-
-
10879-
message: '#^Parameter \#1 \$string of function strlen expects string, mixed given\.$#'
10880-
identifier: argument.type
10881-
count: 1
10882-
path: src/Navigation/NavigationTree.php
10883-
1088410848
-
1088510849
message: '#^Parameter \#1 \$string of method PhpMyAdmin\\Navigation\\NavigationTree\:\:parsePath\(\) expects string, mixed given\.$#'
1088610850
identifier: argument.type
@@ -10923,12 +10887,6 @@ parameters:
1092310887
count: 1
1092410888
path: src/Navigation/NavigationTree.php
1092510889

10926-
-
10927-
message: '#^Parameter \#2 \$needle of function mb_strpos expects string, mixed given\.$#'
10928-
identifier: argument.type
10929-
count: 1
10930-
path: src/Navigation/NavigationTree.php
10931-
1093210890
-
1093310891
message: '#^Parameter \#2 \$path of method PhpMyAdmin\\Navigation\\NavigationTree\:\:buildPathPart\(\) expects array\<mixed\>, mixed given\.$#'
1093410892
identifier: argument.type

psalm-baseline.xml

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6661,38 +6661,23 @@
66616661
<code><![CDATA[$path[0]]]></code>
66626662
<code><![CDATA[$path[0]]]></code>
66636663
<code><![CDATA[$path[0]]]></code>
6664-
<code><![CDATA[$separator]]></code>
6665-
<code><![CDATA[$separator]]></code>
66666664
<code><![CDATA[$value]]></code>
66676665
<code><![CDATA[Current::$database]]></code>
66686666
<code><![CDATA[Current::$database]]></code>
66696667
<code><![CDATA[Current::$database]]></code>
66706668
</MixedArgument>
6671-
<MixedArrayAccess>
6672-
<code><![CDATA[$node->secondIcon['image']]]></code>
6673-
<code><![CDATA[$node->secondIcon['title']]]></code>
6674-
</MixedArrayAccess>
66756669
<MixedAssignment>
66766670
<code><![CDATA[$db]]></code>
66776671
<code><![CDATA[$item]]></code>
66786672
<code><![CDATA[$part]]></code>
66796673
<code><![CDATA[$path]]></code>
66806674
<code><![CDATA[$path]]></code>
66816675
<code><![CDATA[$path]]></code>
6682-
<code><![CDATA[$separator]]></code>
6683-
<code><![CDATA[$separator]]></code>
66846676
<code><![CDATA[$value]]></code>
66856677
</MixedAssignment>
6686-
<MixedOperand>
6687-
<code><![CDATA[$separators[0]]]></code>
6688-
<code><![CDATA[$separators[0]]]></code>
6689-
</MixedOperand>
66906678
<PossiblyFalseOperand>
66916679
<code><![CDATA[strrpos($class, '\\')]]></code>
66926680
</PossiblyFalseOperand>
6693-
<PossiblyFalseReference>
6694-
<code><![CDATA[getPresence]]></code>
6695-
</PossiblyFalseReference>
66966681
<PossiblyInvalidArgument>
66976682
<code><![CDATA[$_POST['aPath']]]></code>
66986683
<code><![CDATA[$_POST['n' . $count . '_aPath']]]></code>
@@ -6736,9 +6721,6 @@
67366721
<TypeDoesNotContainType>
67376722
<code><![CDATA[$value > 500]]></code>
67386723
</TypeDoesNotContainType>
6739-
<UndefinedMethod>
6740-
<code><![CDATA[setHiddenCount]]></code>
6741-
</UndefinedMethod>
67426724
</file>
67436725
<file src="src/Navigation/Nodes/Node.php">
67446726
<DeprecatedMethod>
@@ -6820,7 +6802,7 @@
68206802
</file>
68216803
<file src="src/Navigation/Nodes/NodeDatabaseChild.php">
68226804
<PossiblyInvalidPropertyFetch>
6823-
<code><![CDATA[$this->realParent()->realName]]></code>
6805+
<code><![CDATA[$this->getRealParent()->realName]]></code>
68246806
</PossiblyInvalidPropertyFetch>
68256807
</file>
68266808
<file src="src/Navigation/Nodes/NodeDatabaseContainer.php">
@@ -6834,15 +6816,12 @@
68346816
<code><![CDATA[DatabaseInterface::getInstance()]]></code>
68356817
</DeprecatedMethod>
68366818
<PossiblyInvalidPropertyFetch>
6837-
<code><![CDATA[$this->realParent()->realName]]></code>
6838-
<code><![CDATA[$this->realParent()->realName]]></code>
6819+
<code><![CDATA[$this->getRealParent()->realName]]></code>
6820+
<code><![CDATA[$this->getRealParent()->realName]]></code>
68396821
</PossiblyInvalidPropertyFetch>
68406822
<PossiblyNullArgument>
68416823
<code><![CDATA[$arr['Type']]]></code>
68426824
</PossiblyNullArgument>
6843-
<PossiblyUnusedProperty>
6844-
<code><![CDATA[$secondIcon]]></code>
6845-
</PossiblyUnusedProperty>
68466825
</file>
68476826
<file src="src/Navigation/Nodes/ObjectFetcher.php">
68486827
<MixedReturnTypeCoercion>

resources/templates/navigation/tree/node.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@
3737
&nbsp;<a class="hover_show_full disableAjax" href="{{ url(text_link.route, text_link.params) }}">{{ node.name }}</a>
3838
{% elseif 'index' in node.classes %}
3939
<a class="hover_show_full disableAjax" href="{{ url(text_link.route) }}" data-post="{{ get_common(text_link.params|merge({'is_from_nav': true})) }}" title="{{ text_link.title }}">
40-
{{- node.displayName ?? node.realName -}}
40+
{{- displayName -}}
4141
</a>
4242
{% else %}
4343
<a class="hover_show_full disableAjax" href="{{ url(text_link.route, text_link.params) }}" title="{{ text_link.title }}">
44-
{{- node.displayName ?? node.realName -}}
44+
{{- displayName -}}
4545
</a>
4646
{% endif %}
4747
{% endif %}

src/Navigation/NavigationTree.php

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,7 @@
5656
use function floor;
5757
use function htmlspecialchars;
5858
use function in_array;
59-
use function is_array;
6059
use function is_bool;
61-
use function is_object;
62-
use function is_string;
6360
use function mb_strlen;
6461
use function mb_strpos;
6562
use function mb_substr;
@@ -84,8 +81,8 @@ class NavigationTree
8481
{
8582
private const SPECIAL_NODE_NAMES = ['tables', 'views', 'functions', 'procedures', 'events'];
8683

87-
/** @var Node Reference to the root node of the tree */
88-
private Node $tree;
84+
/** @var NodeDatabaseContainer Reference to the root node of the tree */
85+
private NodeDatabaseContainer $tree;
8986
/**
9087
* @var mixed[] The actual paths to all expanded nodes in the tree
9188
* This does not include nodes created after the grouping
@@ -211,15 +208,6 @@ public function __construct(
211208

212209
// Initialize the tree by creating a root node
213210
$this->tree = new NodeDatabaseContainer($this->config, 'root');
214-
if (
215-
! $this->config->settings['NavigationTreeEnableGrouping']
216-
|| ! $this->config->settings['ShowDatabasesNavigationAsTree']
217-
) {
218-
return;
219-
}
220-
221-
$this->tree->separator = $this->config->settings['NavigationTreeDbSeparator'];
222-
$this->tree->separatorDepth = 10000;
223211
}
224212

225213
/**
@@ -676,18 +664,11 @@ public function groupNode(Node $node): void
676664
return;
677665
}
678666

679-
$separators = [];
680-
if (is_array($node->separator)) {
681-
$separators = $node->separator;
682-
} elseif (is_string($node->separator) && $node->separator !== '') {
683-
$separators[] = $node->separator;
684-
}
685-
686667
$prefixes = [];
687668
if ($node->separatorDepth > 0) {
688669
foreach ($node->children as $child) {
689670
$prefixPos = false;
690-
foreach ($separators as $separator) {
671+
foreach ($node->separators as $separator) {
691672
$sepPos = mb_strpos($child->name, $separator);
692673
if (
693674
$sepPos === false
@@ -771,7 +752,7 @@ public function groupNode(Node $node): void
771752
}
772753

773754
$newChildren = [];
774-
foreach ($separators as $separator) {
755+
foreach ($node->separators as $separator) {
775756
$separatorLength = strlen($separator);
776757
// FIXME: this could be more efficient
777758
foreach ($node->children as $child) {
@@ -785,7 +766,11 @@ public function groupNode(Node $node): void
785766
}
786767

787768
$newChild = new $child($this->config, mb_substr($child->name, $keySeparatorLength));
788-
if ($child instanceof NodeDatabase && $child->getHiddenCount() > 0) {
769+
if (
770+
$child instanceof NodeDatabase
771+
&& $newChild instanceof NodeDatabase
772+
&& $child->getHiddenCount() > 0
773+
) {
789774
$newChild->setHiddenCount($child->getHiddenCount());
790775
}
791776

@@ -810,16 +795,16 @@ public function groupNode(Node $node): void
810795
// and the new group contains all of the current node's children, combine them
811796
$class = $node::class;
812797
if (count($newChildren) === $numChildren && substr($class, strrpos($class, '\\') + 1) === 'Node') {
813-
$node->name .= $separators[0] . htmlspecialchars((string) $key);
814-
$node->realName .= $separators[0] . htmlspecialchars((string) $key);
798+
$node->name .= $node->separators[0] . htmlspecialchars((string) $key);
799+
$node->realName .= $node->separators[0] . htmlspecialchars((string) $key);
815800
$node->separatorDepth--;
816801
foreach ($newChildren as $newChild) {
817802
$node->removeChild($newChild['replaces_name']);
818803
$node->addChild($newChild['node']);
819804
}
820805
} else {
821806
$groups[$key] = new Node($this->config, (string) $key, NodeType::Container, true);
822-
$groups[$key]->separator = $node->separator;
807+
$groups[$key]->separators = $node->separators;
823808
$groups[$key]->separatorDepth = $node->separatorDepth - 1;
824809
$groups[$key]->icon = ['image' => 'b_group', 'title' => __('Groups')];
825810
$groups[$key]->pos2 = $node->pos2;
@@ -918,9 +903,9 @@ public function renderPath(UserPrivileges $userPrivileges): string|false
918903
if ($hasSearchClause && ! is_bool($node)) {
919904
$results = 0;
920905
if ($this->searchClause2 !== '') {
921-
if (is_object($node->realParent())) {
922-
$results = $node->realParent()
923-
->getPresence($userPrivileges, $node->realName, $this->searchClause2);
906+
$parent = $node->getRealParent();
907+
if ($parent instanceof Node) {
908+
$results = $parent->getPresence($userPrivileges, $node->realName, $this->searchClause2);
924909
}
925910
} else {
926911
$results = $this->tree->getPresence($userPrivileges, 'databases', $this->searchClause);
@@ -1095,7 +1080,7 @@ private function renderNode(UserPrivileges $userPrivileges, Node $node, string $
10951080
'title' => $node->icon['title'],
10961081
];
10971082

1098-
if (isset($node->links['second_icon'], $node->secondIcon)) {
1083+
if ($node instanceof NodeTable && isset($node->links['second_icon'], $node->secondIcon)) {
10991084
$iconLinks[] = [
11001085
'route' => $node->links['second_icon']['route'],
11011086
'params' => array_merge(
@@ -1146,6 +1131,7 @@ private function renderNode(UserPrivileges $userPrivileges, Node $node, string $
11461131

11471132
return $this->template->render('navigation/tree/node', [
11481133
'node' => $node,
1134+
'displayName' => $node instanceof NodeColumn ? $node->displayName : $node->realName,
11491135
'class' => $class,
11501136
'show_node' => $showNode,
11511137
'has_siblings' => $node->hasSiblings(),
@@ -1269,7 +1255,7 @@ private function fastFilterHtml(UserPrivileges $userPrivileges, Node $node): str
12691255

12701256
$nodeIsSpecial = in_array($node->realName, self::SPECIAL_NODE_NAMES, true);
12711257

1272-
$realParent = $node->realParent();
1258+
$realParent = $node->getRealParent();
12731259
if (
12741260
$nodeIsContainer && $nodeIsSpecial
12751261
&& $realParent instanceof Node
@@ -1360,7 +1346,7 @@ private function getPageSelector(UserPrivileges $userPrivileges, Node $node): st
13601346
}
13611347

13621348
/** @var Node $realParent */
1363-
$realParent = $node->realParent();
1349+
$realParent = $node->getRealParent();
13641350
$num = $realParent->getPresence($userPrivileges, $node->realName, $this->searchClause2);
13651351
$retval .= Generator::getListNavigator(
13661352
$num,

0 commit comments

Comments
 (0)