Verified on latest main
PHPantom version / commit
33718a6
Installation method
Built from source
Operating system
Linux x86_64
Editor
VS Code
Bug description
Happens both with the prebuilt VSCode version and with the latest main version via cargo.
If you have a class with a method that returns $this and uses a closure as an argument, chaining further methods afterwards completely disables type recognition within the closure.
Steps to reproduce
- Create a file with the following content:
<?php
class ClosureTest
{
public function useClosure(\Closure $closure): static
{
return $this;
}
public function return(): string
{
return 'test';
}
}
$closure = new ClosureTest();
$closure->useClosure(function (string $s) {
echo $s;
$test = 'a';
});
- When hovering over
$s within the closure body, it shows $s = string as expected. When hovering over $test, it shows $test = 'a' as expected.
- Chain another class method like so:
<?php
class ClosureTest
{
public function useClosure(\Closure $closure): static
{
return $this;
}
public function return(): string
{
return 'test';
}
}
$closure = new ClosureTest();
$closure->useClosure(function (string $s) {
echo $s;
$test = 'a';
})->return();
- Expected: Same behavior as in 2.
- Actual: When hovering over
$s within the closure body now, it only shows $s. The type information is lost within the closure body, even though the closure definition defines it as string. Likewise, when hovering over $test, it now also only shows $test and loses the type information.
When explicitly adding type information with phpstan, it works for variables:
- Create a file with the following content:
<?php
class ClosureTest
{
public function useClosure(\Closure $closure): static
{
return $this;
}
public function return(): string
{
return 'test';
}
}
$closure = new ClosureTest();
/** @var string $s */
$closure->useClosure(function (string $s) {
echo $s;
/** @var string $test */
$test = 'a';
})->return();
- Hovering over
$s and $test in the closure body yields type info of string for both.
However, typing the closure more strictly with phpstan does not change anything from the initially-described case:
<?php
class ClosureTest
{
/**
* @param (\Closure(string $s): void) $closure
*
* @return $this
*/
public function useClosure(\Closure $closure): static
{
return $this;
}
public function return(): string
{
return 'test';
}
}
$closure = new ClosureTest();
$closure->useClosure(function (string $s) {
echo $s;
$test = 'a';
})->return();
Error output or panic trace
.phpantom.toml
Additional context
No response
Verified on latest main
mainbranch.PHPantom version / commit
33718a6
Installation method
Built from source
Operating system
Linux x86_64
Editor
VS Code
Bug description
Happens both with the prebuilt VSCode version and with the latest
mainversion via cargo.If you have a class with a method that returns
$thisand uses a closure as an argument, chaining further methods afterwards completely disables type recognition within the closure.Steps to reproduce
$swithin the closure body, it shows$s = stringas expected. When hovering over$test, it shows$test = 'a'as expected.$swithin the closure body now, it only shows$s. The type information is lost within the closure body, even though the closure definition defines it asstring. Likewise, when hovering over$test, it now also only shows$testand loses the type information.When explicitly adding type information with phpstan, it works for variables:
$sand$testin the closure body yields type info ofstringfor both.However, typing the closure more strictly with phpstan does not change anything from the initially-described case:
Error output or panic trace
.phpantom.toml
default / no config fileAdditional context
No response