Skip to content

Commit 1022514

Browse files
Fix "Only variable references should be returned by reference"
1 parent 0db5a0f commit 1022514

File tree

2 files changed

+42
-13
lines changed

2 files changed

+42
-13
lines changed

src/ProxyManager/ProxyGenerator/Util/PublicScopeSimulator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public static function getPublicAccessSimulationCode(
4949
?string $interfaceName = null
5050
): string {
5151
$byRef = self::getByRefReturnValue($operationType);
52-
$value = $operationType === self::OPERATION_SET ? ', $value' : '';
52+
$value = $operationType === self::OPERATION_SET ? ', $' . ($valueParameter ?? 'value') : '';
5353
$target = '$this';
5454

5555
if ($valueHolder) {
@@ -70,7 +70,7 @@ public static function getPublicAccessSimulationCode(
7070
. " return;\n"
7171
. '}' . "\n\n"
7272
. '$targetObject = ' . self::getTargetObject($valueHolder) . ";\n"
73-
. '$accessor = function ' . $byRef . '() use ($targetObject, $name' . $value . ') {' . "\n"
73+
. '$accessor = function ' . $byRef . '() use ($targetObject, $' . $nameParameter . $value . ') {' . "\n"
7474
. ' ' . self::getOperation($operationType, $nameParameter, $valueParameter) . "\n"
7575
. "};\n"
7676
. self::getScopeReBind()
@@ -147,7 +147,7 @@ private static function getOperation(string $operationType, string $nameParamete
147147
throw new InvalidArgumentException('Parameter $valueParameter not provided');
148148
}
149149

150-
return 'return $targetObject->$' . $nameParameter . ' = $' . $valueParameter . ';';
150+
return '$targetObject->$' . $nameParameter . ' = $' . $valueParameter . '; return $targetObject->$' . $nameParameter . ';';
151151

152152
case self::OPERATION_ISSET:
153153
return 'return isset($targetObject->$' . $nameParameter . ');';

tests/ProxyManagerTest/ProxyGenerator/Util/PublicScopeSimulatorTest.php

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
use Laminas\Code\Generator\PropertyGenerator;
99
use PHPUnit\Framework\TestCase;
1010
use ProxyManager\ProxyGenerator\Util\PublicScopeSimulator;
11+
use ProxyManagerTestAsset\ClassWithMixedProperties;
12+
13+
use function sprintf;
1114

1215
/**
1316
* Tests for {@see \ProxyManager\ProxyGenerator\Util\PublicScopeSimulator}
@@ -41,7 +44,7 @@ public function testSimpleGet(): void
4144
}
4245
4346
$targetObject = $realInstanceReflection->newInstanceWithoutConstructor();
44-
$accessor = function & () use ($targetObject, $name) {
47+
$accessor = function & () use ($targetObject, $foo) {
4548
return $targetObject->$foo;
4649
};
4750
$backtrace = debug_backtrace(true, 2);
@@ -70,13 +73,13 @@ public function testSimpleSet(): void
7073
if (! $realInstanceReflection->hasProperty($foo)) {
7174
$targetObject = $this;
7275
73-
return $targetObject->$foo = $baz;
76+
$targetObject->$foo = $baz; return $targetObject->$foo;
7477
return;
7578
}
7679
7780
$targetObject = $realInstanceReflection->newInstanceWithoutConstructor();
78-
$accessor = function & () use ($targetObject, $name, $value) {
79-
return $targetObject->$foo = $baz;
81+
$accessor = function & () use ($targetObject, $foo, $baz) {
82+
$targetObject->$foo = $baz; return $targetObject->$foo;
8083
};
8184
$backtrace = debug_backtrace(true, 2);
8285
$scopeObject = isset($backtrace[1]['object']) ? $backtrace[1]['object'] : new \ProxyManager\Stub\EmptyClassStub();
@@ -109,7 +112,7 @@ public function testSimpleIsset(): void
109112
}
110113
111114
$targetObject = $realInstanceReflection->newInstanceWithoutConstructor();
112-
$accessor = function () use ($targetObject, $name) {
115+
$accessor = function () use ($targetObject, $foo) {
113116
return isset($targetObject->$foo);
114117
};
115118
$backtrace = debug_backtrace(true, 2);
@@ -143,7 +146,7 @@ public function testSimpleUnset(): void
143146
}
144147
145148
$targetObject = $realInstanceReflection->newInstanceWithoutConstructor();
146-
$accessor = function () use ($targetObject, $name) {
149+
$accessor = function () use ($targetObject, $foo) {
147150
unset($targetObject->$foo);
148151
};
149152
$backtrace = debug_backtrace(true, 2);
@@ -185,13 +188,13 @@ public function testDelegatesToValueHolderWhenAvailable(): void
185188
if (! $realInstanceReflection->hasProperty($foo)) {
186189
$targetObject = $this->valueHolder;
187190
188-
return $targetObject->$foo = $baz;
191+
$targetObject->$foo = $baz; return $targetObject->$foo;
189192
return;
190193
}
191194
192195
$targetObject = $this->valueHolder;
193-
$accessor = function & () use ($targetObject, $name, $value) {
194-
return $targetObject->$foo = $baz;
196+
$accessor = function & () use ($targetObject, $foo, $baz) {
197+
$targetObject->$foo = $baz; return $targetObject->$foo;
195198
};
196199
$backtrace = debug_backtrace(true, 2);
197200
$scopeObject = isset($backtrace[1]['object']) ? $backtrace[1]['object'] : new \ProxyManager\Stub\EmptyClassStub();
@@ -242,7 +245,7 @@ public function testWillReturnDirectlyWithNoReturnParam(): void
242245
}
243246
244247
$targetObject = $realInstanceReflection->newInstanceWithoutConstructor();
245-
$accessor = function & () use ($targetObject, $name) {
248+
$accessor = function & () use ($targetObject, $foo) {
246249
return $targetObject->$foo;
247250
};
248251
$backtrace = debug_backtrace(true, 2);
@@ -261,4 +264,30 @@ public function testWillReturnDirectlyWithNoReturnParam(): void
261264
)
262265
);
263266
}
267+
268+
public function testFunctional(): void
269+
{
270+
/** @psalm-var ClassWithMixedProperties $sut */
271+
$sut = eval(
272+
sprintf(
273+
'return new class() extends %s
274+
{
275+
public function doGet($prop) { %s }
276+
public function doSet($prop, $val) { %s }
277+
public function doIsset($prop) { %s }
278+
public function doUnset($prop) { %s }
279+
};',
280+
ClassWithMixedProperties::class,
281+
PublicScopeSimulator::getPublicAccessSimulationCode(PublicScopeSimulator::OPERATION_GET, 'prop'),
282+
PublicScopeSimulator::getPublicAccessSimulationCode(PublicScopeSimulator::OPERATION_SET, 'prop', 'val'),
283+
PublicScopeSimulator::getPublicAccessSimulationCode(PublicScopeSimulator::OPERATION_ISSET, 'prop'),
284+
PublicScopeSimulator::getPublicAccessSimulationCode(PublicScopeSimulator::OPERATION_UNSET, 'prop')
285+
)
286+
);
287+
288+
$this->assertSame('publicProperty0', $sut->doGet('publicProperty0'));
289+
$this->assertSame('bar', $sut->doSet('publicProperty0', 'bar'));
290+
$this->assertTrue($sut->doIsset('publicProperty0'));
291+
$this->assertNull($sut->doUnset('publicProperty0'));
292+
}
264293
}

0 commit comments

Comments
 (0)