Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix generating return statements when magic method returns void #779

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 ReflectionClass;

Expand Down Expand Up @@ -33,4 +34,13 @@ public function __construct(ReflectionClass $originalClass, string $name, array

$this->setReturnsReference($originalClass->getMethod($name)->returnsReference());
}

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 @@ -128,6 +129,7 @@ public function getTestedClasses(): array

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

return $objects;
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
{
}
}