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 #31

Merged
merged 1 commit into from
Jan 30, 2023
Merged
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 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
{
}
}