Skip to content

Commit acc86be

Browse files
committed
Fix null search query at the caller and add regression test
Coalesce a null `q` to an empty string in AbstractSearcher::search() rather than in GambitManager::explode(), where the `string` parameter type makes `?? ''` dead code that fails PHPStan. This keeps the gambit chain's type contract intact and also covers an empty/absent `q`. Adds a regression test asserting a null search query does not emit the str_getcsv() null deprecation (fatal on strict PHP 8.1+ setups).
1 parent b40bd04 commit acc86be

3 files changed

Lines changed: 31 additions & 2 deletions

File tree

framework/core/src/Search/AbstractSearcher.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function search(QueryCriteria $criteria, $limit = null, $offset = 0): Que
5252

5353
$search = new SearchState($query->getQuery(), $actor);
5454

55-
$this->gambits->apply($search, $criteria->query['q']);
55+
$this->gambits->apply($search, $criteria->query['q'] ?? '');
5656
$this->applySort($search, $criteria->sort, $criteria->sortIsDefault);
5757
$this->applyOffset($search, $offset);
5858
$this->applyLimit($search, $limit + 1);

framework/core/src/Search/GambitManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function apply(SearchState $search, $query)
6464
*/
6565
protected function explode($query)
6666
{
67-
return str_getcsv($query ?? '', ' ');
67+
return str_getcsv($query, ' ');
6868
}
6969

7070
/**

framework/core/tests/integration/api/users/ListTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,4 +313,33 @@ public function email_gambit_only_works_for_admin()
313313
$data = json_decode($response->getBody()->getContents(), true)['data'];
314314
$this->assertEquals([], Arr::pluck($data, 'id'));
315315
}
316+
317+
/**
318+
* A search with a null query (rather than a string) must not trip the
319+
* "passing null to a non-nullable parameter" deprecation on PHP 8.1+, which
320+
* escalates to a fatal on stricter setups. Regression test for the gambit
321+
* search choking on a null `q`.
322+
*
323+
* @test
324+
*/
325+
public function search_with_null_query_does_not_emit_deprecation()
326+
{
327+
set_error_handler(function ($errno, $errstr) {
328+
throw new \ErrorException($errstr, 0, $errno);
329+
}, E_DEPRECATED);
330+
331+
try {
332+
$response = $this->send(
333+
$this->request('GET', '/api/users', [
334+
'authenticatedAs' => 1,
335+
])->withQueryParams([
336+
'filter' => ['q' => null],
337+
])
338+
);
339+
} finally {
340+
restore_error_handler();
341+
}
342+
343+
$this->assertEquals(200, $response->getStatusCode());
344+
}
316345
}

0 commit comments

Comments
 (0)