We're trying to implement type hints for an app using a custom factory pattern. Each class is already defined in a separate file so we have tried to dynamically construct hints. We have been unsuccessful at any attempt to dynamically construct the .phpstorm.meta.php file, however when we pass a string directly rather than assigning a variable, we are able to see type hints.
To troubleshoot, I created a PHP Storm free trial and when configured with the code below, does not present any issues with type hinting. I also tried PHP Intellisense and it's broken there as well.
Given the following files in VS Code, I am getting type hints for class Test1
but not Test2
.
.phpstorm.meta.php
<?php
namespace PHPSTORM_META {
$test2 = \TestSpace\Test2::class;
override(\TestSpace\Factory::get(), map([
'\TestSpace\Test' => \TestSpace\Test::class, // type hints works with this one
'\TestSpace\Test2' => $test2, // but it does not when a variable is assigned
]));
}
phpstorm.sample.php
<?php
namespace TestSpace;
class Factory {
public static function get($name) {
$class = new \ReflectionClass($name);
return $class->newInstanceArgs();
}
}
class Test {
public function test() {
return 'test';
}
}
class Test2 {
public function test2() {
return 'test2';
}
}
echo Factory::get('\TestSpace\Test')->test() . "\n"; // type hinting works here when I mouse over test()
echo Factory::get('\TestSpace\Test2')->test2() . "\n"; // but not here when I mouse over test2()
Any idea why the variable $test
would not work here but the string that's assigned to it would instead?