Search code examples
phpphpstorm

PhpStorm: Type hinting chained methods


In the code snippet below, I get type hinting on $contactInfo[0], and again on $order.

I would like the same with logger, which is an object of type \Monolog\Logger, accessed as a member of \psr\container\ContainerInterface

I am using PhpStorm which is warning me that Field 'logger' not found in Psr\Container\ContainerInterface

/**
 * @param Order $order
 * @param ContactInfo[] $contactInfo
 * @var Monolog\Logger $this->container->logger
 */
private function buildCreateOrderJSON(Order $order, $contactInfo)
{
    try {
        $currentDate = new DateTime();
    } catch (Exception $e) {
        $this->container->logger->addInfo('Some exception', $e->getMessage());
        return;
    }
    $lastName = $contactInfo[0]->getLastName();
    $order->getInvoiceNumber();
}

Solution

  • As LazyOne mentioned in the comments, you can't type hint a 3rd level entity.

    What you can do to retain method name refactoring is you can assign your class element to a variable, then type hint that:

    /** @var $logger \Monolog\Logger */
    $logger = $this->container->logger;
    $logger->addInfo('Some exception', $e->getMessage());