function it_might_be_a_getter(ReflectionMethod $reflectionMethod) { $reflectionMethod->getNumberOfParameters()->willReturn(0); $reflectionMethod->getBodyCode()->willReturn('$this->myProperty = "test";'); $this->isGetterOf('myProperty')->shouldBe(false); $reflectionMethod->getBodyCode()->willReturn('return (string) $this->myProperty;'); $this->isGetterOf('myProperty')->shouldBe(true); $reflectionMethod->getBodyCode()->willReturn('return strval($this->myProperty);'); $this->isGetterOf('myProperty')->shouldBe(true); $reflectionMethod->getNumberOfParameters()->willReturn(1); $this->isGetterOf('myProperty')->shouldBe(false); }
/** * * * @param ReflectionMethod $reflection * @return Method */ public static function fromReflection(ReflectionMethod $reflection) { // gestion du type // $type = implode('|', $reflection->getDocBlockTypeStrings()); // // construction $method = new static($reflection->getName(), [], $reflection->getBodyCode()); // docblock $docblock = new \phpDocumentor\Reflection\DocBlock($reflection->getDocComment()); $method->setSummary($docblock->getShortDescription()); $method->setDescription($docblock->getLongDescription()); // gestion des modifiers $reflection->isPrivate() ? $method->enablePrivate() : $method->disablePrivate(); $reflection->isProtected() ? $method->enableProtected() : $method->disabledProtected(); $reflection->isPublic() ? $method->enablePublic() : $method->disablePublic(); $reflection->isStatic() ? $method->enableStatic() : $method->disableStatic(); $reflection->isFinal() ? $method->enableFinal() : $method->disableFinal(); foreach ($reflection->getParameters() as $parameter) { $method->addParameter(Parameter::fromReflection($parameter)); } return $method; }
public function isGetterOf(string $property) : bool { return $this->countParameters() === 0 && preg_match("/^return [^;]*\\\$this->{$property}[^;]*;\$/", $this->reflection->getBodyCode()); }