Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API Add strong-typing to SearchContext::getQuery() $limit param #11534

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 2 additions & 14 deletions src/ORM/Search/BasicSearchContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,16 @@ class BasicSearchContext extends SearchContext
* If a filter is applied to a relationship in dot notation,
* the parameter name should have the dots replaced with double underscores,
* for example "Comments__Name" instead of the filter name "Comments.Name".
* @param array|bool|string $sort Field to sort on.
* @param array|false|string $sort Field to sort on.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true isn't a valid value for $sort so I changed this to explicitly false. Not strong-typing here because the main focus of this PR is the $limit param.

* @param int|array|null $limit
* @param SS_List $existingQuery
*/
public function getQuery($searchParams, $sort = false, $limit = false, $existingQuery = null): SS_List
public function getQuery($searchParams, $sort = false, int|array|null $limit = null, $existingQuery = null): SS_List
{
if (!$existingQuery || !is_a($existingQuery, SS_List::class)) {
throw new InvalidArgumentException('getQuery requires a pre-existing SS_List list to be passed as $existingQuery.');
}

if ((count(func_get_args()) >= 3) && (!in_array(gettype($limit), ['array', 'NULL', 'integer']))) {
Deprecation::notice(
'5.1.0',
'$limit should be type of int|array|null'
);
if (is_string($limit) && is_numeric($limit)) {
$limit = (int) $limit;
} else {
$limit = null;
}
}

$searchParams = $this->applySearchFilters($this->normaliseSearchParams($searchParams));
$result = $this->applyGeneralSearchField($searchParams, $existingQuery);

Expand Down
14 changes: 2 additions & 12 deletions src/ORM/Search/SearchContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,22 +142,15 @@ protected function applyBaseTableFields()
* If a filter is applied to a relationship in dot notation,
* the parameter name should have the dots replaced with double underscores,
* for example "Comments__Name" instead of the filter name "Comments.Name".
* @param array|bool|string $sort Database column to sort on.
* @param array|false|string $sort Database column to sort on.
* Falls back to {@link DataObject::$default_sort} if not provided.
* @param int|array|null $limit
* @param DataList $existingQuery
* @return DataList<T>
* @throws Exception
*/
public function getQuery($searchParams, $sort = false, $limit = false, $existingQuery = null)
public function getQuery($searchParams, $sort = false, int|array|null $limit = null, $existingQuery = null)
{
if ((count(func_get_args()) >= 3) && (!in_array(gettype($limit), ['integer', 'array', 'NULL']))) {
Deprecation::notice(
'5.1.0',
'$limit should be type of int|array|null'
);
$limit = null;
}
$this->setSearchParams($searchParams);
$query = $this->prepareQuery($sort, $limit, $existingQuery);
return $this->search($query);
Expand Down Expand Up @@ -197,9 +190,6 @@ private function prepareQuery($sort, $limit, ?DataList $existingQuery): DataList
}
$query = null;
if ($existingQuery) {
if (!($existingQuery instanceof DataList)) {
throw new InvalidArgumentException("existingQuery must be DataList");
}
Comment on lines -200 to -202
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's strongly typed so this is dead code.

if ($existingQuery->dataClass() != $this->modelClass) {
throw new InvalidArgumentException("existingQuery's dataClass is " . $existingQuery->dataClass()
. ", $this->modelClass expected.");
Expand Down
20 changes: 0 additions & 20 deletions tests/php/ORM/Search/BasicSearchContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,26 +266,6 @@ public static function provideGetQueryLimit(): array
'Sara',
],
],
'limit numeric string' => [
'limit' => '4',
'expected' => [
'James',
'John',
'Jane',
'Hemi',
],
],
'limit invalid string' => [
'limit' => 'blah',
'expected' => [
'James',
'John',
'Jane',
'Hemi',
'Sara',
'MatchNothing',
],
],
];
}

Expand Down
Loading