Skip to content

Commit a527c9d

Browse files
committedJan 30, 2023
Fix generating return statements when magic method returns void
1 parent 8835461 commit a527c9d

File tree

3 files changed

+64
-3
lines changed

3 files changed

+64
-3
lines changed
 

‎src/ProxyManager/Generator/MagicMethodGenerator.php

+10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace ProxyManager\Generator;
66

7+
use Laminas\Code\Generator\MethodGenerator as LaminasMethodGenerator;
78
use Laminas\Code\Generator\ParameterGenerator;
89
use LogicException;
910
use ReflectionClass;
@@ -55,4 +56,13 @@ public function __construct(ReflectionClass $originalClass, string $name, array
5556
throw new LogicException('Unexpected ' . get_class($type));
5657
}
5758
}
59+
60+
public function setBody($body): LaminasMethodGenerator
61+
{
62+
if ((string) $this->getReturnType() === 'void') {
63+
$body = preg_replace('/return ([^;]++;)/', '\1 return;', $body);
64+
}
65+
66+
return parent::setBody($body);
67+
}
5868
}

‎tests/ProxyManagerTest/Functional/MultipleProxyGenerationTest.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use ProxyManagerTestAsset\ClassWithFinalMagicMethods;
1616
use ProxyManagerTestAsset\ClassWithFinalMethods;
1717
use ProxyManagerTestAsset\ClassWithMagicMethods;
18+
use ProxyManagerTestAsset\ClassWithTypedMagicMethods;
1819
use ProxyManagerTestAsset\ClassWithMethodWithByRefVariadicFunction;
1920
use ProxyManagerTestAsset\ClassWithMethodWithVariadicFunction;
2021
use ProxyManagerTestAsset\ClassWithMixedProperties;
@@ -60,7 +61,7 @@ final class MultipleProxyGenerationTest extends TestCase
6061
*/
6162
public function testCanGenerateMultipleDifferentProxiesForSameClass($object): void
6263
{
63-
if (null === $object && \PHP_VERSION_ID < 70400) {
64+
if (null === $object && PHP_VERSION_ID < 70400) {
6465
self::markTestSkipped('PHP 7.4 required.');
6566
}
6667

@@ -116,8 +117,8 @@ public function getTestedClasses(): array
116117
[new ClassWithFinalMagicMethods()],
117118
[new ClassWithByRefMagicMethods()],
118119
[new ClassWithMixedProperties()],
119-
[\PHP_VERSION_ID >= 70400 ? new ClassWithMixedTypedProperties() : null],
120-
[\PHP_VERSION_ID >= 70400 ? new ClassWithMixedReferenceableTypedProperties() : null],
120+
[PHP_VERSION_ID >= 70400 ? new ClassWithMixedTypedProperties() : null],
121+
[PHP_VERSION_ID >= 70400 ? new ClassWithMixedReferenceableTypedProperties() : null],
121122
// [new ClassWithPublicStringTypedProperty()],
122123
// [new ClassWithPublicStringNullableTypedProperty()],
123124
[new ClassWithPrivateProperties()],
@@ -139,6 +140,7 @@ public function getTestedClasses(): array
139140

140141
if (PHP_VERSION_ID >= 80000) {
141142
$objects[] = [new ClassWithPhp80TypedMethods()];
143+
$objects[] = [new ClassWithTypedMagicMethods()];
142144
}
143145

144146
if (PHP_VERSION_ID >= 80100) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ProxyManagerTestAsset;
6+
7+
/**
8+
* Base test class to play around with pre-existing typed magic methods
9+
*
10+
* @author Marco Pivetta <ocramius@gmail.com>
11+
* @license MIT
12+
*/
13+
class ClassWithTypedMagicMethods
14+
{
15+
public array $data = [];
16+
17+
public function __set(string|int $name, mixed $value): void
18+
{
19+
$this->data[$name] = $value;
20+
}
21+
22+
public function __get(string|int $name): mixed
23+
{
24+
return $this->data[$name] ?? null;
25+
}
26+
27+
public function __isset(string|int $name): bool
28+
{
29+
return isset($this->data[$name]);
30+
}
31+
32+
public function __unset(string|int $name): void
33+
{
34+
unset($this->data[$name]);
35+
}
36+
37+
public function __sleep(): array
38+
{
39+
return ['data'];
40+
}
41+
42+
public function __wakeup(): void
43+
{
44+
}
45+
46+
public function __clone(): void
47+
{
48+
}
49+
}

0 commit comments

Comments
 (0)
Please sign in to comment.