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

Psalm 5.14.0 and higher triggers issues with defined array-shapes

Open jaytaph opened this issue 2 years ago • 5 comments

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

jaytaph avatar Aug 08 '23 12:08 jaytaph