Skip to content

Commit

Permalink
Introduce InTraitNode
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jul 13, 2023
1 parent e9ef210 commit 657fc54
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
use PHPStan\Node\InForeachNode;
use PHPStan\Node\InFunctionNode;
use PHPStan\Node\InstantiationCallableNode;
use PHPStan\Node\InTraitNode;
use PHPStan\Node\LiteralArrayItem;
use PHPStan\Node\LiteralArrayNode;
use PHPStan\Node\MatchExpressionArm;
Expand Down Expand Up @@ -4362,7 +4363,10 @@ private function processNodesForTraitUse($node, ClassReflection $traitReflection
$methodAst->setAttribute('originalTraitMethodName', $methodAst->name->toLowerString());
$methodAst->name = $methodNames[$methodName];
}
$this->processStmtNodes($node, $stmts, $scope->enterTrait($traitReflection), $nodeCallback, StatementContext::createTopLevel());

$traitScope = $scope->enterTrait($traitReflection);
$nodeCallback(new InTraitNode($node, $traitReflection), $traitScope);
$this->processStmtNodes($node, $stmts, $traitScope, $nodeCallback, StatementContext::createTopLevel());
return;
}
if ($node instanceof Node\Stmt\ClassLike) {
Expand Down
40 changes: 40 additions & 0 deletions src/Node/InTraitNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php declare(strict_types = 1);

namespace PHPStan\Node;

use PhpParser\Node;
use PHPStan\Reflection\ClassReflection;

/** @api */
class InTraitNode extends Node\Stmt implements VirtualNode
{

public function __construct(private Node\Stmt\Trait_ $originalNode, private ClassReflection $traitReflection)
{
parent::__construct($originalNode->getAttributes());
}

public function getOriginalNode(): Node\Stmt\Trait_
{
return $this->originalNode;
}

public function getTraitReflection(): ClassReflection
{
return $this->traitReflection;
}

public function getType(): string
{
return 'PHPStan_Stmt_InTraitNode';
}

/**
* @return string[]
*/
public function getSubNodeNames(): array
{
return [];
}

}

2 comments on commit 657fc54

@staabm
Copy link
Contributor

@staabm staabm commented on 657fc54 Jul 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this addition mean, we can now build rules to analyze traits without analyzing them in context of their used class?

@ondrejmirtes
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No :)

Please sign in to comment.