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