2021-04-22 00:02:38 +02:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2021-04-22 00:02:38 +02:00
|
|
|
namespace Rector\DowngradePhp72\NodeAnalyzer;
|
|
|
|
|
|
|
|
use PhpParser\Node\Stmt\Class_;
|
2021-04-23 11:03:45 +02:00
|
|
|
use PhpParser\Node\Stmt\ClassLike;
|
2021-04-22 00:02:38 +02:00
|
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
2021-04-24 01:14:40 +02:00
|
|
|
use PhpParser\Node\Stmt\Interface_;
|
2021-04-22 00:02:38 +02:00
|
|
|
use PHPStan\Analyser\Scope;
|
|
|
|
use PHPStan\Reflection\ClassReflection;
|
|
|
|
use Rector\NodeCollector\NodeCollector\NodeRepository;
|
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
|
|
|
final class ClassLikeWithTraitsClassMethodResolver
|
|
|
|
{
|
|
|
|
/**
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\NodeCollector\NodeCollector\NodeRepository
|
2021-04-22 00:02:38 +02:00
|
|
|
*/
|
|
|
|
private $nodeRepository;
|
2021-05-10 22:23:08 +00:00
|
|
|
public function __construct(\Rector\NodeCollector\NodeCollector\NodeRepository $nodeRepository)
|
2021-04-22 00:02:38 +02:00
|
|
|
{
|
|
|
|
$this->nodeRepository = $nodeRepository;
|
|
|
|
}
|
|
|
|
/**
|
2021-04-24 01:14:40 +02:00
|
|
|
* @param Class_|Interface_ $classLike
|
2021-04-22 00:02:38 +02:00
|
|
|
* @return ClassMethod[]
|
|
|
|
*/
|
2021-05-10 22:23:08 +00:00
|
|
|
public function resolve(\PhpParser\Node\Stmt\ClassLike $classLike) : array
|
2021-04-22 00:02:38 +02:00
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
$scope = $classLike->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::SCOPE);
|
|
|
|
if (!$scope instanceof \PHPStan\Analyser\Scope) {
|
2021-04-23 11:03:45 +02:00
|
|
|
return [];
|
2021-04-22 00:02:38 +02:00
|
|
|
}
|
|
|
|
$classReflection = $scope->getClassReflection();
|
2021-05-10 22:23:08 +00:00
|
|
|
if (!$classReflection instanceof \PHPStan\Reflection\ClassReflection) {
|
2021-04-23 11:03:45 +02:00
|
|
|
return [];
|
2021-04-22 00:02:38 +02:00
|
|
|
}
|
2021-04-23 11:03:45 +02:00
|
|
|
$classMethods = [];
|
|
|
|
foreach ($classReflection->getAncestors() as $ancestorClassReflection) {
|
2021-04-24 01:14:40 +02:00
|
|
|
$ancestorClassLike = $this->nodeRepository->findClassLike($ancestorClassReflection->getName());
|
2021-05-10 22:23:08 +00:00
|
|
|
if (!$ancestorClassLike instanceof \PhpParser\Node\Stmt\ClassLike) {
|
2021-04-23 11:03:45 +02:00
|
|
|
continue;
|
2021-04-22 00:02:38 +02:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
$classMethods = \array_merge($classMethods, $ancestorClassLike->getMethods());
|
2021-04-22 00:02:38 +02:00
|
|
|
}
|
|
|
|
return $classMethods;
|
|
|
|
}
|
|
|
|
}
|