2020-02-10 09:17:05 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2020-12-24 16:31:24 +00:00
|
|
|
namespace Rector\StaticTypeMapper\Naming;
|
2020-02-10 09:17:05 +00:00
|
|
|
|
|
|
|
use PhpParser\Node;
|
2021-01-08 22:30:33 +00:00
|
|
|
use PhpParser\Node\Stmt\ClassLike;
|
2020-02-10 09:17:05 +00:00
|
|
|
use PhpParser\Node\Stmt\Use_;
|
|
|
|
use PhpParser\Node\Stmt\UseUse;
|
|
|
|
use PHPStan\Analyser\NameScope;
|
2021-03-17 22:03:49 +00:00
|
|
|
use PHPStan\Analyser\Scope;
|
2020-12-28 13:01:27 +00:00
|
|
|
use PHPStan\Type\Generic\TemplateTypeMap;
|
2021-01-08 22:30:33 +00:00
|
|
|
use PHPStan\Type\Type;
|
2021-01-19 21:32:28 +00:00
|
|
|
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
|
2020-02-10 09:17:05 +00:00
|
|
|
use Rector\Core\Exception\ShouldNotHappenException;
|
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
2020-12-28 13:01:27 +00:00
|
|
|
use Rector\StaticTypeMapper\StaticTypeMapper;
|
2020-02-10 09:17:05 +00:00
|
|
|
/**
|
|
|
|
* @see https://github.com/phpstan/phpstan-src/blob/8376548f76e2c845ae047e3010e873015b796818/src/Analyser/NameScope.php#L32
|
|
|
|
*/
|
|
|
|
final class NameScopeFactory
|
|
|
|
{
|
2020-12-28 13:01:27 +00:00
|
|
|
/**
|
|
|
|
* @var StaticTypeMapper
|
|
|
|
*/
|
|
|
|
private $staticTypeMapper;
|
2021-01-19 21:32:28 +00:00
|
|
|
/**
|
|
|
|
* @var PhpDocInfoFactory
|
|
|
|
*/
|
|
|
|
private $phpDocInfoFactory;
|
|
|
|
/**
|
|
|
|
* This is needed to avoid circular references
|
|
|
|
* @required
|
|
|
|
*/
|
2021-05-10 22:23:08 +00:00
|
|
|
public function autowireNameScopeFactory(\Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory $phpDocInfoFactory) : void
|
2021-01-19 21:32:28 +00:00
|
|
|
{
|
|
|
|
$this->phpDocInfoFactory = $phpDocInfoFactory;
|
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
public function createNameScopeFromNodeWithoutTemplateTypes(\PhpParser\Node $node) : \PHPStan\Analyser\NameScope
|
2020-02-10 09:17:05 +00:00
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
$scope = $node->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::SCOPE);
|
|
|
|
$namespace = $scope instanceof \PHPStan\Analyser\Scope ? $scope->getNamespace() : null;
|
2020-02-10 09:17:05 +00:00
|
|
|
/** @var Use_[] $useNodes */
|
2021-05-10 22:23:08 +00:00
|
|
|
$useNodes = (array) $node->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::USE_NODES);
|
2020-02-10 09:17:05 +00:00
|
|
|
$uses = $this->resolveUseNamesByAlias($useNodes);
|
2021-05-10 22:23:08 +00:00
|
|
|
$className = $node->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::CLASS_NAME);
|
|
|
|
return new \PHPStan\Analyser\NameScope($namespace, $uses, $className);
|
2020-02-10 09:17:05 +00:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
public function createNameScopeFromNode(\PhpParser\Node $node) : \PHPStan\Analyser\NameScope
|
2020-12-28 13:01:27 +00:00
|
|
|
{
|
|
|
|
$nameScope = $this->createNameScopeFromNodeWithoutTemplateTypes($node);
|
|
|
|
$templateTypeMap = $this->templateTemplateTypeMap($node);
|
2021-05-10 22:23:08 +00:00
|
|
|
return new \PHPStan\Analyser\NameScope($nameScope->getNamespace(), $nameScope->getUses(), $nameScope->getClassName(), null, $templateTypeMap);
|
2020-12-28 13:01:27 +00:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
public function setStaticTypeMapper(\Rector\StaticTypeMapper\StaticTypeMapper $staticTypeMapper) : void
|
2020-12-28 13:01:27 +00:00
|
|
|
{
|
|
|
|
$this->staticTypeMapper = $staticTypeMapper;
|
|
|
|
}
|
2020-02-10 09:17:05 +00:00
|
|
|
/**
|
|
|
|
* @param Use_[] $useNodes
|
2020-09-01 17:56:30 +00:00
|
|
|
* @return array<string, string>
|
2020-02-10 09:17:05 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
private function resolveUseNamesByAlias(array $useNodes) : array
|
2020-02-10 09:17:05 +00:00
|
|
|
{
|
|
|
|
$useNamesByAlias = [];
|
|
|
|
foreach ($useNodes as $useNode) {
|
|
|
|
foreach ($useNode->uses as $useUse) {
|
|
|
|
/** @var UseUse $useUse */
|
2021-05-09 20:15:43 +00:00
|
|
|
$aliasName = $useUse->getAlias()->name;
|
2020-02-10 09:17:05 +00:00
|
|
|
$useName = $useUse->name->toString();
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!\is_string($useName)) {
|
2021-05-10 22:23:08 +00:00
|
|
|
throw new \Rector\Core\Exception\ShouldNotHappenException();
|
2020-02-10 09:17:05 +00:00
|
|
|
}
|
2020-09-01 17:56:30 +00:00
|
|
|
// uses must be lowercase, as PHPStan lowercases it
|
2021-05-09 20:15:43 +00:00
|
|
|
$lowercasedAliasName = \strtolower($aliasName);
|
2020-09-01 17:56:30 +00:00
|
|
|
$useNamesByAlias[$lowercasedAliasName] = $useName;
|
2020-02-10 09:17:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $useNamesByAlias;
|
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
private function templateTemplateTypeMap(\PhpParser\Node $node) : \PHPStan\Type\Generic\TemplateTypeMap
|
2021-01-08 22:30:33 +00:00
|
|
|
{
|
|
|
|
$nodeTemplateTypes = $this->resolveTemplateTypesFromNode($node);
|
2021-05-10 22:23:08 +00:00
|
|
|
$class = $node->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::CLASS_NODE);
|
2021-01-08 22:30:33 +00:00
|
|
|
$classTemplateTypes = [];
|
2021-05-10 22:23:08 +00:00
|
|
|
if ($class instanceof \PhpParser\Node\Stmt\ClassLike) {
|
2021-01-08 22:30:33 +00:00
|
|
|
$classTemplateTypes = $this->resolveTemplateTypesFromNode($class);
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
$templateTypes = \array_merge($nodeTemplateTypes, $classTemplateTypes);
|
2021-05-10 22:23:08 +00:00
|
|
|
return new \PHPStan\Type\Generic\TemplateTypeMap($templateTypes);
|
2021-01-08 22:30:33 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @return Type[]
|
|
|
|
*/
|
2021-05-10 22:23:08 +00:00
|
|
|
private function resolveTemplateTypesFromNode(\PhpParser\Node $node) : array
|
2020-12-28 13:01:27 +00:00
|
|
|
{
|
2021-01-19 21:32:28 +00:00
|
|
|
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
|
2020-12-28 13:01:27 +00:00
|
|
|
$templateTypes = [];
|
2021-01-08 22:30:33 +00:00
|
|
|
foreach ($phpDocInfo->getTemplateTagValueNodes() as $templateTagValueNode) {
|
|
|
|
$phpstanType = $this->staticTypeMapper->mapPHPStanPhpDocTypeToPHPStanType($templateTagValueNode, $node);
|
|
|
|
$templateTypes[$templateTagValueNode->name] = $phpstanType;
|
2020-12-28 13:01:27 +00:00
|
|
|
}
|
2021-01-08 22:30:33 +00:00
|
|
|
return $templateTypes;
|
2020-12-28 13:01:27 +00:00
|
|
|
}
|
2020-02-10 09:17:05 +00:00
|
|
|
}
|