0

I have used an interface and a trait to fulfill the interface.

interface Attachable {
    public function getAttachment();
    public function setAttachment($attachment);
}

trait HasAttachment {
    public $attachment = null;

    public function getAttachment() {
        return $this->attachment;
    }

    public function setAttachment($attachment) {
        $this->attachment = $attachment;
    }
}

class SomeClass implements Attachable {
    use HasAttachment;

}

I need to unset the attachment in order to serialize and store the data. But when I try this, PHPStan shows error saying it is not a known property of Attachable

/** @var $data */
if ($data instanceof Attachable) {


    unset($data->attachment); // PHPStan cries here
}

I need some way to make phpstan happy in this scenario.

  • There is something wrong with your design, a class shouldn't have to use something like `if ($data instanceof Attachable) {` as this means the class has to know information about other classes used or not. – Nigel Ren Jan 02 '23 at 14:31
  • With $data = new SomeClass; before the if, PHPStan does not return any errors up to Level5. Please show the pristine PHPStan messages and also show how to generate $data. – jspit Jan 02 '23 at 18:53

0 Answers0