2019-10-13 07:59:52 +02:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2019-05-19 23:44:38 +02:00
|
|
|
namespace Rector\CodingStyle\Application;
|
|
|
|
|
2022-03-28 09:17:08 +00:00
|
|
|
use RectorPrefix20220328\Nette\Utils\Strings;
|
2019-05-19 23:44:38 +02:00
|
|
|
use PhpParser\Node\Stmt;
|
2019-10-03 08:53:23 +02:00
|
|
|
use PhpParser\Node\Stmt\Declare_;
|
2019-05-19 23:44:38 +02:00
|
|
|
use PhpParser\Node\Stmt\Namespace_;
|
2020-11-20 00:26:04 +00:00
|
|
|
use PhpParser\Node\Stmt\Nop;
|
2019-05-19 23:44:38 +02:00
|
|
|
use PhpParser\Node\Stmt\Use_;
|
2020-01-21 01:54:47 +01:00
|
|
|
use PHPStan\Type\ObjectType;
|
2020-11-20 13:37:53 +00:00
|
|
|
use Rector\CodingStyle\ClassNameImport\UsedImportsResolver;
|
2021-11-29 11:31:19 +00:00
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
2021-07-21 13:46:30 +00:00
|
|
|
use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory;
|
2020-12-24 17:31:24 +01:00
|
|
|
use Rector\StaticTypeMapper\ValueObject\Type\AliasedObjectType;
|
|
|
|
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
|
2019-05-19 23:44:38 +02:00
|
|
|
final class UseImportsAdder
|
|
|
|
{
|
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\CodingStyle\ClassNameImport\UsedImportsResolver
|
2019-05-19 23:44:38 +02:00
|
|
|
*/
|
|
|
|
private $usedImportsResolver;
|
2021-07-21 13:46:30 +00:00
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-07-21 13:46:30 +00:00
|
|
|
* @var \Rector\NodeTypeResolver\PHPStan\Type\TypeFactory
|
|
|
|
*/
|
|
|
|
private $typeFactory;
|
|
|
|
public function __construct(\Rector\CodingStyle\ClassNameImport\UsedImportsResolver $usedImportsResolver, \Rector\NodeTypeResolver\PHPStan\Type\TypeFactory $typeFactory)
|
2019-05-19 23:44:38 +02:00
|
|
|
{
|
|
|
|
$this->usedImportsResolver = $usedImportsResolver;
|
2021-07-21 13:46:30 +00:00
|
|
|
$this->typeFactory = $typeFactory;
|
2019-05-19 23:44:38 +02:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param Stmt[] $stmts
|
2021-07-21 14:23:45 +00:00
|
|
|
* @param array<FullyQualifiedObjectType|AliasedObjectType> $useImportTypes
|
|
|
|
* @param array<FullyQualifiedObjectType|AliasedObjectType> $functionUseImportTypes
|
2020-11-20 00:26:04 +00:00
|
|
|
* @return Stmt[]
|
2019-05-19 23:44:38 +02:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function addImportsToStmts(array $stmts, array $useImportTypes, array $functionUseImportTypes) : array
|
2019-05-19 23:44:38 +02:00
|
|
|
{
|
2019-09-06 12:30:58 +02:00
|
|
|
$existingUseImportTypes = $this->usedImportsResolver->resolveForStmts($stmts);
|
2019-05-19 23:44:38 +02:00
|
|
|
$existingFunctionUseImports = $this->usedImportsResolver->resolveFunctionImportsForStmts($stmts);
|
2019-09-06 12:30:58 +02:00
|
|
|
$useImportTypes = $this->diffFullyQualifiedObjectTypes($useImportTypes, $existingUseImportTypes);
|
2021-05-09 20:15:43 +00:00
|
|
|
$functionUseImportTypes = $this->diffFullyQualifiedObjectTypes($functionUseImportTypes, $existingFunctionUseImports);
|
2019-09-06 12:30:58 +02:00
|
|
|
$newUses = $this->createUses($useImportTypes, $functionUseImportTypes, null);
|
2021-03-05 01:16:22 +01:00
|
|
|
if ($newUses === []) {
|
|
|
|
return $stmts;
|
|
|
|
}
|
2019-10-03 08:53:23 +02:00
|
|
|
// place after declare strict_types
|
|
|
|
foreach ($stmts as $key => $stmt) {
|
2021-05-10 22:23:08 +00:00
|
|
|
if ($stmt instanceof \PhpParser\Node\Stmt\Declare_) {
|
|
|
|
if (isset($stmts[$key + 1]) && $stmts[$key + 1] instanceof \PhpParser\Node\Stmt\Use_) {
|
2020-11-27 13:39:01 +01:00
|
|
|
$nodesToAdd = $newUses;
|
|
|
|
} else {
|
|
|
|
// add extra space, if there are no new use imports to be added
|
2021-05-10 22:23:08 +00:00
|
|
|
$nodesToAdd = \array_merge([new \PhpParser\Node\Stmt\Nop()], $newUses);
|
2020-11-27 13:39:01 +01:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
\array_splice($stmts, $key + 1, 0, $nodesToAdd);
|
2019-10-03 08:53:23 +02:00
|
|
|
return $stmts;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// make use stmts first
|
2021-05-09 20:15:43 +00:00
|
|
|
return \array_merge($newUses, $stmts);
|
2019-05-19 23:44:38 +02:00
|
|
|
}
|
|
|
|
/**
|
2019-09-06 12:30:58 +02:00
|
|
|
* @param FullyQualifiedObjectType[] $useImportTypes
|
|
|
|
* @param FullyQualifiedObjectType[] $functionUseImportTypes
|
2019-05-19 23:44:38 +02:00
|
|
|
*/
|
2021-05-10 22:23:08 +00:00
|
|
|
public function addImportsToNamespace(\PhpParser\Node\Stmt\Namespace_ $namespace, array $useImportTypes, array $functionUseImportTypes) : void
|
2021-05-09 20:15:43 +00:00
|
|
|
{
|
2019-05-19 23:44:38 +02:00
|
|
|
$namespaceName = $this->getNamespaceName($namespace);
|
2019-09-06 12:30:58 +02:00
|
|
|
$existingUseImportTypes = $this->usedImportsResolver->resolveForNode($namespace);
|
|
|
|
$existingFunctionUseImportTypes = $this->usedImportsResolver->resolveFunctionImportsForStmts($namespace->stmts);
|
2021-07-21 14:23:45 +00:00
|
|
|
$existingUseImportTypes = $this->typeFactory->uniquateTypes($existingUseImportTypes);
|
2019-09-06 12:30:58 +02:00
|
|
|
$useImportTypes = $this->diffFullyQualifiedObjectTypes($useImportTypes, $existingUseImportTypes);
|
2021-05-09 20:15:43 +00:00
|
|
|
$functionUseImportTypes = $this->diffFullyQualifiedObjectTypes($functionUseImportTypes, $existingFunctionUseImportTypes);
|
2019-09-06 12:30:58 +02:00
|
|
|
$newUses = $this->createUses($useImportTypes, $functionUseImportTypes, $namespaceName);
|
2021-11-29 11:31:19 +00:00
|
|
|
if ($namespace->stmts[0] instanceof \PhpParser\Node\Stmt\Use_ && $newUses !== []) {
|
|
|
|
$comments = (array) $namespace->stmts[0]->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::COMMENTS);
|
|
|
|
if ($comments !== []) {
|
|
|
|
$newUses[0]->setAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::COMMENTS, $namespace->stmts[0]->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::COMMENTS));
|
|
|
|
$namespace->stmts[0]->setAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::COMMENTS, null);
|
|
|
|
}
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
$namespace->stmts = \array_merge($newUses, $namespace->stmts);
|
2019-05-19 23:44:38 +02:00
|
|
|
}
|
2019-10-30 10:49:07 +01:00
|
|
|
/**
|
2021-07-21 14:23:45 +00:00
|
|
|
* @param array<FullyQualifiedObjectType|AliasedObjectType> $mainTypes
|
|
|
|
* @param array<FullyQualifiedObjectType|AliasedObjectType> $typesToRemove
|
|
|
|
* @return array<FullyQualifiedObjectType|AliasedObjectType>
|
2019-10-30 10:49:07 +01:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
private function diffFullyQualifiedObjectTypes(array $mainTypes, array $typesToRemove) : array
|
2019-05-19 23:44:38 +02:00
|
|
|
{
|
2019-10-30 10:49:07 +01:00
|
|
|
foreach ($mainTypes as $key => $mainType) {
|
|
|
|
foreach ($typesToRemove as $typeToRemove) {
|
|
|
|
if ($mainType->equals($typeToRemove)) {
|
|
|
|
unset($mainTypes[$key]);
|
|
|
|
}
|
|
|
|
}
|
2019-05-19 23:44:38 +02:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
return \array_values($mainTypes);
|
2019-05-19 23:44:38 +02:00
|
|
|
}
|
|
|
|
/**
|
2021-07-21 14:23:45 +00:00
|
|
|
* @param array<AliasedObjectType|FullyQualifiedObjectType> $useImportTypes
|
|
|
|
* @param array<FullyQualifiedObjectType|AliasedObjectType> $functionUseImportTypes
|
2019-05-19 23:44:38 +02:00
|
|
|
* @return Use_[]
|
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
private function createUses(array $useImportTypes, array $functionUseImportTypes, ?string $namespaceName) : array
|
2019-05-19 23:44:38 +02:00
|
|
|
{
|
2019-09-06 12:30:58 +02:00
|
|
|
$newUses = [];
|
|
|
|
foreach ($useImportTypes as $useImportType) {
|
2019-09-11 15:37:09 +02:00
|
|
|
if ($namespaceName !== null && $this->isCurrentNamespace($namespaceName, $useImportType)) {
|
2019-05-19 23:44:38 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// already imported in previous cycle
|
2019-09-06 12:30:58 +02:00
|
|
|
$newUses[] = $useImportType->getUseNode();
|
2019-05-19 23:44:38 +02:00
|
|
|
}
|
2019-09-06 12:30:58 +02:00
|
|
|
foreach ($functionUseImportTypes as $functionUseImportType) {
|
2019-09-11 15:37:09 +02:00
|
|
|
if ($namespaceName !== null && $this->isCurrentNamespace($namespaceName, $functionUseImportType)) {
|
2019-05-19 23:44:38 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// already imported in previous cycle
|
2019-09-06 12:30:58 +02:00
|
|
|
$newUses[] = $functionUseImportType->getFunctionUseNode();
|
2019-05-19 23:44:38 +02:00
|
|
|
}
|
|
|
|
return $newUses;
|
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
private function getNamespaceName(\PhpParser\Node\Stmt\Namespace_ $namespace) : ?string
|
2019-09-06 12:30:58 +02:00
|
|
|
{
|
2019-10-30 10:49:07 +01:00
|
|
|
if ($namespace->name === null) {
|
|
|
|
return null;
|
2019-09-06 12:30:58 +02:00
|
|
|
}
|
2019-10-30 10:49:07 +01:00
|
|
|
return $namespace->name->toString();
|
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
private function isCurrentNamespace(string $namespaceName, \PHPStan\Type\ObjectType $objectType) : bool
|
2020-01-21 01:54:47 +01:00
|
|
|
{
|
2022-03-28 09:17:08 +00:00
|
|
|
$afterCurrentNamespace = \RectorPrefix20220328\Nette\Utils\Strings::after($objectType->getClassName(), $namespaceName . '\\');
|
2021-11-28 18:05:13 +00:00
|
|
|
if ($afterCurrentNamespace === null) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2019-10-30 10:49:07 +01:00
|
|
|
}
|
2021-05-29 22:10:59 +00:00
|
|
|
return \strpos($afterCurrentNamespace, '\\') === \false;
|
2019-09-06 12:30:58 +02:00
|
|
|
}
|
2019-05-19 23:44:38 +02:00
|
|
|
}
|