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

Add MethodGenerator::copyMethodSignature without body and phpdoc #132

Merged
merged 3 commits into from
Oct 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions src/Generator/MethodGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -88,8 +103,6 @@ public static function fromReflection(MethodReflection $reflectionMethod)
$method->setParameter(ParameterGenerator::fromReflection($reflectionParameter));
}

$method->setBody(static::clearBodyIndention($reflectionMethod->getBody()));

return $method;
}

Expand Down
11 changes: 11 additions & 0 deletions test/Generator/ClassGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}


}

Expand Down
13 changes: 12 additions & 1 deletion test/Generator/FileGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function testFromFileReflection()

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

$expectedOutput = <<<EOS
$expectedOutput = <<<'EOS'
<?php
/**
* File header here
Expand All @@ -140,6 +140,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;
}

public function foobar()
{
}
Expand Down
14 changes: 14 additions & 0 deletions test/Generator/MethodGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,20 @@ public function testDocBlockGetterAndSetter()
self::assertSame($docblockGenerator, $method->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');
Expand Down
15 changes: 15 additions & 0 deletions test/Generator/TestAsset/TestSampleSingleClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}