Skip to content

Commit 0b3d3e8

Browse files
committed
chore: improve code clarity
1 parent 040fef6 commit 0b3d3e8

File tree

3 files changed

+12
-16
lines changed

3 files changed

+12
-16
lines changed

src/parsers/array-slice-selector.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
function apply_slice_selector($selector, $json)
1111
{
1212
if (!is_json_array($json)) {
13-
// throw new Error(`JSON node ${JSON.stringify(json)} is not an array`);
1413
return [];
1514
}
1615

src/parsers/root.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ function apply_segment($segment, $root_node, $json)
4242
return apply_selector($selector, $root_node, $json);
4343
}, $segment);
4444
$segment_result = array_flatten($selector_results);
45+
4546
return $segment_result;
4647
}
4748

@@ -104,23 +105,19 @@ function apply_member_name_selector($selector, $json)
104105
if (json_object_has_key($json, $selector->member)) {
105106
return [json_get($json, $selector->member)];
106107
}
108+
107109
return [];
108110
}
109111

110112
// 2.3.3. Index Selector
111113
// An index selector <index> matches at most one array element value.
112114
function apply_index_selector($node, $json)
113115
{
114-
if (is_json_array($json)) {
115-
if ($node->index < sizeof($json)) {
116-
$index = normalize_index($node->index, sizeof($json));
117-
if ($index === null) {
118-
return [];
119-
} else {
120-
return [$json[$index]];
121-
}
122-
}
116+
if (!is_json_array($json)) {
123117
return [];
124118
}
125-
return [];
119+
120+
$index = normalize_index($node->index, sizeof($json));
121+
122+
return $index === null ? [] : [$json[$index]];
126123
}

src/utils/traverse-descendant.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44

55
function traverse_descendant($json)
66
{
7-
$nodelist = [];
8-
array_push($nodelist, $json);
7+
$node_list = [];
8+
array_push($node_list, $json);
99

1010
if (is_json_array($json)) {
1111
foreach ($json as $node) {
12-
array_push($nodelist, ...traverse_descendant($node));
12+
array_push($node_list, ...traverse_descendant($node));
1313
}
1414
} elseif (is_json_object($json)) {
1515
foreach ((array) $json as $value) {
16-
array_push($nodelist, ...traverse_descendant($value));
16+
array_push($node_list, ...traverse_descendant($value));
1717
}
1818
}
1919

20-
return $nodelist;
20+
return $node_list;
2121
}

0 commit comments

Comments
 (0)