Skip to content

Commit aa6edcb

Browse files
committed
Promoted constructor properties should not be doubled
1 parent 17ae929 commit aa6edcb

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

tests/ProxyManagerTest/ProxyGenerator/ValueHolder/MethodGenerator/ConstructorTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PHPUnit\Framework\TestCase;
99
use ProxyManager\ProxyGenerator\ValueHolder\MethodGenerator\Constructor;
1010
use ProxyManagerTestAsset\ClassWithMixedProperties;
11+
use ProxyManagerTestAsset\ClassWithPromotedProperties;
1112
use ProxyManagerTestAsset\ClassWithVariadicConstructorArgument;
1213
use ProxyManagerTestAsset\EmptyClass;
1314
use ProxyManagerTestAsset\ProxyGenerator\LazyLoading\MethodGenerator\ClassWithTwoPublicProperties;
@@ -133,4 +134,23 @@ public function testBodyStructureWithVariadicArguments(): void
133134

134135
self::assertSame($expectedCode, $constructor->getBody());
135136
}
137+
138+
public function testConstructorPropertyPromotion(): void
139+
{
140+
$valueHolder = $this->createMock(PropertyGenerator::class);
141+
142+
$constructor = Constructor::generateMethod(
143+
new ReflectionClass(ClassWithPromotedProperties::class),
144+
$valueHolder
145+
);
146+
147+
self::assertSame('__construct', $constructor->getName());
148+
$parameters = $constructor->getParameters();
149+
self::assertCount(2, $parameters);
150+
151+
// Promoted constructor properties should not be doubled, since they are inherited anyway
152+
$this->assertSame('int $amount', $parameters['amount']->generate());
153+
$this->assertSame('?int $nullableAmount', $parameters['nullableAmount']->generate());
154+
}
155+
136156
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ProxyManagerTestAsset;
6+
7+
/**
8+
* Class with a promoted constructor properties
9+
*
10+
* @license MIT
11+
*/
12+
class ClassWithPromotedProperties
13+
{
14+
public function __construct(
15+
protected int $amount,
16+
protected ?int $nullableAmount
17+
) {
18+
}
19+
20+
public function getAmount(): int
21+
{
22+
return $this->amount;
23+
}
24+
25+
public function getNullableAmount(): ?int
26+
{
27+
return $this->nullableAmount;
28+
}
29+
}

0 commit comments

Comments
 (0)