Skip to content

Commit 7b57b2d

Browse files
nicolas-grekasOcramius
authored andcommitted
Fix "Only variable references should be returned by reference"
1 parent 098e306 commit 7b57b2d

File tree

2 files changed

+47
-13
lines changed

2 files changed

+47
-13
lines changed

src/ProxyManager/ProxyGenerator/Util/PublicScopeSimulator.php

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

5454
if ($valueHolder) {
@@ -67,7 +67,7 @@ public static function getPublicAccessSimulationCode(
6767
. " return;\n"
6868
. '}' . "\n\n"
6969
. '$targetObject = ' . self::getTargetObject($valueHolder) . ";\n"
70-
. '$accessor = function ' . $byRef . '() use ($targetObject, $name' . $value . ') {' . "\n"
70+
. '$accessor = function ' . $byRef . '() use ($targetObject, $' . $nameParameter . $value . ') {' . "\n"
7171
. ' ' . self::getOperation($operationType, $nameParameter, $valueParameter) . "\n"
7272
. "};\n"
7373
. self::getScopeReBind()
@@ -138,7 +138,7 @@ private static function getOperation(string $operationType, string $nameParamete
138138
throw new InvalidArgumentException('Parameter $valueParameter not provided');
139139
}
140140

141-
return 'return $targetObject->$' . $nameParameter . ' = $' . $valueParameter . ';';
141+
return '$targetObject->$' . $nameParameter . ' = $' . $valueParameter . '; return $targetObject->$' . $nameParameter . ';';
142142

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

tests/ProxyManagerTest/ProxyGenerator/Util/PublicScopeSimulatorTest.php

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@
99
use Laminas\Code\Generator\PropertyGenerator;
1010
use PHPUnit\Framework\TestCase;
1111
use ProxyManager\ProxyGenerator\Util\PublicScopeSimulator;
12+
use ProxyManagerTestAsset\ClassWithMixedProperties;
1213
use ReflectionClass;
1314

15+
use function sprintf;
16+
1417
/**
1518
* Tests for {@see \ProxyManager\ProxyGenerator\Util\PublicScopeSimulator}
1619
*
@@ -43,7 +46,7 @@ public function testSimpleGet(): void
4346
}
4447
4548
$targetObject = $realInstanceReflection->newInstanceWithoutConstructor();
46-
$accessor = function & () use ($targetObject, $name) {
49+
$accessor = function & () use ($targetObject, $foo) {
4750
return $targetObject->$foo;
4851
};
4952
$backtrace = debug_backtrace(true, 2);
@@ -72,13 +75,13 @@ public function testSimpleSet(): void
7275
if (! $realInstanceReflection->hasProperty($foo)) {
7376
$targetObject = $this;
7477
75-
return $targetObject->$foo = $baz;
78+
$targetObject->$foo = $baz; return $targetObject->$foo;
7679
return;
7780
}
7881
7982
$targetObject = $realInstanceReflection->newInstanceWithoutConstructor();
80-
$accessor = function & () use ($targetObject, $name, $value) {
81-
return $targetObject->$foo = $baz;
83+
$accessor = function & () use ($targetObject, $foo, $baz) {
84+
$targetObject->$foo = $baz; return $targetObject->$foo;
8285
};
8386
$backtrace = debug_backtrace(true, 2);
8487
$scopeObject = isset($backtrace[1]['object']) ? $backtrace[1]['object'] : new \ProxyManager\Stub\EmptyClassStub();
@@ -111,7 +114,7 @@ public function testSimpleIsset(): void
111114
}
112115
113116
$targetObject = $realInstanceReflection->newInstanceWithoutConstructor();
114-
$accessor = function () use ($targetObject, $name) {
117+
$accessor = function () use ($targetObject, $foo) {
115118
return isset($targetObject->$foo);
116119
};
117120
$backtrace = debug_backtrace(true, 2);
@@ -145,7 +148,7 @@ public function testSimpleUnset(): void
145148
}
146149
147150
$targetObject = $realInstanceReflection->newInstanceWithoutConstructor();
148-
$accessor = function () use ($targetObject, $name) {
151+
$accessor = function () use ($targetObject, $foo) {
149152
unset($targetObject->$foo);
150153
};
151154
$backtrace = debug_backtrace(true, 2);
@@ -187,13 +190,13 @@ public function testDelegatesToValueHolderWhenAvailable(): void
187190
if (! $realInstanceReflection->hasProperty($foo)) {
188191
$targetObject = $this->valueHolder;
189192
190-
return $targetObject->$foo = $baz;
193+
$targetObject->$foo = $baz; return $targetObject->$foo;
191194
return;
192195
}
193196
194197
$targetObject = $this->valueHolder;
195-
$accessor = function & () use ($targetObject, $name, $value) {
196-
return $targetObject->$foo = $baz;
198+
$accessor = function & () use ($targetObject, $foo, $baz) {
199+
$targetObject->$foo = $baz; return $targetObject->$foo;
197200
};
198201
$backtrace = debug_backtrace(true, 2);
199202
$scopeObject = isset($backtrace[1]['object']) ? $backtrace[1]['object'] : new \ProxyManager\Stub\EmptyClassStub();
@@ -244,7 +247,7 @@ public function testWillReturnDirectlyWithNoReturnParam(): void
244247
}
245248
246249
$targetObject = $realInstanceReflection->newInstanceWithoutConstructor();
247-
$accessor = function & () use ($targetObject, $name) {
250+
$accessor = function & () use ($targetObject, $foo) {
248251
return $targetObject->$foo;
249252
};
250253
$backtrace = debug_backtrace(true, 2);
@@ -282,4 +285,35 @@ public function testWillNotAttemptToGetParentClassWhenReflectionClassIsGivenUpfr
282285
)
283286
);
284287
}
288+
289+
/**
290+
* @group #632
291+
* @group #645
292+
* @group #646
293+
*/
294+
public function testFunctional(): void
295+
{
296+
/** @psalm-var ClassWithMixedProperties $sut */
297+
$sut = eval(
298+
sprintf(
299+
'return new class() extends %s
300+
{
301+
public function doGet($prop) { %s }
302+
public function doSet($prop, $val) { %s }
303+
public function doIsset($prop) { %s }
304+
public function doUnset($prop) { %s }
305+
};',
306+
ClassWithMixedProperties::class,
307+
PublicScopeSimulator::getPublicAccessSimulationCode(PublicScopeSimulator::OPERATION_GET, 'prop'),
308+
PublicScopeSimulator::getPublicAccessSimulationCode(PublicScopeSimulator::OPERATION_SET, 'prop', 'val'),
309+
PublicScopeSimulator::getPublicAccessSimulationCode(PublicScopeSimulator::OPERATION_ISSET, 'prop'),
310+
PublicScopeSimulator::getPublicAccessSimulationCode(PublicScopeSimulator::OPERATION_UNSET, 'prop')
311+
)
312+
);
313+
314+
$this->assertSame('publicProperty0', $sut->doGet('publicProperty0'));
315+
$this->assertSame('bar', $sut->doSet('publicProperty0', 'bar'));
316+
$this->assertTrue($sut->doIsset('publicProperty0'));
317+
$this->assertNull($sut->doUnset('publicProperty0'));
318+
}
285319
}

0 commit comments

Comments
 (0)