2020-03-09 21:41:40 +01:00
|
|
|
<?php
|
2020-03-15 04:54:42 +01:00
|
|
|
namespace Psalm\Internal\PhpVisitor;
|
2020-03-09 21:41:40 +01:00
|
|
|
|
|
|
|
use PhpParser;
|
2021-12-03 21:40:18 +01:00
|
|
|
use ReflectionClass;
|
|
|
|
use Throwable;
|
2021-06-08 04:55:21 +02:00
|
|
|
|
2020-03-09 21:41:40 +01:00
|
|
|
use function count;
|
2021-12-03 21:07:25 +01:00
|
|
|
use function end;
|
|
|
|
use function explode;
|
|
|
|
use function trait_exists;
|
2020-03-09 21:41:40 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a list of file diffs, this scans an AST to find the sections it can replace, and parses
|
|
|
|
* just those methods.
|
|
|
|
*/
|
2020-10-15 19:23:35 +02:00
|
|
|
class TraitFinder extends PhpParser\NodeVisitorAbstract
|
2020-03-09 21:41:40 +01:00
|
|
|
{
|
|
|
|
/** @var list<PhpParser\Node\Stmt\Trait_> */
|
|
|
|
private $matching_trait_nodes = [];
|
|
|
|
|
|
|
|
private $fq_trait_name;
|
|
|
|
|
|
|
|
public function __construct(string $fq_trait_name)
|
|
|
|
{
|
|
|
|
$this->fq_trait_name = $fq_trait_name;
|
|
|
|
}
|
|
|
|
|
2021-10-04 00:03:06 +02:00
|
|
|
public function enterNode(PhpParser\Node $node, bool &$traverseChildren = true): ?int
|
2020-03-09 21:41:40 +01:00
|
|
|
{
|
2020-03-09 21:59:21 +01:00
|
|
|
if ($node instanceof PhpParser\Node\Stmt\Trait_) {
|
|
|
|
/** @var ?string */
|
|
|
|
$resolved_name = $node->getAttribute('resolvedName');
|
|
|
|
|
|
|
|
if ($resolved_name === null) {
|
|
|
|
// compare ends of names, a temporary hack because PHPParser caches
|
|
|
|
// may not have that attribute
|
|
|
|
|
2021-12-03 21:07:25 +01:00
|
|
|
$fq_trait_name_parts = explode('\\', $this->fq_trait_name);
|
2020-03-09 21:59:21 +01:00
|
|
|
|
|
|
|
/** @psalm-suppress PossiblyNullPropertyFetch */
|
2021-12-03 21:07:25 +01:00
|
|
|
if ($node->name->name === end($fq_trait_name_parts)) {
|
2020-03-09 21:59:21 +01:00
|
|
|
$this->matching_trait_nodes[] = $node;
|
|
|
|
}
|
|
|
|
} elseif ($resolved_name === $this->fq_trait_name) {
|
|
|
|
$this->matching_trait_nodes[] = $node;
|
|
|
|
}
|
2020-03-09 21:41:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($node instanceof PhpParser\Node\Stmt\ClassLike
|
|
|
|
|| $node instanceof PhpParser\Node\FunctionLike
|
|
|
|
) {
|
|
|
|
return PhpParser\NodeTraverser::DONT_TRAVERSE_CHILDREN;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-12-05 18:51:26 +01:00
|
|
|
public function getNode(): ?PhpParser\Node\Stmt\Trait_
|
2020-03-09 21:41:40 +01:00
|
|
|
{
|
|
|
|
if (!count($this->matching_trait_nodes)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-12-03 21:07:25 +01:00
|
|
|
if (count($this->matching_trait_nodes) === 1 || !trait_exists($this->fq_trait_name)) {
|
2020-03-09 21:41:40 +01:00
|
|
|
return $this->matching_trait_nodes[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2021-12-03 21:40:18 +01:00
|
|
|
$reflection_trait = new ReflectionClass($this->fq_trait_name);
|
|
|
|
} catch (Throwable $t) {
|
2020-03-09 21:41:40 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this->matching_trait_nodes as $node) {
|
|
|
|
if ($node->getLine() === $reflection_trait->getStartLine()) {
|
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|