| Subject | Details |
| :------------- | :---------------------------------------------------------------------------- |
| Issue type | Feature Request |
| Plugin | Php Inspections (EA Ultimate), 2.0.12 |
| Language level | PHP 7.3 |
Here is some example code:
<?php declare(strict_types=1);
final class Foo
{
public function bar(): Closure
{
return static function () {
return $this->haha();
};
}
private function haha(): string
{
return 'I love Inspections!';
}
}
(new Foo())->bar()();
No warning is displayed.
Display a warning that this leads to a fatal error (see https://3v4l.org/2v7qZ). $this cannot be accessed when not in an object context (which it is when the Closure is declared as static).
When static is removed from the Closure being returned, the above code works.
Great code snipplet, Andreas!
Implemented!
@kalessil Is this update is mean to be also included into community version of plugin? There is recently added Static closure can be used inspection and its use without this update may lead to broken code.
Only ultimate. Static closure can be used checks if $this has been used, so I'm not sure how we can break the code, same as the similar rule in PHP CS Fixer. Can you elaborate?
Static closure can be used works correctly, but in a case if closure contents get changed with adding $this inside static closure - there is no report from IDE and this situation effectively leads to fatal error in runtime. Of course it is not a fault of your (exceptionally useful) plugin.
Thank you for your answer, I will certainly consider buying ultimate version in this case.
@kalessil Please consider promoting this inspection to the OSS edition. I agree with @FlyingDR, the two rules really seem like they should go together. If you start changing closures to static based on the inspection in the open source version and accidentally introduce a $this, it will go unnoticed and eventually generate a fatal error.
I don't know how you differentiate what goes into the Ultimate edition, but I got the impression it was primarily quality enhancements and not fatal error detection. This is the second time I've been hit with this particular issue, so I'm sure it's happening to a few others as well. Thank you!
@FlyingDR, @hackel: ok, will move it into extended.
See #1497 for the status updates.