Skip to content

Commit c9f9516

Browse files
Added function param passedByReference
1 parent 739d275 commit c9f9516

File tree

3 files changed

+150
-6
lines changed

3 files changed

+150
-6
lines changed

src/Formatter/FunctionFormatter.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ protected function formatParams(): string
4949
$param .= ' ';
5050
}
5151

52+
if ($parameter->isPassedByReference()) {
53+
$param .= '&';
54+
}
55+
5256
if ($parameter->isVariadic()) {
5357
$param .= '...';
5458
}

tests/unit/Formatter/FunctionFormatterTest.php

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ protected function createReflectionParameterMock(
2929
bool $hasDefault,
3030
?string $defaultConstant,
3131
$defaultValue,
32-
bool $isVariadic
32+
bool $isVariadic,
33+
bool $isPassedByReference
3334
): \ReflectionParameter {
3435
$result = $this->getMockBuilder(\ReflectionParameter::class)
3536
->setMethods([
@@ -39,7 +40,8 @@ protected function createReflectionParameterMock(
3940
'isDefaultValueConstant',
4041
'getDefaultValueConstantName',
4142
'getDefaultValue',
42-
'isVariadic'
43+
'isVariadic',
44+
'isPassedByReference'
4345
])
4446
->disableOriginalConstructor()
4547
->getMock();
@@ -63,6 +65,7 @@ protected function createReflectionParameterMock(
6365
);
6466
}
6567
$result->method('isVariadic')->willReturn($isVariadic);
68+
$result->method('isPassedByReference')->willReturn($isPassedByReference);
6669

6770
/** @noinspection PhpIncompatibleReturnTypeInspection */
6871
return $result;
@@ -147,6 +150,7 @@ public function testFunctionWithParams()
147150
false,
148151
null,
149152
null,
153+
false,
150154
false
151155
);
152156

@@ -156,6 +160,7 @@ public function testFunctionWithParams()
156160
false,
157161
null,
158162
null,
163+
false,
159164
false
160165
);
161166

@@ -165,6 +170,7 @@ public function testFunctionWithParams()
165170
true,
166171
'TEST_CONSTANT',
167172
null,
173+
false,
168174
false
169175
);
170176

@@ -174,6 +180,7 @@ public function testFunctionWithParams()
174180
true,
175181
null,
176182
123,
183+
false,
177184
false
178185
);
179186
$reflectionFunction = $this->createReflectionFunctionMock('test', [$a, $b, $c, $d], null, null);
@@ -193,6 +200,7 @@ public function testFunctionWithVariadicParam()
193200
false,
194201
null,
195202
null,
203+
false,
196204
false
197205
);
198206

@@ -202,14 +210,75 @@ public function testFunctionWithVariadicParam()
202210
false,
203211
null,
204212
null,
205-
true
213+
true,
214+
false
206215
);
207216
$reflectionFunction = $this->createReflectionFunctionMock('test', [$a, $b], null, null);
208217

209218
$expectedOutput = $t . 'function test(int $a, ...$b) {}' . $n . $n;
210219
$this->assertSame($expectedOutput, (new FunctionFormatter($reflectionFunction))->format());
211220
}
212221

222+
public function testFunctionWithPassedByReferenceParam()
223+
{
224+
$n = PhpStubGenerator::$eol;
225+
$t = PhpStubGenerator::$tab;
226+
227+
$a = $this->createReflectionParameterMock(
228+
'a',
229+
$this->createReflectionTypeMock('int'),
230+
false,
231+
null,
232+
null,
233+
false,
234+
true
235+
);
236+
237+
$b = $this->createReflectionParameterMock(
238+
'b',
239+
null,
240+
false,
241+
null,
242+
null,
243+
false,
244+
true
245+
);
246+
$reflectionFunction = $this->createReflectionFunctionMock('test', [$a, $b], null, null);
247+
248+
$expectedOutput = $t . 'function test(int &$a, &$b) {}' . $n . $n;
249+
$this->assertSame($expectedOutput, (new FunctionFormatter($reflectionFunction))->format());
250+
}
251+
252+
public function testFunctionWithVariadicPassedByReferenceParam()
253+
{
254+
$n = PhpStubGenerator::$eol;
255+
$t = PhpStubGenerator::$tab;
256+
257+
$a = $this->createReflectionParameterMock(
258+
'a',
259+
$this->createReflectionTypeMock('int'),
260+
false,
261+
null,
262+
null,
263+
false,
264+
false
265+
);
266+
267+
$b = $this->createReflectionParameterMock(
268+
'b',
269+
null,
270+
false,
271+
null,
272+
null,
273+
true,
274+
true
275+
);
276+
$reflectionFunction = $this->createReflectionFunctionMock('test', [$a, $b], null, null);
277+
278+
$expectedOutput = $t . 'function test(int $a, &...$b) {}' . $n . $n;
279+
$this->assertSame($expectedOutput, (new FunctionFormatter($reflectionFunction))->format());
280+
}
281+
213282
public function testFunctionWithDoc()
214283
{
215284
$n = PhpStubGenerator::$eol;

tests/unit/Formatter/MethodFormatterTest.php

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ protected function createReflectionParameterMock(
3030
bool $hasDefault,
3131
?string $defaultConstant,
3232
$defaultValue,
33-
bool $isVariadic
33+
bool $isVariadic,
34+
bool $isPassedByReference
3435
): \ReflectionParameter {
3536
$result = $this->getMockBuilder(\ReflectionParameter::class)
3637
->setMethods([
@@ -40,7 +41,8 @@ protected function createReflectionParameterMock(
4041
'isDefaultValueConstant',
4142
'getDefaultValueConstantName',
4243
'getDefaultValue',
43-
'isVariadic'
44+
'isVariadic',
45+
'isPassedByReference'
4446
])
4547
->disableOriginalConstructor()
4648
->getMock();
@@ -64,6 +66,7 @@ protected function createReflectionParameterMock(
6466
);
6567
}
6668
$result->method('isVariadic')->willReturn($isVariadic);
69+
$result->method('isPassedByReference')->willReturn($isPassedByReference);
6770

6871
/** @noinspection PhpIncompatibleReturnTypeInspection */
6972
return $result;
@@ -261,6 +264,7 @@ public function testMethodWithParams()
261264
false,
262265
null,
263266
null,
267+
false,
264268
false
265269
);
266270

@@ -270,6 +274,7 @@ public function testMethodWithParams()
270274
false,
271275
null,
272276
null,
277+
false,
273278
false
274279
);
275280

@@ -279,6 +284,7 @@ public function testMethodWithParams()
279284
true,
280285
'TEST_CONSTANT',
281286
null,
287+
false,
282288
false
283289
);
284290

@@ -288,6 +294,7 @@ public function testMethodWithParams()
288294
true,
289295
null,
290296
123,
297+
false,
291298
false
292299
);
293300
$modifiers = ReflectionMethod::IS_PUBLIC;
@@ -316,6 +323,7 @@ public function testMethodWithVariadicParam()
316323
false,
317324
null,
318325
null,
326+
false,
319327
false
320328
);
321329

@@ -325,7 +333,8 @@ public function testMethodWithVariadicParam()
325333
false,
326334
null,
327335
null,
328-
true
336+
true,
337+
false
329338
);
330339
$modifiers = ReflectionMethod::IS_PUBLIC;
331340
$reflectionMethod = $this->createReflectionMethodMock('test', 'SomeClass', $modifiers, [$a, $b], null, null);
@@ -334,6 +343,68 @@ public function testMethodWithVariadicParam()
334343
$this->assertSame($expectedOutput, (new MethodFormatter('SomeClass', false, $reflectionMethod))->format());
335344
}
336345

346+
public function testMethodWithPassedByReferenceParam()
347+
{
348+
$n = PhpStubGenerator::$eol;
349+
$t = PhpStubGenerator::$tab;
350+
351+
$a = $this->createReflectionParameterMock(
352+
'a',
353+
$this->createReflectionTypeMock('int'),
354+
false,
355+
null,
356+
null,
357+
false,
358+
true
359+
);
360+
361+
$b = $this->createReflectionParameterMock(
362+
'b',
363+
null,
364+
false,
365+
null,
366+
null,
367+
false,
368+
true
369+
);
370+
$modifiers = ReflectionMethod::IS_PUBLIC;
371+
$reflectionMethod = $this->createReflectionMethodMock('test', 'SomeClass', $modifiers, [$a, $b], null, null);
372+
373+
$expectedOutput = $t . $t . 'public function test(int &$a, &$b) {}' . $n . $n;
374+
$this->assertSame($expectedOutput, (new MethodFormatter('SomeClass', false, $reflectionMethod))->format());
375+
}
376+
377+
public function testMethodWithVariadicPassedByReferenceParam()
378+
{
379+
$n = PhpStubGenerator::$eol;
380+
$t = PhpStubGenerator::$tab;
381+
382+
$a = $this->createReflectionParameterMock(
383+
'a',
384+
$this->createReflectionTypeMock('int'),
385+
false,
386+
null,
387+
null,
388+
false,
389+
false
390+
);
391+
392+
$b = $this->createReflectionParameterMock(
393+
'b',
394+
null,
395+
false,
396+
null,
397+
null,
398+
true,
399+
true
400+
);
401+
$modifiers = ReflectionMethod::IS_PUBLIC;
402+
$reflectionMethod = $this->createReflectionMethodMock('test', 'SomeClass', $modifiers, [$a, $b], null, null);
403+
404+
$expectedOutput = $t . $t . 'public function test(int $a, &...$b) {}' . $n . $n;
405+
$this->assertSame($expectedOutput, (new MethodFormatter('SomeClass', false, $reflectionMethod))->format());
406+
}
407+
337408
public function testMethodWithDoc()
338409
{
339410
$n = PhpStubGenerator::$eol;

0 commit comments

Comments
 (0)