diff --git a/src/Generator/MethodGenerator.php b/src/Generator/MethodGenerator.php index 4d75e6b6..f9efd620 100644 --- a/src/Generator/MethodGenerator.php +++ b/src/Generator/MethodGenerator.php @@ -58,17 +58,32 @@ class MethodGenerator extends AbstractMemberGenerator */ public static function fromReflection(MethodReflection $reflectionMethod) { - $method = new static(); - $declaringClass = $reflectionMethod->getDeclaringClass(); + $method = static::copyMethodSignature($reflectionMethod); $method->setSourceContent($reflectionMethod->getContents(false)); $method->setSourceDirty(false); - $method->setReturnType(self::extractReturnTypeFromMethodReflection($reflectionMethod)); if ($reflectionMethod->getDocComment() != '') { $method->setDocBlock(DocBlockGenerator::fromReflection($reflectionMethod->getDocBlock())); } + $method->setBody(static::clearBodyIndention($reflectionMethod->getBody())); + + return $method; + } + + /** + * Returns a MethodGenerator based on a MethodReflection with only the signature copied. + * + * This is similar to fromReflection() but without the method body and phpdoc as this is quite heavy to copy. + * It's for example useful when creating proxies where you normally change the method body anyway. + */ + public static function copyMethodSignature(MethodReflection $reflectionMethod): MethodGenerator + { + $method = new static(); + $declaringClass = $reflectionMethod->getDeclaringClass(); + + $method->setReturnType(self::extractReturnTypeFromMethodReflection($reflectionMethod)); $method->setFinal($reflectionMethod->isFinal()); if ($reflectionMethod->isPrivate()) { @@ -88,8 +103,6 @@ public static function fromReflection(MethodReflection $reflectionMethod) $method->setParameter(ParameterGenerator::fromReflection($reflectionParameter)); } - $method->setBody(static::clearBodyIndention($reflectionMethod->getBody())); - return $method; } diff --git a/test/Generator/ClassGeneratorTest.php b/test/Generator/ClassGeneratorTest.php index 72a512b3..325b3d7e 100644 --- a/test/Generator/ClassGeneratorTest.php +++ b/test/Generator/ClassGeneratorTest.php @@ -816,6 +816,17 @@ public function someMethod() /* test test */ } + /** + * Enter description here... + * + * @return bool + */ + protected function withParamsAndReturnType($mixed, array $array, ?callable $callable = null, ?int $int = 0) : bool + { + /* test test */ + return true; + } + } diff --git a/test/Generator/FileGeneratorTest.php b/test/Generator/FileGeneratorTest.php index e61ceb98..f2ec0bc9 100644 --- a/test/Generator/FileGeneratorTest.php +++ b/test/Generator/FileGeneratorTest.php @@ -113,7 +113,7 @@ public function testFromFileReflection() $codeGenFileFromDisk->getClass()->addMethod('foobar'); - $expectedOutput = <<getDocBlock()); } + public function testCopyMethodSignature() + { + $ref = new MethodReflection(TestAsset\TestSampleSingleClass::class, 'withParamsAndReturnType'); + + $methodGenerator = MethodGenerator::copyMethodSignature($ref); + $target = <<<'EOS' + protected function withParamsAndReturnType($mixed, array $array, ?callable $callable = null, ?int $int = 0) : bool + { + } + +EOS; + self::assertEquals($target, (string) $methodGenerator); + } + public function testMethodFromReflection() { $ref = new MethodReflection(TestAsset\TestSampleSingleClass::class, 'someMethod'); diff --git a/test/Generator/TestAsset/TestSampleSingleClass.php b/test/Generator/TestAsset/TestSampleSingleClass.php index 34cf532f..c55f01f6 100644 --- a/test/Generator/TestAsset/TestSampleSingleClass.php +++ b/test/Generator/TestAsset/TestSampleSingleClass.php @@ -23,4 +23,19 @@ public function someMethod() /* test test */ } + /** + * Enter description here... + * + * @return bool + */ + protected function withParamsAndReturnType( + $mixed, + array $array, + callable $callable = null, + ?int $int = 0 + ): bool { + /* test test */ + return true; + } + }