From bc5ec41da83dec2baa1b4403cc7cde817b6215ca Mon Sep 17 00:00:00 2001 From: Tobias Schultze Date: Wed, 11 Oct 2017 12:16:16 +0200 Subject: [PATCH 1/3] Add method to copy method signature from reflection without the body and phpdoc --- src/Generator/MethodGenerator.php | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) 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; } From c4f4f2c77e230e4612a8652b4e87116d0d0756ee Mon Sep 17 00:00:00 2001 From: Tobias Schultze Date: Wed, 11 Oct 2017 18:24:03 +0200 Subject: [PATCH 2/3] add test for copyMethodSignature --- test/Generator/ClassGeneratorTest.php | 11 +++++++++++ test/Generator/FileGeneratorTest.php | 13 ++++++++++++- test/Generator/MethodGeneratorTest.php | 14 ++++++++++++++ .../TestAsset/TestSampleSingleClass.php | 17 +++++++++++++++++ 4 files changed, 54 insertions(+), 1 deletion(-) diff --git a/test/Generator/ClassGeneratorTest.php b/test/Generator/ClassGeneratorTest.php index 72a512b3..113b76c2 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, ?string $string = null, iterable $iterable = array(), ?int $int = 0) : bool + { + /* test test */ + return true; + } + } diff --git a/test/Generator/FileGeneratorTest.php b/test/Generator/FileGeneratorTest.php index e61ceb98..0aa6dd80 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, ?string $string = null, iterable $iterable = array(), ?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..0ed25fd2 100644 --- a/test/Generator/TestAsset/TestSampleSingleClass.php +++ b/test/Generator/TestAsset/TestSampleSingleClass.php @@ -23,4 +23,21 @@ public function someMethod() /* test test */ } + /** + * Enter description here... + * + * @return bool + */ + protected function withParamsAndReturnType( + $mixed, + array $array, + callable $callable, + string $string = null, + iterable $iterable = [], + ?int $int = 0 + ): bool { + /* test test */ + return true; + } + } From 50d61eaf995d506e865f2d90506a300403598843 Mon Sep 17 00:00:00 2001 From: Tobias Schultze Date: Thu, 12 Oct 2017 01:09:08 +0200 Subject: [PATCH 3/3] fix line length cs --- test/Generator/ClassGeneratorTest.php | 2 +- test/Generator/FileGeneratorTest.php | 2 +- test/Generator/MethodGeneratorTest.php | 2 +- test/Generator/TestAsset/TestSampleSingleClass.php | 4 +--- 4 files changed, 4 insertions(+), 6 deletions(-) diff --git a/test/Generator/ClassGeneratorTest.php b/test/Generator/ClassGeneratorTest.php index 113b76c2..325b3d7e 100644 --- a/test/Generator/ClassGeneratorTest.php +++ b/test/Generator/ClassGeneratorTest.php @@ -821,7 +821,7 @@ public function someMethod() * * @return bool */ - protected function withParamsAndReturnType($mixed, array $array, callable $callable, ?string $string = null, iterable $iterable = array(), ?int $int = 0) : 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 0aa6dd80..f2ec0bc9 100644 --- a/test/Generator/FileGeneratorTest.php +++ b/test/Generator/FileGeneratorTest.php @@ -145,7 +145,7 @@ public function someMethod() * * @return bool */ - protected function withParamsAndReturnType($mixed, array $array, callable $callable, ?string $string = null, iterable $iterable = array(), ?int $int = 0) : bool + protected function withParamsAndReturnType($mixed, array $array, ?callable $callable = null, ?int $int = 0) : bool { /* test test */ return true; diff --git a/test/Generator/MethodGeneratorTest.php b/test/Generator/MethodGeneratorTest.php index cb0c027d..9b668498 100644 --- a/test/Generator/MethodGeneratorTest.php +++ b/test/Generator/MethodGeneratorTest.php @@ -100,7 +100,7 @@ public function testCopyMethodSignature() $methodGenerator = MethodGenerator::copyMethodSignature($ref); $target = <<<'EOS' - protected function withParamsAndReturnType($mixed, array $array, callable $callable, ?string $string = null, iterable $iterable = array(), ?int $int = 0) : bool + protected function withParamsAndReturnType($mixed, array $array, ?callable $callable = null, ?int $int = 0) : bool { } diff --git a/test/Generator/TestAsset/TestSampleSingleClass.php b/test/Generator/TestAsset/TestSampleSingleClass.php index 0ed25fd2..c55f01f6 100644 --- a/test/Generator/TestAsset/TestSampleSingleClass.php +++ b/test/Generator/TestAsset/TestSampleSingleClass.php @@ -31,9 +31,7 @@ public function someMethod() protected function withParamsAndReturnType( $mixed, array $array, - callable $callable, - string $string = null, - iterable $iterable = [], + callable $callable = null, ?int $int = 0 ): bool { /* test test */