1

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();
}
7
  • before going any further : did you use the Jetbrains standard bug-fix method ??? that would be 'file->reindex&restart' Commented Jan 8, 2019 at 3:14
  • Haha yes, thank you though! Commented Jan 8, 2019 at 3:17
  • If ContainerInterface is own code, try adding a @var declaration in the interface for the logger var. Commented Jan 8, 2019 at 3:20
  • @var Monolog\Logger $this->container->logger -- this is simply wrong as you cannot typehint 3rd level entity like that. PHPDoc and PhpStorm allows typehinting only 1st level Commented Jan 8, 2019 at 10:05
  • Do you have any suggestions? Commented Jan 8, 2019 at 21:29

2 Answers 2

3

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());
Sign up to request clarification or add additional context in comments.

Comments

0

Interfaces cannot have state in PHP, only methods signatures.

For this error to disappear, you can try assigning $this->container to a variable, and type hint that explicitly with a concrete class.

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

This would work if logger is an actual property of that class and not something accessed through magic getters. In that case, you might have to assign logger to another variable and do the same explicit type hint.

Another option is to supress inpesection for that line. https://www.jetbrains.com/help/webstorm/2017.2/suppressing-inspections.html#d218445e68

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.