phpstan-drupal icon indicating copy to clipboard operation
phpstan-drupal copied to clipboard

How to handle type inferencing for $entity->get('field_ref')->entity?

Open mrweiner opened this issue 3 years ago • 0 comments

I've got a custom ContentEntity with the following:

  /**
   * {@inheritdoc}
   */
  public function getOrderItem(): ?OrderItemInterface {
    return $this->get('order_item')->entity;
  }

and PHPStan level 3 is spitting out Method Drupal\hc_commerce_transfer\Entity\CommerceTransferItem::getOrderItem() should return Drupal\commerce_order\Entity\OrderItemInterface|null but returns Drupal\Core\Entity\ContentEntityInterface.

Is there a recommended way of handling this? All I can figure is to either use @phpstan-ignore or to add an ignore rule for the pattern, but those don't seem ideal.

mrweiner avatar Aug 04 '22 18:08 mrweiner