noverify icon indicating copy to clipboard operation
noverify copied to clipboard

Incorrect type handling of 'self' in 'traits'

Open Danil42Russia opened this issue 2 years ago • 1 comments

<?php

trait TrainA {
  private ?string $name = null;

  public function name(string $name): self {
    $this->name = $name;

    return $this;
  }
}

class ClassA {
  use TrainA;

  private ?string $description = null;

  public function description(string $description): self {
    $this->description = $description;

    return $this;
  }

  public function __toString(): string {
    return "name: " . $this->name . PHP_EOL .
      "description: " . $this->description . PHP_EOL;
  }
}

function main() {
  $a = (new ClassA())
    ->name("name")
    ->description("description");

  echo $a;
}

main();

Actual Behavior:

<critical> ERROR   undefinedMethod: Call to undefined method {\TrainA}->description() at index.php:33
    ->description("description");
      ^^^^^^^^^^^

Expected Behavior:

No error

Danil42Russia avatar Oct 11 '22 15:10 Danil42Russia

Yeah when I initially added support for Traits, it was just to prevent reporting false positives for trait usage, but in general traits were not analysed properly and not really supported. I wonder if that changed since.

YuriyNasretdinov avatar Oct 12 '22 11:10 YuriyNasretdinov