2021-05-08 21:09:48 +00:00
|
|
|
<?php
|
2021-05-08 22:50:26 +00:00
|
|
|
|
2021-05-08 21:09:48 +00:00
|
|
|
declare(strict_types=1);
|
2021-05-08 22:50:26 +00:00
|
|
|
|
2021-05-08 21:09:48 +00:00
|
|
|
namespace Fbrinker\ExtensionCheck\Parser\Visitors;
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\NodeVisitorAbstract;
|
|
|
|
|
2021-05-08 22:50:26 +00:00
|
|
|
class ClassCollector extends NodeVisitorAbstract implements CollectorInferface
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var array<string,bool> $classes
|
|
|
|
*/
|
2021-05-08 21:09:48 +00:00
|
|
|
private $classes = [];
|
|
|
|
|
2021-05-08 22:50:26 +00:00
|
|
|
public function getCollected(): array
|
|
|
|
{
|
2021-05-08 21:09:48 +00:00
|
|
|
$list = array_keys($this->classes);
|
|
|
|
natcasesort($list);
|
|
|
|
|
|
|
|
return array_values($list);
|
|
|
|
}
|
|
|
|
|
2021-05-08 22:50:26 +00:00
|
|
|
public function enterNode(Node $node)
|
|
|
|
{
|
2021-05-08 21:09:48 +00:00
|
|
|
if ($node instanceof Node\Stmt\Class_) {
|
|
|
|
if (!empty($node->extends)) {
|
|
|
|
$this->classes[$node->extends->toString()] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($node->implements)) {
|
2021-05-08 22:50:26 +00:00
|
|
|
foreach ($node->implements as $implement) {
|
2021-05-08 21:09:48 +00:00
|
|
|
$this->classes[$implement->toString()] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($node instanceof Node\Stmt\UseUse) {
|
|
|
|
if (!empty($node->name) && $node->name instanceof Node\Name) {
|
|
|
|
$this->classes[$node->name->toString()] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($node instanceof Node\Expr\New_) {
|
2021-05-08 22:50:26 +00:00
|
|
|
if (!empty($node->class) && $node->class instanceof Node\Name) {
|
2021-05-08 21:09:48 +00:00
|
|
|
$this->classes[$node->class->toString()] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($node instanceof Node\Expr\ClassConstFetch) {
|
|
|
|
if (!empty($node->class) && $node->class instanceof Node\Name) {
|
|
|
|
$this->classes[$node->class->toString()] = true;
|
|
|
|
}
|
|
|
|
}
|
2021-05-08 22:50:26 +00:00
|
|
|
|
|
|
|
return null;
|
2021-05-08 21:09:48 +00:00
|
|
|
}
|
2021-05-08 22:50:26 +00:00
|
|
|
}
|