From e486734c2426dac8b05337e4e7dfc83a91680df4 Mon Sep 17 00:00:00 2001 From: Brian Foley Date: Tue, 15 Sep 2026 12:42:32 +0100 Subject: [PATCH] page_table.inc: Add array_search wrapper that always returns int|false --- pinc/misc.inc | 16 ++++++++++++++++ pinc/page_table.inc | 3 +-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/pinc/misc.inc b/pinc/misc.inc index a6913d964..c0bd029b1 100644 --- a/pinc/misc.inc +++ b/pinc/misc.inc @@ -1665,6 +1665,22 @@ function validate_csrf_token(): void } +/** + * When calling `array_search` with a list, the key for a matching value is + * always an integer -- it's the index of the value. + * PHPStan dosen't know this because of `array_search`'s type signature, so + * this wrapper function lets us tell PHPStan that list keys are integers + * by squashing the impossible non-integral `$key` result to `false`. + * @template T + * @param T $needle + * @param T[] $haystack + */ +function list_search(mixed $needle, array $haystack): int|false +{ + $key = array_search($needle, $haystack, strict: true); + return is_int($key) ? $key : false; +} + // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX // Useful exceptions diff --git a/pinc/page_table.inc b/pinc/page_table.inc index c21167efa..1ab266cc6 100644 --- a/pinc/page_table.inc +++ b/pinc/page_table.inc @@ -166,9 +166,8 @@ function fetch_page_table_data(Project $project, $page_selector = null, ?Round $ $tables = $project->projectid; foreach ($rounds_to_display as $round) { $rn = $round->round_number; - // find the previous round with data to use as the diff column - $rounds_with_data_index = array_search($round, $rounds_with_data); + $rounds_with_data_index = list_search($round, $rounds_with_data); if ($rounds_with_data_index === 0 || $rn == 1) { $prev_text_column_name = 'master_text'; } elseif ($rounds_with_data_index === false) {