How to type context in the IDE?

I use Closure::call( http://php.net/manual/en/closure.call.php ) to trigger an external closure inside the class context.

Here is simple:

class Foo {
    private $bar = 'baz';

    /**
     * Executes a closure in $this context and returns whatever the closure returns.
     *
     * @param \Closure $closure
     * @return mixed
     */
    public function callClosureInThisContext(\Closure $closure) {
        return $closure->call($this);
    }
}

class Closures {
    /**
     * @return \Closure
     */
    public function getClosureForFoo() : \Closure {
        return function () {
            // how do I tell my IDE that in this context $this is actually class Foo,
            // and not the class Closures?
            print $this->bar;
        };
    }
}

$foo = new Foo();
$closures = new Closures();
$foo->callClosureInThisContext($closures->getClosureForFoo()); // prints "baz"

This works as expected, but my IDE, of course, is not happy and warns me about the "field barnot found": enter image description here

Is there any way to tell the IDE (in this case PhpStorm) that the closure will be used inside another class and that it should accept its context?

+4
source share
2 answers

to try

/** @var $this Foo */
print $this->bar;

It will add autocomplete for the Foo class in addition to Closure.

0
source

I do not think this can be done using phpdoc.

phpDocumentor, - @ @used-by tag.

, , .

0

Source: https://habr.com/ru/post/1688027/


All Articles