Skip to content

Commit

Permalink
Fix processing traits with renamed methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jul 3, 2023
1 parent fae4e23 commit c6035f3
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 7 deletions.
2 changes: 1 addition & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,7 @@ parameters:

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

-
Expand Down
22 changes: 16 additions & 6 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4293,16 +4293,22 @@ private function processNodesForTraitUse($node, ClassReflection $traitReflection
if ($node instanceof Node) {
if ($node instanceof Node\Stmt\Trait_ && $traitReflection->getName() === (string) $node->namespacedName && $traitReflection->getNativeReflection()->getStartLine() === $node->getStartLine()) {
$methodModifiers = [];
$methodNames = [];
foreach ($adaptations as $adaptation) {
if (!$adaptation instanceof Node\Stmt\TraitUseAdaptation\Alias) {
continue;
}

if ($adaptation->newModifier === null) {
$methodName = $adaptation->method->toLowerString();
if ($adaptation->newModifier !== null) {
$methodModifiers[$methodName] = $adaptation->newModifier;
}

if ($adaptation->newName === null) {
continue;
}

$methodModifiers[$adaptation->method->toLowerString()] = $adaptation->newModifier;
$methodNames[$methodName] = $adaptation->newName;
}

$stmts = $node->stmts;
Expand All @@ -4311,13 +4317,17 @@ private function processNodesForTraitUse($node, ClassReflection $traitReflection
continue;
}
$methodName = $stmt->name->toLowerString();
if (!array_key_exists($methodName, $methodModifiers)) {
$methodAst = clone $stmt;
$stmts[$i] = $methodAst;
if (array_key_exists($methodName, $methodModifiers)) {
$methodAst->flags = ($methodAst->flags & ~ Node\Stmt\Class_::VISIBILITY_MODIFIER_MASK) | $methodModifiers[$methodName];
}

if (!array_key_exists($methodName, $methodNames)) {
continue;
}

$methodAst = clone $stmt;
$methodAst->flags = ($methodAst->flags & ~ Node\Stmt\Class_::VISIBILITY_MODIFIER_MASK) | $methodModifiers[$methodName];
$stmts[$i] = $methodAst;
$methodAst->name = $methodNames[$methodName];
}
$this->processStmtNodes($node, $stmts, $scope->enterTrait($traitReflection), $nodeCallback, StatementContext::createTopLevel());
return;
Expand Down
37 changes: 37 additions & 0 deletions tests/PHPStan/Rules/Properties/data/bug-7198.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,40 @@ public function foo(): void
$this->callee->foo();
}
}

trait Identifiable
{
public readonly int $id;

public function __construct()
{
$this->id = rand();
}
}

trait CreateAware
{
public readonly \DateTimeImmutable $createdAt;

public function __construct()
{
$this->createdAt = new \DateTimeImmutable();
}
}

abstract class Entity
{
use Identifiable {
Identifiable::__construct as private __identifiableConstruct;
}

use CreateAware {
CreateAware::__construct as private __createAwareConstruct;
}

public function __construct()
{
$this->__identifiableConstruct();
$this->__createAwareConstruct();
}
}

0 comments on commit c6035f3

Please sign in to comment.