Skip to content

Commit

Permalink
Fix generating return statements when magic method returns void
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Jan 30, 2023
1 parent 8835461 commit a527c9d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
10 changes: 10 additions & 0 deletions src/ProxyManager/Generator/MagicMethodGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace ProxyManager\Generator;

use Laminas\Code\Generator\MethodGenerator as LaminasMethodGenerator;
use Laminas\Code\Generator\ParameterGenerator;
use LogicException;
use ReflectionClass;
Expand Down Expand Up @@ -55,4 +56,13 @@ public function __construct(ReflectionClass $originalClass, string $name, array
throw new LogicException('Unexpected ' . get_class($type));
}
}

public function setBody($body): LaminasMethodGenerator
{
if ((string) $this->getReturnType() === 'void') {
$body = preg_replace('/return ([^;]++;)/', '\1 return;', $body);
}

return parent::setBody($body);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use ProxyManagerTestAsset\ClassWithFinalMagicMethods;
use ProxyManagerTestAsset\ClassWithFinalMethods;
use ProxyManagerTestAsset\ClassWithMagicMethods;
use ProxyManagerTestAsset\ClassWithTypedMagicMethods;
use ProxyManagerTestAsset\ClassWithMethodWithByRefVariadicFunction;
use ProxyManagerTestAsset\ClassWithMethodWithVariadicFunction;
use ProxyManagerTestAsset\ClassWithMixedProperties;
Expand Down Expand Up @@ -60,7 +61,7 @@ final class MultipleProxyGenerationTest extends TestCase
*/
public function testCanGenerateMultipleDifferentProxiesForSameClass($object): void
{
if (null === $object && \PHP_VERSION_ID < 70400) {
if (null === $object && PHP_VERSION_ID < 70400) {
self::markTestSkipped('PHP 7.4 required.');
}

Expand Down Expand Up @@ -116,8 +117,8 @@ public function getTestedClasses(): array
[new ClassWithFinalMagicMethods()],
[new ClassWithByRefMagicMethods()],
[new ClassWithMixedProperties()],
[\PHP_VERSION_ID >= 70400 ? new ClassWithMixedTypedProperties() : null],
[\PHP_VERSION_ID >= 70400 ? new ClassWithMixedReferenceableTypedProperties() : null],
[PHP_VERSION_ID >= 70400 ? new ClassWithMixedTypedProperties() : null],
[PHP_VERSION_ID >= 70400 ? new ClassWithMixedReferenceableTypedProperties() : null],
// [new ClassWithPublicStringTypedProperty()],
// [new ClassWithPublicStringNullableTypedProperty()],
[new ClassWithPrivateProperties()],
Expand All @@ -139,6 +140,7 @@ public function getTestedClasses(): array

if (PHP_VERSION_ID >= 80000) {
$objects[] = [new ClassWithPhp80TypedMethods()];
$objects[] = [new ClassWithTypedMagicMethods()];
}

if (PHP_VERSION_ID >= 80100) {
Expand Down
49 changes: 49 additions & 0 deletions tests/ProxyManagerTestAsset/ClassWithTypedMagicMethods.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace ProxyManagerTestAsset;

/**
* Base test class to play around with pre-existing typed magic methods
*
* @author Marco Pivetta <[email protected]>
* @license MIT
*/
class ClassWithTypedMagicMethods
{
public array $data = [];

public function __set(string|int $name, mixed $value): void
{
$this->data[$name] = $value;
}

public function __get(string|int $name): mixed
{
return $this->data[$name] ?? null;
}

public function __isset(string|int $name): bool
{
return isset($this->data[$name]);
}

public function __unset(string|int $name): void
{
unset($this->data[$name]);
}

public function __sleep(): array
{
return ['data'];
}

public function __wakeup(): void
{
}

public function __clone(): void
{
}
}

0 comments on commit a527c9d

Please sign in to comment.