Skip to content

Commit

Permalink
Allow promoted properties in trait __construct even when renamed
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jul 5, 2023
1 parent 8f8c1af commit 4dd3f75
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Rules/Classes/InvalidPromotedPropertiesRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ public function processNode(Node $node, Scope $scope): array

if (
!$node instanceof Node\Stmt\ClassMethod
|| $node->name->toLowerString() !== '__construct'
|| (
$node->name->toLowerString() !== '__construct'
&& $node->getAttribute('originalTraitMethodName') !== '__construct')
) {
return [
RuleErrorBuilder::message(
Expand Down
11 changes: 11 additions & 0 deletions tests/PHPStan/Rules/Classes/InvalidPromotedPropertiesRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PHPStan\Php\PhpVersion;
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use const PHP_VERSION_ID;

/**
* @extends RuleTestCase<InvalidPromotedPropertiesRule>
Expand Down Expand Up @@ -93,4 +94,14 @@ public function testSupportedOnPhp8(): void
]);
}

public function testBug9577(): void
{
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped('Test requires PHP 8.1.');
}

$this->phpVersion = 80100;
$this->analyse([__DIR__ . '/data/bug-9577.php'], []);
}

}
26 changes: 26 additions & 0 deletions tests/PHPStan/Rules/Classes/data/bug-9577.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php // lint >= 8.1

namespace Bug9577;

trait StringableMessageTrait
{
public function __construct(
public readonly string $message,
) {

}
}

class SpecializedException
{
use StringableMessageTrait {
StringableMessageTrait::__construct as __traitConstruct;
}

public function __construct(
public int $code,
string $message,
) {
$this->__traitConstruct($message);
}
}

0 comments on commit 4dd3f75

Please sign in to comment.