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

Fix for PHPStan issues #1439

Closed
wants to merge 1 commit into from
Closed

Fix for PHPStan issues #1439

wants to merge 1 commit into from

Conversation

ezimuel
Copy link
Contributor

@ezimuel ezimuel commented Mar 22, 2025

This PR fixes the PHPStan issues reported in #1425, #1424, #1423 and #1346. It also improved the PHPStan rule level to 5.

* human?: bool, // Return human readable values for statistics. (DEFAULT: true)
* error_trace?: bool, // Include the stack trace of returned errors. (DEFAULT: false)
* source?: string, // The URL-encoded request definition. Useful for libraries that do not accept a request body for non-POST requests.
* filter_path?: string, // A comma-separated list of filters used to reduce the response.
Copy link
Contributor

Choose a reason for hiding this comment

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

Note the code will actually also accept an array of sting elements: string|array<string>

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@AJenbo these array fields are specificed by the Elasticsearch rest-api-specification, reported here. A list for this specification is represented as a comma separated list in the querystring, that is equivalent as a PHP string. That's why I used only string here.

Copy link
Contributor

Choose a reason for hiding this comment

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

I understand that, but the implementation in this repo will take a list of strings and turn it in to a comma separated string:

// Convert to comma-separated list if array

Not documenting this effectively turns this functionality in to dead code and prevents us from taking advantage of the convenience.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@AJenbo unfortunately the convertValue() function that is able to translate an array into a comma separated value is used only for the query string parameter (e.g. filter_path). I need to adjust the usage of convertValue also for other parameters in the URL, e.g. here. Anyway, you right we should use this feature with string|array<string>. Thanks for you help!

@@ -29,6 +29,9 @@ public function getProtocolVersion(): string
return $this->response->getProtocolVersion();
}

/**
* @return MessageInterface
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: This PHPDoc is redundant since it's already expressed by the native return type

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree that this is redundant but if I remove it I get the following error from PHPStan.

$ composer run-script phpstan
> phpstan analyse --no-progress --memory-limit 256M
Note: Using configuration file /.../Git/elasticsearch-php/phpstan.neon.
 ------ ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
  Line   Traits/MessageResponseTrait.php (in context of class Elastic\Elasticsearch\Response\Elasticsearch)                                                                                             
 ------ ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
  :34    Method Elastic\Elasticsearch\Response\Elasticsearch::withProtocolVersion() should return static(Elastic\Elasticsearch\Response\Elasticsearch) but returns Psr\Http\Message\ResponseInterface.  
         🪪  return.type                                                                                                                                                                                
 ------ ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 

I'm trying to understand where is the issue here, any idea? Thanks.

Copy link
Contributor

Choose a reason for hiding this comment

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

This looks to be because Elasticsearch is actually violating the ResponseInterface interface by returning an instance of the underlying ResponseInterface rather then the object it's being called on.

https://github.com/php-fig/http-message/blob/402d35bcb92c70c026d1a6a9883f06b2ead23d71/src/MessageInterface.php#L39

By adding the PHPDoc to the trait it's overwriting the one from the ResponseInterface which is why it silences the error.

I have opened a PR to fix this properly: #1440

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @AJenbo nice catch! I'll ask to wait the #1440 until we merge this PR: Moreover, can you rebase the PR to 8.17 branch? Thanks.

Copy link
Contributor

Choose a reason for hiding this comment

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

Done, I also set it to target the 8.17 branch so you can make it part of this PR.

* scroll?: int|string, // Specify how long a consistent view of the index should be maintained for scrolled search
* search_type?: string, // Search operation type
* search_timeout?: int|string, // Explicit timeout for each search request. Defaults to no timeout.
* max_docs?: float, // Maximum number of documents to process (default: all documents)
Copy link
Contributor

@AJenbo AJenbo Mar 22, 2025

Choose a reason for hiding this comment

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

Consider if int|float should be the type for number as some of these do not appear to make sens as fractions. If it can accept a string representation then you could set it as numeric as that is short for int|float|numeric-string. If float was really the only intended input the source would probably have been double instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For the Elasticsearch specification reported here, a number is alias for double (moreover it's deprecated). I agree that makes no sense for max_docs that should be always an integer. I will double check with the Elasticsearch team. Thanks!

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, in that case it sounds like the issue isn't related to this PR but more the upstream documentation 👍

* wait_for_completion?: bool, // Should the request should block until the reindex is complete.
* requests_per_second?: float, // The throttle to set on this request in sub-requests per second. -1 means no throttle.
* scroll?: int|string, // Control how long to keep the search context alive
* slices?: float|number|string, // The number of slices this task should be divided into. Defaults to 1, meaning the task isn't sliced into subtasks. Can be set to `auto`.
Copy link
Contributor

@AJenbo AJenbo Mar 22, 2025

Choose a reason for hiding this comment

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

This one still has the invalid number type as part of it's signature.

It's also valid to have the literal string in the PHPDoc type so the ideal type would be int|'auto'

* error_trace?: bool, // Include the stack trace of returned errors. (DEFAULT: false)
* source?: string, // The URL-encoded request definition. Useful for libraries that do not accept a request body for non-POST requests.
* filter_path?: string, // A comma-separated list of filters used to reduce the response.
* body: string|array, // (REQUIRED) The document. If body is a string must be a valid JSON.
Copy link
Contributor

Choose a reason for hiding this comment

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

To get closer to passing PHPStan level 6 you might want to hint this as either string|array<string, mixed> or string|array<mixed> (if lists are allowed)

* index_details?: bool, // Whether to include details of each index in the snapshot, if those details are available. Defaults to false.
* include_repository?: bool, // Whether to include the repository name in the snapshot info. Defaults to true.
* sort?: string, // Allows setting a sort order for the result. Defaults to start_time
* size?: integer, // Maximum number of snapshots to return. Defaults to 0 which means return all that match without limit.
Copy link
Contributor

Choose a reason for hiding this comment

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

Since you changed boolean to bool you might also want to change integer to int

Copy link
Contributor

@AJenbo AJenbo left a comment

Choose a reason for hiding this comment

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

Nice work. This change should improve things a lot.

I went over all the changes ad made comments where I think things can still be improved a bit in ways that would make it easier to work with the library and not get false positives.

@ezimuel
Copy link
Contributor Author

ezimuel commented Mar 23, 2025

Thanks @AJenbo for all your feedback and contribution. I'm finalizing this PR with all your feedback.

@ezimuel
Copy link
Contributor Author

ezimuel commented Mar 24, 2025

I'm closing this PR since it has been sent to main by mistake. I need to revert some changes and send it to 8.17.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants