8000 Merge pull request #132 from Tobion/copy-method-signature · zendframework/zend-code@2cd2574 · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit 2cd2574

Browse files
authored
Merge pull request #132 from Tobion/copy-method-signature
Add MethodGenerator::copyMethodSignature without body and phpdoc
2 parents 3149f6f + 50d61ea commit 2cd2574

File tree

5 files changed

+70
-6
lines changed

5 files changed

+70
-6
lines changed

src/Generator/MethodGenerator.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,32 @@ class MethodGenerator extends AbstractMemberGenerator
5858
*/
5959
public static function fromReflection(MethodReflection $reflectionMethod)
6060
{
61-
$method = new static();
62-
$declaringClass = $reflectionMethod->getDeclaringClass();
61+
$method = static::copyMethodSignature($reflectionMethod);
6362

6463
$method->setSourceContent($reflectionMethod->getContents(false));
6564
$method->setSourceDirty(false);
66-
$method->setReturnType(self::extractReturnTypeFromMethodReflection($reflectionMethod));
6765

6866
if ($reflectionMethod->getDocComment() != '') {
6967
$method->setDocBlock(DocBlockGenerator::fromReflection($reflectionMethod->getDocBlock()));
7068
}
7169

70+
$method->setBody(static::clearBodyIndention($reflectionMethod->getBody()));
71+
72+
return $method;
73+
}
74+
75+
/**
76+
* Returns a MethodGenerator based on a MethodReflection with only the signature copied.
77+
*
78+
* This is similar to fromReflection() but without the method body and phpdoc as this is quite heavy to copy.
79+
* It's for example useful when creating proxies where you normally change the method body anyway.
80+
*/
81+
public static function copyMethodSignature(MethodReflection $reflectionMethod): MethodGenerator
82+
{
83+
$method = new static();
84+
$declaringClass = $reflectionMethod->getDeclaringClass();
85+
86+
$method->setReturnType(self::extractReturnTypeFromMethodReflection($reflectionMethod));
7287
$method->setFinal($reflectionMethod->isFinal());
7388

7489
if ($reflectionMethod->isPrivate()) {
@@ -88,8 +103,6 @@ public static function fromReflection(MethodReflection $reflectionMethod)
88103
$method->setParameter(ParameterGenerator::fromReflection($reflectionParameter));
89104
}
90105

91-
$method->setBody(static::clearBodyIndention($reflectionMethod->getBody()));
92-
93106
return $method;
94107
}
95108

test/Generator/ClassGeneratorTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,17 @@ public function someMethod()
816816
/* test test */
817817
}
818818
819+
/**
820+
* Enter description here...
821+
*
822+
* @return bool
823+
*/
824+
protected function withParamsAndReturnType($mixed, array $array, ?callable $callable = null, ?int $int = 0) : bool
825+
{
826+
/* test test */
827+
return true;
828+
}
829+
819830
820831
}
821832

test/Generator/FileGeneratorTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public function testFromFileReflection()
113113

114114
$codeGenFileFromDisk->getClass()->addMethod('foobar');
115115

116-
$expectedOutput = <<<EOS
116+
$expectedOutput = <<<'EOS'
117117
<?php
118118
/**
119119
* File header here
@@ -140,6 +140,17 @@ public function someMethod()
140140
/* test test */
141141
}
142142
143+
/**
144+
* Enter description here...
145+
*
146+
* @return bool
147+
*/
148+
protected function withParamsAndReturnType($mixed, array $array, ?callable $callable = null, ?int $int = 0) : bool
149+
{
150+
/* test test */
151+
return true;
152+
}
153+
143154
public function foobar()
144155
{
145156
}

test/Generator/MethodGeneratorTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,20 @@ public function testDocBlockGetterAndSetter()
9494
self::assertSame($docblockGenerator, $method->getDocBlock());
9595
}
9696

97+
public function testCopyMethodSignature()
98+
{
99+
$ref = new MethodReflection(TestAsset\TestSampleSingleClass::class, 'withParamsAndReturnType');
100+
101+
$methodGenerator = MethodGenerator::copyMethodSignature($ref);
102+
$target = <<<'EOS'
103+
protected function withParamsAndReturnType($mixed, array $array, ?callable $callable = null, ?int $int = 0) : bool
104+
{
105+
}
106+
107+
EOS;
108+
self::assertEquals($target, (string) $methodGenerator);
109+
}
110+
97111
public function testMethodFromReflection()
98112
{
99113
$ref = new MethodReflection(TestAsset\TestSampleSingleClass::class, 'someMethod');

test/Generator/TestAsset/TestSampleSingleClass.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,19 @@ public function someMethod()
2323
/* test test */
2424
}
2525

26+
/**
27+
* Enter description here...
28+
*
29+
* @return bool
30+
*/
31+
protected function withParamsAndReturnType(
32+
$mixed,
33+
array $array,
34+
callable $callable = null,
35+
?int $int = 0
36+
): bool {
37+
/* test test */
38+
return true;
39+
}
40+
2641
}

0 commit comments

Comments
 (0)
0