phpDocumentor / fig-standards

Standards either proposed or approved by the Framework Interop Group
http://www.php-fig.org/
Other
360 stars 85 forks source link

Support rebound closure scope #113

Closed KorvinSzanto closed 6 years ago

KorvinSzanto commented 9 years ago

In writing tests for abstract classes, it's often useful to stub abstract methods with working code that can access protected members. In PHPUnit, this looks like this:

<?php

abstract class AbstractFoo
{

    protected function doSomething()
    {
        return 'foo';
    }

    abstract public function foo();

}

class FooTest extends PHPUnit_Framework_TestCase
{

    public function testFoo()
    {
        $mock = $this->getMockForAbstractClass('AbstractFoo');

        $foo_stub = function() {
            /** @type AbstractFoo $this */
            return $this->doSomething();
        };

        $mock->method('foo')->will($this->returnCallback($foo_stub->bindTo($mock, $mock)));
        $this->assertEquals('foo', $mock->foo());
    }

}

Would it be useful to be able to annotate the closure with something like @scope AbstractFoo so that IDE's don't mark $this->doSomething() as an error?

Fleshgrinder commented 9 years ago

See #64 that could be used for this as well.

TysonAndre commented 6 years ago

https://github.com/phan/phan/wiki/Annotating-Your-Source-Code#phan-closure-scope is similar to what is discussed here

ashnazg commented 6 years ago

@KorvinSzanto , if you wish to pursue this further, please bring this up as a new thread on the FIG mailing list for discussion -- https://groups.google.com/forum/#!forum/php-fig