Skip to content

Commit c6035f3

Browse files
committed
Fix processing traits with renamed methods
1 parent fae4e23 commit c6035f3

File tree

3 files changed

+54
-7
lines changed

3 files changed

+54
-7
lines changed

phpstan-baseline.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,7 @@ parameters:
848848

849849
-
850850
message: "#^Doing instanceof PHPStan\\\\Type\\\\ConstantScalarType is error\\-prone and deprecated\\. Use Type\\:\\:isConstantScalarValue\\(\\) or Type\\:\\:getConstantScalarTypes\\(\\) or Type\\:\\:getConstantScalarValues\\(\\) instead\\.$#"
851-
count: 2
851+
count: 4
852852
path: src/Type/Constant/ConstantBooleanType.php
853853

854854
-

src/Analyser/NodeScopeResolver.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4293,16 +4293,22 @@ private function processNodesForTraitUse($node, ClassReflection $traitReflection
42934293
if ($node instanceof Node) {
42944294
if ($node instanceof Node\Stmt\Trait_ && $traitReflection->getName() === (string) $node->namespacedName && $traitReflection->getNativeReflection()->getStartLine() === $node->getStartLine()) {
42954295
$methodModifiers = [];
4296+
$methodNames = [];
42964297
foreach ($adaptations as $adaptation) {
42974298
if (!$adaptation instanceof Node\Stmt\TraitUseAdaptation\Alias) {
42984299
continue;
42994300
}
43004301

4301-
if ($adaptation->newModifier === null) {
4302+
$methodName = $adaptation->method->toLowerString();
4303+
if ($adaptation->newModifier !== null) {
4304+
$methodModifiers[$methodName] = $adaptation->newModifier;
4305+
}
4306+
4307+
if ($adaptation->newName === null) {
43024308
continue;
43034309
}
43044310

4305-
$methodModifiers[$adaptation->method->toLowerString()] = $adaptation->newModifier;
4311+
$methodNames[$methodName] = $adaptation->newName;
43064312
}
43074313

43084314
$stmts = $node->stmts;
@@ -4311,13 +4317,17 @@ private function processNodesForTraitUse($node, ClassReflection $traitReflection
43114317
continue;
43124318
}
43134319
$methodName = $stmt->name->toLowerString();
4314-
if (!array_key_exists($methodName, $methodModifiers)) {
4320+
$methodAst = clone $stmt;
4321+
$stmts[$i] = $methodAst;
4322+
if (array_key_exists($methodName, $methodModifiers)) {
4323+
$methodAst->flags = ($methodAst->flags & ~ Node\Stmt\Class_::VISIBILITY_MODIFIER_MASK) | $methodModifiers[$methodName];
4324+
}
4325+
4326+
if (!array_key_exists($methodName, $methodNames)) {
43154327
continue;
43164328
}
43174329

4318-
$methodAst = clone $stmt;
4319-
$methodAst->flags = ($methodAst->flags & ~ Node\Stmt\Class_::VISIBILITY_MODIFIER_MASK) | $methodModifiers[$methodName];
4320-
$stmts[$i] = $methodAst;
4330+
$methodAst->name = $methodNames[$methodName];
43214331
}
43224332
$this->processStmtNodes($node, $stmts, $scope->enterTrait($traitReflection), $nodeCallback, StatementContext::createTopLevel());
43234333
return;

tests/PHPStan/Rules/Properties/data/bug-7198.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,40 @@ public function foo(): void
4949
$this->callee->foo();
5050
}
5151
}
52+
53+
trait Identifiable
54+
{
55+
public readonly int $id;
56+
57+
public function __construct()
58+
{
59+
$this->id = rand();
60+
}
61+
}
62+
63+
trait CreateAware
64+
{
65+
public readonly \DateTimeImmutable $createdAt;
66+
67+
public function __construct()
68+
{
69+
$this->createdAt = new \DateTimeImmutable();
70+
}
71+
}
72+
73+
abstract class Entity
74+
{
75+
use Identifiable {
76+
Identifiable::__construct as private __identifiableConstruct;
77+
}
78+
79+
use CreateAware {
80+
CreateAware::__construct as private __createAwareConstruct;
81+
}
82+
83+
public function __construct()
84+
{
85+
$this->__identifiableConstruct();
86+
$this->__createAwareConstruct();
87+
}
88+
}

0 commit comments

Comments
 (0)