I'm currently working on a PHP project where I've implemented dependency injection in my code. The code is functioning correctly, but I'm encountering an issue with PhpStorm (version 2023.1) misinterpreting the code, which is causing typehinting to break. I'm reaching out to the community for assistance in resolving this problem and helping PhpStorm correctly interpret my code to restore seamless typehinting functionality.
To provide some context, here's a simplified example of the code:
class B {
public static function SayHello(){
echo 'Hello World';
}
}
class A {
/**
* @param B::class $B
*/
public function __construct(
private readonly string $B
){}
public function start(){
$this->B::SayHello(); // <---- I Would like that SayHallo() was available via typehinting
}
}
class AFactory extends A {
public function __construct(){
parent::__construct(B::class);
}
}
$class = new AFactory();
$class->start();
This behavior is puzzling because I have clearly indicated in the PHPDoc that $B
should be of type B::class
, referring to the class rather than just a generic string.
When attempting to address the issue, I made several attempts to inform PhpStorm that $this->B
should be recognized as an instance of the B
class, rather than just a string. One approach I tried was specifying in the PHPDoc that $B
should be considered an instance of the B
class. However, this approach introduced other problems.
PhpStorm interpreted the PHPDoc declaration as indicating that $B
had already been initialized, which was not the case. As a result, it introduced further confusion and did not resolve the issue as expected.
I would greatly appreciate any help or suggestions from the community to resolve this issue or find alternative ways to implement Dependency Injection effectively.