meilisearch-php icon indicating copy to clipboard operation
meilisearch-php copied to clipboard

Added ability to init SearchQuery props on construction

Open Enchiridion opened this issue 2 years ago • 5 comments

Pull Request

What does this PR do?

This lets you init the SearchQuery props when the object is constructed. I think the PHP library is the only one that doesn't yet let you do this or use a simple array instead. I created this because I was using the search() method but needed to switch to multiSearch(), but found it works completely differently, this speeds up using it.

Previous way:

$data = (new SearchQuery())
   ->setIndexUid('books')
   ->setQuery('butler')
   ->setSort(['author:desc']);

Additional way:

$data = new SearchQuery([
    'indexUid' => 'books',
    'q'        => 'butler',
    'sort'     => ['author:desc'],
]);

Note 1: My change currently does not pass the test due to the use of variable property access. However I think it's safe because all the props are simple and typed. If you think that's ok, I can have phpstan ignore that line.

 ------ -----------------------------------------------------------------------
  Line   src/Contracts/SearchQuery.php
 ------ -----------------------------------------------------------------------
  33     Variable property access on $this(Meilisearch\Contracts\SearchQuery).
 ------ -----------------------------------------------------------------------

Note 2: The initial code I based this on from main currently does not pass all the phpstan tests:

 ------ ------------------------------------------------------------------------------
  Line   tests/Endpoints/DocumentsTest.php
 ------ ------------------------------------------------------------------------------
  324    Call to an undefined method
         PHPUnit\Framework\MockObject\Rule\InvokedCount::numberOfInvocations().
  805    Call to an undefined method
         PHPUnit\Framework\MockObject\Rule\InvokedAtLeastOnce::numberOfInvocations().
 ------ ------------------------------------------------------------------------------

PR checklist

Please check if your PR fulfills the following requirements:

  • [x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)?
  • [x] Have you read the contributing guidelines?
  • [x] Have you made sure that the title is accurate and descriptive of the changes?

Enchiridion avatar Jul 11 '23 02:07 Enchiridion

I added in the linter ignore for the Variable property access line.

Enchiridion avatar Jul 11 '23 02:07 Enchiridion

Is it really worth? With array you don't have autocompletion, because the structure is undefined

norkunas avatar Jul 11 '23 04:07 norkunas

I added in the linter ignore for the Variable property access line.

I'd document the structure instead of ignoring

norkunas avatar Jul 11 '23 04:07 norkunas

I've updated the PR so all the properties are in the constructor. With PHP8+ you can specify the args using named arguments along with array spreading to get the same effect of setting all those properties quickly and easily using an array.

Enchiridion avatar Jul 13 '23 18:07 Enchiridion

@Enchiridion do you still plan to work on this PR? 😊

curquiza avatar Jun 11 '24 12:06 curquiza

Closing this for lack of activity. Feel free to re-open of course

curquiza avatar Oct 03 '24 13:10 curquiza