Skip to content

Commit c99ef8e

Browse files
authored
Merge pull request #121 from Ocramius/feature/#119-allow-setting-reference-on-methods-generated-via-arrays
Allow setting by-reference return on methods generated via `MethodGenerator::fromArray()`, add `MethodGenerator#returnsReference()`
2 parents 4d57f38 + d697211 commit c99ef8e

File tree

2 files changed

+51
-9
lines changed

2 files changed

+51
-9
lines changed

src/Generator/MethodGenerator.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,17 @@ protected static function clearBodyIndention($body)
118118
/**
119119
* Generate from array
120120
*
121-
* @configkey name string [required] Class Name
122-
* @configkey docblock string The docblock information
123-
* @configkey flags int Flags, one of MethodGenerator::FLAG_ABSTRACT MethodGenerator::FLAG_FINAL
124-
* @configkey parameters string Class which this class is extending
125-
* @configkey body string
126-
* @configkey abstract bool
127-
* @configkey final bool
128-
* @configkey static bool
129-
* @configkey visibility string
121+
* @configkey name string [required] Class Name
122+
* @configkey docblock string The DocBlock information
123+
* @configkey flags int Flags, one of self::FLAG_ABSTRACT, self::FLAG_FINAL
124+
* @configkey parameters string Class which this class is extending
125+
* @configkey body string
126+
* @configkey returntype string
127+
* @configkey returnsreference bool
128+
* @configkey abstract bool
129+
* @configkey final bool
130+
* @configkey static bool
131+
* @configkey visibility string
130132
* @throws Exception\InvalidArgumentException
131133
* @param array $array
132134
* @return MethodGenerator
@@ -174,6 +176,8 @@ public static function fromArray(array $array)
174176
case 'returntype':
175177
$method->setReturnType($value);
176178
break;
179+
case 'returnsreference':
180+
$method->setReturnsReference((bool) $value);
177181
}
178182
}
179183

@@ -314,6 +318,11 @@ public function setReturnsReference($returnsReference)
314318
return $this;
315319
}
316320

321+
public function returnsReference(): bool
322+
{
323+
return $this->returnsReference;
324+
}
325+
317326
/**
318327
* Sort parameters by their position
319328
*/

test/Generator/MethodGeneratorTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,39 @@ public function testCreateFromArray()
320320
self::assertSame('\\SampleType', $methodGenerator->getReturnType()->generate());
321321
}
322322

323+
/**
324+
* @dataProvider returnsReferenceValues
325+
* @param bool|string|int $value
326+
* @param bool $expected
327+
*/
328+
public function testCreateFromArrayWithReturnsReference($value, $expected): void
329+
{
330+
$methodGenerator = MethodGenerator::fromArray([
331+
'name' => 'SampleMethod',
332+
'returnsreference' => $value,
333+
]);
334+
335+
self::assertSame($expected, $methodGenerator->returnsReference());
336+
}
337+
338+
/**
339+
* @return list<array{
340+
* bool|string|int,
341+
* bool
342+
* }>
343+
*/
344+
public function returnsReferenceValues(): array
345+
{
346+
return [
347+
[true, true],
348+
[1, true],
349+
['true', true],
350+
[false, false],
351+
[0, false],
352+
['', false],
353+
];
354+
}
355+
323356
public function testCreateInterfaceMethodFromArray()
324357
{
325358
$methodGenerator = MethodGenerator::fromArray([

0 commit comments

Comments
 (0)