Skip to content

Commit dbd6db2

Browse files
committed
refactor: convert 'switch' statements to equivalent 'match' expressions
1 parent b18f16f commit dbd6db2

10 files changed

Lines changed: 108 additions & 209 deletions

src/comparator/compare-arrays.php

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,13 @@
66
// the first array is equal to the corresponding element of the second array, or
77
function compare_arrays($operator, $a, $b)
88
{
9-
switch ($operator) {
10-
case '==':
11-
return is_equal($a, $b);
12-
case '!=':
13-
return !is_equal($a, $b);
14-
case '<':
15-
// Not defined
16-
return false;
17-
case '<=':
18-
return is_equal($a, $b);
19-
case '>':
20-
// Not defined
21-
return false;
22-
case '>=':
23-
return is_equal($a, $b);
24-
default:
25-
throw new \InvalidArgumentException("Unsupported operator: $operator");
26-
}
9+
return match ($operator) {
10+
'==' => is_equal($a, $b),
11+
'!=' => !is_equal($a, $b),
12+
'<' => false, // Not defined
13+
'<=' => is_equal($a, $b),
14+
'>' => false, // Not defined
15+
'>=' => is_equal($a, $b),
16+
default => throw new \InvalidArgumentException("Unsupported operator: $operator"),
17+
};
2718
}

src/comparator/compare-booleans.php

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,13 @@
44

55
function compare_booleans($operator, $a, $b)
66
{
7-
switch ($operator) {
8-
case '==':
9-
return $a === $b;
10-
case '!=':
11-
return $a !== $b;
12-
case '<':
13-
return false;
14-
case '<=':
15-
return $a === $b;
16-
case '>':
17-
return false;
18-
case '>=':
19-
return $a === $b;
20-
default:
21-
throw new \InvalidArgumentException("Invalid operator: $operator");
22-
}
7+
return match ($operator) {
8+
'==' => $a === $b,
9+
'!=' => $a !== $b,
10+
'<' => false,
11+
'<=' => $a === $b,
12+
'>' => false,
13+
'>=' => $a === $b,
14+
default => throw new \InvalidArgumentException("Invalid operator: $operator"),
15+
};
2316
}

src/comparator/compare-nodes.php

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,13 @@ function compare_nodes($operator, $a, $b)
1010
{
1111
$nothing = nothing();
1212

13-
switch ($operator) {
14-
case '==':
15-
return ($a === $nothing || $b === $nothing) && $a === $b;
16-
case '!=':
17-
return ($a === $nothing || $b === $nothing) && $a !== $b;
18-
case '<':
19-
// Not defined
20-
return false;
21-
case '<=':
22-
return ($a === $nothing || $b === $nothing) && $a === $b;
23-
case '>':
24-
// Not defined
25-
return false;
26-
case '>=':
27-
return ($a === $nothing || $b === $nothing) && $a === $b;
28-
default:
29-
throw new \InvalidArgumentException("Invalid comparison operator: $operator");
30-
}
13+
return match ($operator) {
14+
'==' => ($a === $nothing || $b === $nothing) && $a === $b,
15+
'!=' => ($a === $nothing || $b === $nothing) && $a !== $b,
16+
'<' => false, // Not defined
17+
'<=' => ($a === $nothing || $b === $nothing) && $a === $b,
18+
'>' => false, // Not defined
19+
'>=' => ($a === $nothing || $b === $nothing) && $a === $b,
20+
default => throw new \InvalidArgumentException("Invalid comparison operator: $operator"),
21+
};
3122
}

src/comparator/compare-nulls.php

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,13 @@
44

55
function compare_nulls($operator, $a, $b)
66
{
7-
switch ($operator) {
8-
case '==':
9-
return $a === $b;
10-
case '!=':
11-
return $a !== $b;
12-
case '<':
13-
return false;
14-
case '<=':
15-
return $a === $b;
16-
case '>':
17-
return false;
18-
case '>=':
19-
return $a === $b;
20-
default:
21-
throw new \InvalidArgumentException("Invalid comparison operator: $operator");
22-
}
7+
return match ($operator) {
8+
'==' => $a === $b,
9+
'!=' => $a !== $b,
10+
'<' => false,
11+
'<=' => $a === $b,
12+
'>' => false,
13+
'>=' => $a === $b,
14+
default => throw new \InvalidArgumentException("Invalid comparison operator: $operator"),
15+
};
2316
}

src/comparator/compare-numbers.php

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,13 @@ function compare_numbers($operator, $a, $b)
1313
$b = floatval($b);
1414
}
1515

16-
switch ($operator) {
17-
case '==':
18-
return $a === $b;
19-
case '!=':
20-
return $a !== $b;
21-
case '<':
22-
if (!is_number($b)) {
23-
return false;
24-
}
25-
return $a < $b;
26-
case '<=':
27-
if (!is_number($b)) {
28-
return false;
29-
}
30-
return $a <= $b;
31-
case '>':
32-
if (!is_number($b)) {
33-
return false;
34-
}
35-
return $a > $b;
36-
case '>=':
37-
if (!is_number($b)) {
38-
return false;
39-
}
40-
return $a >= $b;
41-
default:
42-
throw new \InvalidArgumentException("Invalid comparison operator: $operator");
43-
}
16+
return match ($operator) {
17+
'==' => $a === $b,
18+
'!=' => $a !== $b,
19+
'<' => is_number($b) && $a < $b,
20+
'<=' => is_number($b) && $a <= $b,
21+
'>' => is_number($b) && $a > $b,
22+
'>=' => is_number($b) && $a >= $b,
23+
default => throw new \InvalidArgumentException("Invalid comparison operator: $operator"),
24+
};
4425
}

src/comparator/compare-objects.php

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,13 @@
77
// for each of those names, the values associated with the name by the objects are equal.
88
function compare_objects($operator, $a, $b)
99
{
10-
switch ($operator) {
11-
case '==':
12-
return is_equal($a, $b);
13-
case '!=':
14-
return !is_equal($a, $b);
15-
case '<':
16-
// Not defined
17-
return false;
18-
case '<=':
19-
return is_equal($a, $b);
20-
case '>':
21-
// Not defined
22-
return false;
23-
case '>=':
24-
return is_equal($a, $b);
25-
default:
26-
throw new \InvalidArgumentException("Invalid comparison operator: $operator");
27-
}
10+
return match ($operator) {
11+
'==' => is_equal($a, $b),
12+
'!=' => !is_equal($a, $b),
13+
'<' => false, // Not defined
14+
'<=' => is_equal($a, $b),
15+
'>' => false, // Not defined
16+
'>=' => is_equal($a, $b),
17+
default => throw new \InvalidArgumentException("Invalid comparison operator: $operator"),
18+
};
2819
}

src/comparator/compare-strings.php

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,13 @@
99
// less than the remainder of the second string.
1010
function compare_strings($operator, $a, $b)
1111
{
12-
switch ($operator) {
13-
case '==':
14-
return $a === $b;
15-
case '!=':
16-
return $a !== $b;
17-
case '<':
18-
return $a < $b;
19-
case '<=':
20-
return $a <= $b;
21-
case '>':
22-
return $a > $b;
23-
case '>=':
24-
return $a >= $b;
25-
default:
26-
throw new \InvalidArgumentException("Invalid comparison operator: $operator");
27-
}
12+
return match ($operator) {
13+
'==' => $a === $b,
14+
'!=' => $a !== $b,
15+
'<' => $a < $b,
16+
'<=' => $a <= $b,
17+
'>' => $a > $b,
18+
'>=' => $a >= $b,
19+
default => throw new \InvalidArgumentException("Invalid comparison operator: $operator"),
20+
};
2821
}

src/parsers/filter-selector.php

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,12 @@ function apply_filter_selector($selector, $root_node, $json)
2121
function apply_filter_expression($expr, $root_node, $json)
2222
{
2323
$exp_type = $expr->type;
24-
switch ($exp_type) {
25-
case 'ComparisonExpr':
26-
return apply_compare($expr, $root_node, $json);
27-
case 'TestExpr':
28-
return apply_test($expr, $root_node, $json);
29-
case 'LogicalBinary':
30-
case 'LogicalUnary':
31-
return apply_logical($expr, $root_node, $json);
32-
default:
33-
throw new \Exception("Unexpected expression type: $exp_type");
34-
}
35-
36-
return false;
24+
return match ($exp_type) {
25+
'ComparisonExpr' => apply_compare($expr, $root_node, $json),
26+
'TestExpr' => apply_test($expr, $root_node, $json),
27+
'LogicalBinary', 'LogicalUnary' => apply_logical($expr, $root_node, $json),
28+
default => throw new \Exception("Unexpected expression type: $exp_type"),
29+
};
3730
}
3831

3932
function apply_compare($compare, $root_node, $json)
@@ -97,17 +90,17 @@ function apply_comparable($comparable, $root_node, $json)
9790
{
9891
// These can be obtained via literal values; singular queries,
9992
// each of which selects at most one node
100-
switch ($comparable->type) {
101-
case 'Literal':
102-
return $comparable->member;
103-
case 'CurrentNode':
104-
$result = apply_current_node($comparable, $root_node, [$json]);
105-
return array_key_exists(0, $result) ? $result[0] : nothing();
106-
case 'Root':
107-
return apply_root($comparable, $root_node)[0] ?? nothing();
108-
case 'FunctionExpr':
109-
return apply_function($comparable, $root_node, $json);
110-
}
93+
return match ($comparable->type) {
94+
'Literal' => $comparable->member,
95+
'CurrentNode' => array_key_exists(
96+
0,
97+
$result = apply_current_node($comparable, $root_node, [$json]),
98+
)
99+
? $result[0]
100+
: nothing(),
101+
'Root' => apply_root($comparable, $root_node)[0] ?? nothing(),
102+
'FunctionExpr' => apply_function($comparable, $root_node, $json),
103+
};
111104
}
112105

113106
function apply_test($expr, $root_node, $json)
@@ -146,14 +139,11 @@ function apply_query($query, $root_node, $json)
146139

147140
function apply_logical($expr, $root_node, $json)
148141
{
149-
switch ($expr->operator) {
150-
case '||':
151-
return apply_or($expr, $root_node, $json);
152-
case '&&':
153-
return apply_and($expr, $root_node, $json);
154-
case '!':
155-
return apply_not($expr, $root_node, $json);
156-
}
142+
return match ($expr->operator) {
143+
'||' => apply_or($expr, $root_node, $json),
144+
'&&' => apply_and($expr, $root_node, $json),
145+
'!' => apply_not($expr, $root_node, $json),
146+
};
157147
}
158148

159149
function apply_or($or, $root_node, $json)

src/parsers/function-extensions.php

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,39 +14,23 @@ function apply_function($func, $root_node, $node)
1414
return apply_function_argument($arg, $root_node, $node);
1515
}, $func->args);
1616

17-
switch ($func->name) {
18-
case 'length':
19-
$args = extract_args($length_function, $evaluated_args);
20-
return $length_function->function(...$args);
21-
case 'count':
22-
$args = extract_args($count_function, $evaluated_args);
23-
return $count_function->function(...$args);
24-
case 'match':
25-
$args = extract_args($match_function, $evaluated_args);
26-
return $match_function->function(...$args);
27-
case 'search':
28-
$args = extract_args($search_function, $evaluated_args);
29-
return $search_function->function(...$args);
30-
case 'value':
31-
$args = extract_args($value_function, $evaluated_args);
32-
return $value_function->function(...$args);
33-
}
34-
35-
return nothing();
17+
return match ($func->name) {
18+
'length' => $length_function->function(...extract_args($length_function, $evaluated_args)),
19+
'count' => $count_function->function(...extract_args($count_function, $evaluated_args)),
20+
'match' => $match_function->function(...extract_args($match_function, $evaluated_args)),
21+
'search' => $search_function->function(...extract_args($search_function, $evaluated_args)),
22+
'value' => $value_function->function(...extract_args($value_function, $evaluated_args)),
23+
default => nothing(),
24+
};
3625
}
3726

3827
function apply_function_argument($argument, $root_node, $json)
3928
{
40-
switch ($argument->type) {
41-
case 'Literal':
42-
return $argument->member;
43-
case 'CurrentNode':
44-
return apply_current_node($argument, $root_node, [$json]);
45-
case 'Root':
46-
return apply_root($argument, $root_node);
47-
case 'FunctionExpr':
48-
return apply_function($argument, $root_node, $json);
49-
default:
50-
throw new \Exception('Unknown argument type "' . $argument->type . '"');
51-
}
29+
return match ($argument->type) {
30+
'Literal' => $argument->member,
31+
'CurrentNode' => apply_current_node($argument, $root_node, [$json]),
32+
'Root' => apply_root($argument, $root_node),
33+
'FunctionExpr' => apply_function($argument, $root_node, $json),
34+
default => throw new \Exception('Unknown argument type "' . $argument->type . '"'),
35+
};
5236
}

src/parsers/root.php

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -61,22 +61,14 @@ function apply_segment($segment, $root_node, $json)
6161
// A selector produces a nodelist consisting of zero or more children of the input value.
6262
function apply_selector($selector, $root_node, $json)
6363
{
64-
$type = $selector->type;
65-
switch ($type) {
66-
case 'WildcardSelector':
67-
return apply_wildcard_selector($selector, $json);
68-
case 'IndexSelector':
69-
return apply_index_selector($selector, $json);
70-
case 'SliceSelector':
71-
return apply_slice_selector($selector, $json);
72-
case 'MemberNameShorthand':
73-
case 'NameSelector':
74-
return apply_member_name_selector($selector, $json);
75-
case 'FilterSelector':
76-
return apply_filter_selector($selector, $root_node, $json);
77-
default:
78-
throw new \Exception('Unknown selector type: ' . $type);
79-
}
64+
return match ($selector->type) {
65+
'WildcardSelector' => apply_wildcard_selector($selector, $json),
66+
'IndexSelector' => apply_index_selector($selector, $json),
67+
'SliceSelector' => apply_slice_selector($selector, $json),
68+
'MemberNameShorthand', 'NameSelector' => apply_member_name_selector($selector, $json),
69+
'FilterSelector' => apply_filter_selector($selector, $root_node, $json),
70+
default => throw new \Exception('Unknown selector type: ' . $selector->type),
71+
};
8072
}
8173

8274
// 2.3.2. Wildcard Selector

0 commit comments

Comments
 (0)