elastic / elasticsearch-php

Official PHP client for Elasticsearch.
https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/index.html
MIT License
5.25k stars 964 forks source link

Psalm 5.14.0 and higher triggers issues with defined array-shapes #1346

Open jaytaph opened 11 months ago

jaytaph commented 11 months ago

Array shape declaration of certain methods are not compatible with psalm 5.14.0+.

InvalidArgument - src/Service/Elastic/IndexService.php:114:55 - Argument 1 of Elastic\Elasticsearch\Endpoints\Indices::exists expects array{allow_no_indices: bool, error_trace: bool, expand_wildcards: Elastic\Elasticsearch\Endpoints\enum, filter_path: list<mixed>, flat_settings: bool, human: bool, ignore_unavailable: bool, include_defaults: bool, index: list<mixed>, local: bool, pretty: bool, source: string}, but array{index: string} provided (see https://psalm.dev/004)
        $response = $this->elastic->indices()->exists(['index' => $indexName]);

This works correctly in psalm 5.13.1 or lower, but at 5.14.0 there has been some changes that will generate errors when not given all keys found in the definition.

     * @param array{
     *     index: list, // (REQUIRED) A comma-separated list of index names
     *     local: boolean, // Return local information, do not retrieve the state from master node (default: false)
     *     ignore_unavailable: boolean, // Ignore unavailable indexes (default: false)
     *     allow_no_indices: boolean, // Ignore if a wildcard expression resolves to no concrete indices (default: false)
     *     expand_wildcards: enum, // Whether wildcard expressions should get expanded to open or closed indices (default: open)
     *     flat_settings: boolean, // Return settings in flat format (default: false)
     *     include_defaults: boolean, // Whether to return all default setting for each of the indices.
     *     pretty: boolean, // Pretty format the returned JSON response. (DEFAULT: false)
     *     human: boolean, // Return human readable values for statistics. (DEFAULT: true)
     *     error_trace: boolean, // 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: list, // A comma-separated list of filters used to reduce the response.
     * } $params

This will probably function properly by adding a ? after the keyname in the array-shape.

     * @param array{
     *     index: list, // (REQUIRED) A comma-separated list of index names
     *     local?: boolean, // Return local information, do not retrieve the state from master node (default: false)
     *     ignore_unavailable?: boolean, // Ignore unavailable indexes (default: false)
     *     allow_no_indices?: boolean, // Ignore if a wildcard expression resolves to no concrete indices (default: false)
     *     expand_wildcards?: enum, // Whether wildcard expressions should get expanded to open or closed indices (default: open)
     *     flat_settings?: boolean, // Return settings in flat format (default: false)
     *     include_defaults?: boolean, // Whether to return all default setting for each of the indices.
     *     pretty?: boolean, // Pretty format the returned JSON response. (DEFAULT: false)
     *     human?: boolean, // Return human readable values for statistics. (DEFAULT: true)
     *     error_trace?: boolean, // 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?: list, // A comma-separated list of filters used to reduce the response.
     * } $params

See:

https://psalm.dev/r/fae4b76358 vs https://psalm.dev/r/9b72fe22b5

ezimuel commented 11 months ago

@jaytaph thanks for this PR, I'll have a look. Your proposal sounds good. I need to update the code generator, since these comments are generated using a separate tool.

MidnightDesign commented 10 months ago

@ezimuel Where can I find the codegen? I'd like to give it a shot.

reinschaap commented 8 months ago

@ezimuel what is the current status and planning for this issue?

reinschaap commented 7 months ago

@ezimuel again my question since you did not (yet) give us any answers.

what is the current status and planning for resolving this issue?

pluk commented 2 months ago

Hi!

@ezimuel are there any updates?