Skip to content

Commit

Permalink
Promoted constructor properties should not be doubled
Browse files Browse the repository at this point in the history
  • Loading branch information
Grundik committed May 16, 2023
1 parent 17ae929 commit aa6edcb
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
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;
}
}

0 comments on commit aa6edcb

Please sign in to comment.