Autocompletion of nested array shape with @phpstan-type

As a library developer, I would like developers to get autocompletion and validation of nested array shapes. The following examples represent MongoDB Search Index Definition.

With the following code that contains PHPStan types (compatible Psalm), I would like PHPStorm:

  1. Suggest and autocomplete array keys and values when calling the function
  2. Display a nice warning message when the method is called with an incorrect array shape. The message should give the key path of the error to help debugging (psalm is better than phpstan for that point).
  3. Support union type of array shapes based on a field value. In this example SearchIndexField is an array with a shape that depends on the type value.

 

/**
 * @phpstan-type SearchIndexField array{type: 'boolean'|'date'|'dateFacet'|'objectId'|'stringFacet'|'uuid'} | array{type: 'autocomplete', analyzer?: string, maxGrams?: int, minGrams?: int, tokenization?: 'edgeGram'|'rightEdgeGram'|'nGram', foldDiacritics?: bool} | array{type: 'document'|'embeddedDocuments', dynamic?:bool, fields: array<string, array>} | array{type: 'geo', indexShapes?: bool} | array{type: 'number'|'numberFacet', representation?: 'int64'|'double', indexIntegers?: bool, indexDoubles?: bool} | array{type: 'token', normalizer?: 'lowercase'|'none'} | array{type: 'string', analyzer?: string, searchAnalyzer?: string, indexOptions?: 'docs'|'freqs'|'positions'|'offsets', store?: bool, ignoreAbove?: int, multi?: array<string, array>, norms?: 'include'|'omit'}
 * @phpstan-type SearchIndexAnalyser array{name: string, charFilters?: list<array<string, mixed>>, tokenizer: array{type: string}, tokenFilters?: list<array<string, mixed>>}
 * @phpstan-type SearchIndexStoredSource bool | array{includes: array<string>} | array{excludes: array<string>}
 * @phpstan-type SearchIndexDefinition array{analyser?: string, analyzers?: SearchIndexAnalyser[], searchAnalyzer?: string, mappings: array{dynamic: true} | array{dynamic?: bool, fields: array<string, SearchIndexField>}, storedSource?: SearchIndexStoredSource}
 * @phpstan-type VectorSearchIndexField array{type: 'vector', path: string, numDimensions: int, similarity: 'euclidean'|'cosine'|'dotProduct', quantization?: 'none'|'scalar'|'binary'}
 * @phpstan-type VectorSearchIndexDefinition array{fields: array<string, VectorSearchIndexField>}
 */
class Testing
{
    /** @phpstan-param SearchIndexDefinition $definition */
    public function searchIndex(array $definition): void
    {
        var_dump($definition);
    }

    /** @phpstan-param VectorSearchIndexDefinition $definition */
    public function vectorSearchIndex(array $definition): void
    {
        var_dump($definition);
    }
}

 

1

请先登录再写评论。