Skip to content

Commit 63c73a3

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

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

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

+22
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,25 @@ 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+
$valueHolder->method('getName')->willReturn('foo');
143+
144+
$constructor = Constructor::generateMethod(
145+
new ReflectionClass(ClassWithPromotedProperties::class),
146+
$valueHolder
147+
);
148+
149+
self::assertSame('__construct', $constructor->getName());
150+
$parameters = $constructor->getParameters();
151+
self::assertCount(2, $parameters);
152+
153+
// Promoted constructor properties should not be doubled, since they are inherited anyway
154+
$this->assertSame('int $amount', $parameters['amount']->generate());
155+
$this->assertSame('?int $nullableAmount', $parameters['nullableAmount']->generate());
156+
}
157+
136158
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
/**
15+
* @param int $increment
16+
*/
17+
public function __construct(
18+
protected int $amount,
19+
protected ?int $nullableAmount
20+
) {
21+
}
22+
23+
public function getAmount(): int
24+
{
25+
return $this->amount;
26+
}
27+
28+
public function getNullableAmount(): ?int
29+
{
30+
return $this->nullableAmount;
31+
}
32+
}

0 commit comments

Comments
 (0)