Skip to content

Implement Method->getPrototype() fully #57

@asgrim

Description

@asgrim

Depends on ReflectionClass being implemented fully, in #7

I'm not 100% sure how this should be implemented, as ReflectionMethod::getPrototype() docs are pretty vague, and also the fixture below, from which I tested against core reflection and created a test case (see below) seems inconsistent.

From what I understand, ClassB::foo prototype is ClassA::foo, yet ClassC::foo which directly implements FooInterface descends into FooInterface::foo as the prototype.

It looks like traits are ignored for deciding the prototypes, which makes things slightly easier at least. Either way, we depend on ReflectionClass::getInterfaces() and ReflectionClass::getParentClass() to be implemented before we can do this (see #7).

Fixture, PrototypeTree.php

<?php

interface FooInterface {
    public function foo($a, $b);
}

abstract class ClassA implements FooInterface {
    abstract public function foo($a, $b);
}

class ClassB extends ClassA {
    public function foo($a, $b = 123) {}
}

class ClassC implements FooInterface {
    public function foo($a, $b = 123) {}
}

interface BarInterface {
    public function bar();
}

trait MyTrait {
    abstract public function bar();
}

class ClassT {
    use MyTrait;

    public function bar() {}
}

Failing test case:

public function testGetPrototype()
{
    $fixture = __DIR__ . '/../Fixture/PrototypeTree.php';
    $reflector = new ClassReflector(new SingleFileSourceLocator($fixture));

    $b = $reflector->reflect('ClassB')->getMethod('foo')->getPrototype();
    $this->assertInstanceOf(ReflectionMethod::class, $b);
    $this->assertSame('ClassA', $b->getDeclaringClass()->getName());

    $c = $reflector->reflect('ClassC')->getMethod('foo')->getPrototype();
    $this->assertInstanceOf(ReflectionMethod::class, $c);
    $this->assertSame('FooInterface', $c->getDeclaringClass()->getName());

    $this->setExpectedException(MethodPrototypeNotFound::class);
    $t = $reflector->reflect('ClassT')->getMethod('bar')->getPrototype();
}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions