sirbrillig / phpcs-variable-analysis

Find undefined and unused variables with the PHP Codesniffer static analysis tool.
Other
135 stars 14 forks source link

Variable $this is undefined in enum #288

Closed satiromarra closed 1 year ago

satiromarra commented 1 year ago

When a function is defined inside an enum, the use of $this appears as an undefined variable.

enum Numbers: string {
  case ONE   = '1';
  case TWO   = '2';
  case THREE = '3';
  case FOUR  = '4';

  public function divisibility(): string {
    return match ($this) { // <--- Variable $this is undefined
      self::ONE, self::THREE => 'odd',
      self::TWO, self::FOUR => 'even',
    };
  }
}
sirbrillig commented 1 year ago

Thanks! I think we need to extend the class detection to also detect enums.