Skip to content

Support the key-of keyword of Psalm #6084

@ste93cry

Description

@ste93cry

Feature request

A third party lib I'm using has defined an array shape that uses the key-of feature of Psalm. Unfortunately, PHPStan does not support it and therefore is not able to import the type and use it properly, giving back a bunch of errors. It would be cool if this feature was added, and because PHPStan already supports referencing the constant values as types I think that the foundation on top of which this feature could be built is already here. Would you accept a contribution to add such feature?

class Airports {  
  const JFK = 'jfk';  
  const LGA = 'lga';  
  const EWR = 'ewr';  

  const ALL = [  
    self::JFK => 'John F. Kennedy Airport',  
    self::LGA => 'La Guardia Airport',  
    self::EWR => 'Newark Liberty International',  
  ];  

  /**  
   * @psalm-param key-of<self::ALL> $code  
   */  
  public static function getName(string $code) : string {  
    return self::ALL[$code];  
  }  
}

Airports::getName(Airports::JFK); // good  
Airports::getName('lga'); // good  
Airports::getName('sfo'); // type error

Example taken from the blog post linked above

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions