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

Promoted constructor properties should not be doubled, fix #784 #785

Open
wants to merge 2 commits into
base: 2.15.x
Choose a base branch
from
Open
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
16 changes: 15 additions & 1 deletion src/ProxyManager/Generator/MethodGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Laminas\Code\Generator\DocBlockGenerator;
use Laminas\Code\Generator\MethodGenerator as LaminasMethodGenerator;
use Laminas\Code\Generator\ParameterGenerator;
use Laminas\Code\Reflection\MethodReflection;

/**
Expand All @@ -19,14 +20,27 @@ class MethodGenerator extends LaminasMethodGenerator
public static function fromReflectionWithoutBodyAndDocBlock(MethodReflection $reflectionMethod): self
{
/** @var static $method */
$method = parent::copyMethodSignature($reflectionMethod);
$method = static::copyMethodSignature($reflectionMethod);

$method->setInterface(false);
$method->setBody('');

return $method;
}

public static function copyMethodSignature(MethodReflection $reflectionMethod): parent
{
$method = parent::copyMethodSignature($reflectionMethod);

foreach ($reflectionMethod->getParameters() as $reflectionParameter) {
$method->setParameter(
ParameterGenerator::fromReflection($reflectionParameter)
);
}

return $method;
Comment on lines +33 to +41
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this behavior be fixed in laminas/laminas-code, if we're required to copy parameter declarations twice to fix it?

Copy link
Author

@Grundik Grundik May 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think laminas-code does its best: it detects property promotion, and copies that as requested. But since we are doing inheritance, we dont need that. So problem on usage side, not on provider one.

Solution for laminas-code would be just to drop support of that feature of PHP, which is not the best, I think.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There might be the another problem in laminas-code, since nullable flag are dropped in these promoted properties. But thats another issue. For doublers there should not be constructor property declarations anyways: they are already declared in base class.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've created another issue for this (separate!) problem: laminas/laminas-code#183

}

/**
* {@inheritDoc} override needed to specify type in more detail
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PHPUnit\Framework\TestCase;
use ProxyManager\ProxyGenerator\ValueHolder\MethodGenerator\Constructor;
use ProxyManagerTestAsset\ClassWithMixedProperties;
use ProxyManagerTestAsset\ClassWithPromotedProperties;
use ProxyManagerTestAsset\ClassWithVariadicConstructorArgument;
use ProxyManagerTestAsset\EmptyClass;
use ProxyManagerTestAsset\ProxyGenerator\LazyLoading\MethodGenerator\ClassWithTwoPublicProperties;
Expand Down Expand Up @@ -133,4 +134,23 @@ public function testBodyStructureWithVariadicArguments(): void

self::assertSame($expectedCode, $constructor->getBody());
}

public function testConstructorPropertyPromotion(): void
{
$valueHolder = $this->createMock(PropertyGenerator::class);

$constructor = Constructor::generateMethod(
new ReflectionClass(ClassWithPromotedProperties::class),
$valueHolder
);

self::assertSame('__construct', $constructor->getName());
$parameters = $constructor->getParameters();
self::assertCount(2, $parameters);

// Promoted constructor properties should not be doubled, since they are inherited anyway
$this->assertSame('int $amount', $parameters['amount']->generate());
$this->assertSame('?int $nullableAmount', $parameters['nullableAmount']->generate());
}

}
29 changes: 29 additions & 0 deletions tests/ProxyManagerTestAsset/ClassWithPromotedProperties.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace ProxyManagerTestAsset;

/**
* Class with a promoted constructor properties
*
* @license MIT
*/
class ClassWithPromotedProperties
{
public function __construct(
protected int $amount,
protected ?int $nullableAmount
) {
}

public function getAmount(): int
{
return $this->amount;
}

public function getNullableAmount(): ?int
{
return $this->nullableAmount;
}
}