Skip to content

Commit

Permalink
Merge pull request #121 from Ocramius/feature/#119-allow-setting-refe…
Browse files Browse the repository at this point in the history
…rence-on-methods-generated-via-arrays

Allow setting by-reference return on methods generated via `MethodGenerator::fromArray()`, add `MethodGenerator#returnsReference()`
  • Loading branch information
Ocramius authored Dec 7, 2021
2 parents 4d57f38 + d697211 commit c99ef8e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 9 deletions.
27 changes: 18 additions & 9 deletions src/Generator/MethodGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,17 @@ protected static function clearBodyIndention($body)
/**
* Generate from array
*
* @configkey name string [required] Class Name
* @configkey docblock string The docblock information
* @configkey flags int Flags, one of MethodGenerator::FLAG_ABSTRACT MethodGenerator::FLAG_FINAL
* @configkey parameters string Class which this class is extending
* @configkey body string
* @configkey abstract bool
* @configkey final bool
* @configkey static bool
* @configkey visibility string
* @configkey name string [required] Class Name
* @configkey docblock string The DocBlock information
* @configkey flags int Flags, one of self::FLAG_ABSTRACT, self::FLAG_FINAL
* @configkey parameters string Class which this class is extending
* @configkey body string
* @configkey returntype string
* @configkey returnsreference bool
* @configkey abstract bool
* @configkey final bool
* @configkey static bool
* @configkey visibility string
* @throws Exception\InvalidArgumentException
* @param array $array
* @return MethodGenerator
Expand Down Expand Up @@ -174,6 +176,8 @@ public static function fromArray(array $array)
case 'returntype':
$method->setReturnType($value);
break;
case 'returnsreference':
$method->setReturnsReference((bool) $value);
}
}

Expand Down Expand Up @@ -314,6 +318,11 @@ public function setReturnsReference($returnsReference)
return $this;
}

public function returnsReference(): bool
{
return $this->returnsReference;
}

/**
* Sort parameters by their position
*/
Expand Down
33 changes: 33 additions & 0 deletions test/Generator/MethodGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,39 @@ public function testCreateFromArray()
self::assertSame('\\SampleType', $methodGenerator->getReturnType()->generate());
}

/**
* @dataProvider returnsReferenceValues
* @param bool|string|int $value
* @param bool $expected
*/
public function testCreateFromArrayWithReturnsReference($value, $expected): void
{
$methodGenerator = MethodGenerator::fromArray([
'name' => 'SampleMethod',
'returnsreference' => $value,
]);

self::assertSame($expected, $methodGenerator->returnsReference());
}

/**
* @return list<array{
* bool|string|int,
* bool
* }>
*/
public function returnsReferenceValues(): array
{
return [
[true, true],
[1, true],
['true', true],
[false, false],
[0, false],
['', false],
];
}

public function testCreateInterfaceMethodFromArray()
{
$methodGenerator = MethodGenerator::fromArray([
Expand Down

0 comments on commit c99ef8e

Please sign in to comment.