Skip to content

Commit b27dddd

Browse files
authored
Code simplify.
1 parent 962239c commit b27dddd

File tree

1 file changed

+3
-12
lines changed

1 file changed

+3
-12
lines changed

src/SelectboxTree.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,6 @@ public function process(array $data, ?callable $nameFormatter = null): array
5050

5151

5252
/**
53-
* @param string $table
54-
* @param string $primaryCol
55-
* @param string $parentCol
5653
* @param string[] $wheres
5754
* @return string
5855
*/
@@ -65,13 +62,12 @@ public function sqlBuilder(string $table, string $primaryCol = 'name', string $p
6562
}
6663

6764

68-
/**
69-
* @param int $maxDepth
70-
*/
7165
public function setMaxDepth(int $maxDepth): void
7266
{
7367
if ($maxDepth < 1) {
7468
$maxDepth = 1;
69+
} elseif ($maxDepth > 1000) {
70+
throw new \InvalidArgumentException('Max depth "' . $maxDepth . '" is too big. Maximum value is 1000.');
7571
}
7672
$this->maxDepth = $maxDepth;
7773
}
@@ -81,8 +77,6 @@ public function setMaxDepth(int $maxDepth): void
8177
* Build category tree to simple selectbox array.
8278
*
8379
* @param string[][]|null[][]|null $categories
84-
* @param int $level
85-
* @param string|null $parent
8680
* @return mixed[][]
8781
*/
8882
private function serializeCategoriesToSelectbox(?array $categories, int $level = 0, ?string $parent = null): array
@@ -92,8 +86,7 @@ private function serializeCategoriesToSelectbox(?array $categories, int $level =
9286
if ($level === 0) {
9387
$usedIds = [];
9488
}
95-
96-
if ($categories === null || $categories === [] || $level > $this->maxDepth) { // empty or recursion
89+
if ($categories === null || $categories === [] || $level > $this->maxDepth) { // empty or infinity recursion
9790
return [];
9891
}
9992

@@ -102,7 +95,6 @@ private function serializeCategoriesToSelectbox(?array $categories, int $level =
10295
if (array_key_exists('id', $category) === false || array_key_exists('parent', $category) === false || array_key_exists('name', $category) === false) {
10396
throw new \InvalidArgumentException('Category "' . $catKey . '" must contain keys "id", "parent" and "name".');
10497
}
105-
10698
if ($category['parent'] === $parent) {
10799
if (isset($usedIds[$category['id']]) === false) {
108100
$return[$category['id']] = [
@@ -112,7 +104,6 @@ private function serializeCategoriesToSelectbox(?array $categories, int $level =
112104
unset($categories[$catKey]);
113105
$usedIds[$category['id']] = true;
114106
}
115-
116107
if (($sub = $this->serializeCategoriesToSelectbox($categories, $level + 1, $category['id'])) !== []) {
117108
foreach ($sub as $key => $value) {
118109
$return[$key] = $value;

0 commit comments

Comments
 (0)