Skip to content

Commit 4dd3f75

Browse files
committed
Allow promoted properties in trait __construct even when renamed
1 parent 8f8c1af commit 4dd3f75

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

src/Rules/Classes/InvalidPromotedPropertiesRule.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ public function processNode(Node $node, Scope $scope): array
6161

6262
if (
6363
!$node instanceof Node\Stmt\ClassMethod
64-
|| $node->name->toLowerString() !== '__construct'
64+
|| (
65+
$node->name->toLowerString() !== '__construct'
66+
&& $node->getAttribute('originalTraitMethodName') !== '__construct')
6567
) {
6668
return [
6769
RuleErrorBuilder::message(

tests/PHPStan/Rules/Classes/InvalidPromotedPropertiesRuleTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PHPStan\Php\PhpVersion;
66
use PHPStan\Rules\Rule;
77
use PHPStan\Testing\RuleTestCase;
8+
use const PHP_VERSION_ID;
89

910
/**
1011
* @extends RuleTestCase<InvalidPromotedPropertiesRule>
@@ -93,4 +94,14 @@ public function testSupportedOnPhp8(): void
9394
]);
9495
}
9596

97+
public function testBug9577(): void
98+
{
99+
if (PHP_VERSION_ID < 80100) {
100+
$this->markTestSkipped('Test requires PHP 8.1.');
101+
}
102+
103+
$this->phpVersion = 80100;
104+
$this->analyse([__DIR__ . '/data/bug-9577.php'], []);
105+
}
106+
96107
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php // lint >= 8.1
2+
3+
namespace Bug9577;
4+
5+
trait StringableMessageTrait
6+
{
7+
public function __construct(
8+
public readonly string $message,
9+
) {
10+
11+
}
12+
}
13+
14+
class SpecializedException
15+
{
16+
use StringableMessageTrait {
17+
StringableMessageTrait::__construct as __traitConstruct;
18+
}
19+
20+
public function __construct(
21+
public int $code,
22+
string $message,
23+
) {
24+
$this->__traitConstruct($message);
25+
}
26+
}

0 commit comments

Comments
 (0)