This issue is similar to #252 but it happens inside the if ($expr instanceof Node\Expr\FuncCall) { logic.
This is the error I get in _DefinitionResolver.php_ when the server is parsing this file:
extensionHost.ts:282 [Extension Host] TypeError: Return value of LanguageServer\DefinitionResolver::resolveExpressionNodeToType() must implement interface phpDocumentor\Reflection\Type, null returned in /Users/bcarreco/.vscode/extensions/felixfbecker.php-intellisense-1.1.1/vendor/felixfbecker/language-server/src/DefinitionResolver.php:444
I was able to fix it by adding this code to that line:
if (strtolower((string)$def) === 'null') {
return new Types\Null_;
}
final changed code:
if ($expr instanceof Node\Expr\FuncCall) {
// Find the function definition
if ($expr->name instanceof Node\Expr) {
// Cannot get type for dynamic function call
return new Types\Mixed;
}
$fqn = (string)($expr->getAttribute('namespacedName') ?? $expr->name);
$def = $this->index->getDefinition($fqn, true);
if (strtolower((string)$def) === 'null') {
return new Types\Null_;
}
if ($def !== null) {
return $def->type;
}
}
Not sure that's the right approach but it fixed the problem.
I've been experiencing this same issue.
```TypeError: Return value of LanguageServer\DefinitionResolver::resolveExpressionNodeToType() must implement interface phpDocumentor\Reflection\Type, null returned in /Users/christopher_carman/.vscode/extensions/felixfbecker.php-intellisense-1.1.3/vendor/felixfbecker/language-server/src/DefinitionResolver.php:445
Stack trace:
messageService.ts:126 The PHP Language Server server crashed 5 times in the last 3 minutes. The server will not be restarted.
```
Based on the stack trace, it appears that $def->type is null here: https://github.com/felixfbecker/php-language-server/blob/56bd465bf8012e6b8842439ade7683e639ce8f32/src/DefinitionResolver.php#L445
The file triggering the error appears to be this: https://github.com/laravel/lumen-framework/blob/v5.2.9/tests/FullApplicationTest.php
EDIT: Commented out the contents and indexing completed successfully. That seems to be the only problematic file. Strange.
@carmanchris31, I was able to fix this by adding this check before the return $def->type;.
if ( null === $def->type ) {
return new Types\Mixed;
}
The final logic should be something like this:
if ($def !== null) {
// CUSTOM.
if ( null === $def->type ) {
return new Types\Mixed;
}
//
return $def->type;
}
I had the same problem. Was able to fix it by:
if ($def !== null) {
return $def->type ?: new Types\Mixed;
}
Then it all work fine. No error lines in the developer's console.
@felixfbecker This bug occurs because define() nodes in the stubs file do not have a type set.
Also, this only seems to happen when the constant is used in an array that is used to initialize a class member.
You can successfuly reproduce the crash with the following code:
class Test {
private $foo = [PHP_INT_MAX];
}
I would open a PR but I don't know how the stubs file is created.
Awesome find! The stuns file is generated in ComposerScripts.php, but the bug likely is in DefinitionResolver
Hi, shouldn't this bug be closed as the fix has already been merged?
Most helpful comment
@carmanchris31, I was able to fix this by adding this check before the
return $def->type;.The final logic should be something like this: