2019-10-13 07:59:52 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2019-05-19 23:44:38 +02:00
|
|
|
|
|
|
|
namespace Rector\CodingStyle\Application;
|
|
|
|
|
|
|
|
use Nette\Utils\Strings;
|
|
|
|
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;
|
2020-01-21 01:54:47 +01:00
|
|
|
use Rector\PHPStan\Type\AliasedObjectType;
|
2019-09-06 12:30:58 +02:00
|
|
|
use Rector\PHPStan\Type\FullyQualifiedObjectType;
|
2019-05-19 23:44:38 +02:00
|
|
|
|
|
|
|
final class UseImportsAdder
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var UsedImportsResolver
|
|
|
|
*/
|
|
|
|
private $usedImportsResolver;
|
|
|
|
|
|
|
|
public function __construct(UsedImportsResolver $usedImportsResolver)
|
|
|
|
{
|
|
|
|
$this->usedImportsResolver = $usedImportsResolver;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Stmt[] $stmts
|
2019-09-06 12:30:58 +02:00
|
|
|
* @param FullyQualifiedObjectType[] $useImportTypes
|
|
|
|
* @param FullyQualifiedObjectType[] $functionUseImportTypes
|
2020-11-20 00:26:04 +00:00
|
|
|
* @return Stmt[]
|
2019-05-19 23:44:38 +02:00
|
|
|
*/
|
2019-09-06 12:30:58 +02: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);
|
|
|
|
$functionUseImportTypes = $this->diffFullyQualifiedObjectTypes(
|
|
|
|
$functionUseImportTypes,
|
|
|
|
$existingFunctionUseImports
|
|
|
|
);
|
2019-05-19 23:44:38 +02:00
|
|
|
|
2019-09-06 12:30:58 +02:00
|
|
|
$newUses = $this->createUses($useImportTypes, $functionUseImportTypes, null);
|
2019-05-19 23:44:38 +02:00
|
|
|
|
2019-10-03 08:53:23 +02:00
|
|
|
// place after declare strict_types
|
|
|
|
foreach ($stmts as $key => $stmt) {
|
|
|
|
if ($stmt instanceof Declare_) {
|
2020-11-20 00:26:04 +00:00
|
|
|
$nodesToAdd = array_merge([new Nop()], $newUses);
|
|
|
|
array_splice($stmts, $key + 1, 0, $nodesToAdd);
|
2019-10-03 08:53:23 +02:00
|
|
|
|
|
|
|
return $stmts;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// make use stmts first
|
2019-05-19 23:44:38 +02:00
|
|
|
return array_merge($newUses, $stmts);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-09-06 12:30:58 +02:00
|
|
|
* @param FullyQualifiedObjectType[] $useImportTypes
|
|
|
|
* @param FullyQualifiedObjectType[] $functionUseImportTypes
|
2019-05-19 23:44:38 +02:00
|
|
|
*/
|
2019-09-06 12:30:58 +02:00
|
|
|
public function addImportsToNamespace(
|
|
|
|
Namespace_ $namespace,
|
|
|
|
array $useImportTypes,
|
|
|
|
array $functionUseImportTypes
|
|
|
|
): void {
|
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);
|
2019-05-19 23:44:38 +02:00
|
|
|
|
2019-09-06 12:30:58 +02:00
|
|
|
$useImportTypes = $this->diffFullyQualifiedObjectTypes($useImportTypes, $existingUseImportTypes);
|
|
|
|
$functionUseImportTypes = $this->diffFullyQualifiedObjectTypes(
|
|
|
|
$functionUseImportTypes,
|
|
|
|
$existingFunctionUseImportTypes
|
|
|
|
);
|
2019-05-19 23:44:38 +02:00
|
|
|
|
2019-09-06 12:30:58 +02:00
|
|
|
$newUses = $this->createUses($useImportTypes, $functionUseImportTypes, $namespaceName);
|
2019-05-19 23:44:38 +02:00
|
|
|
$namespace->stmts = array_merge($newUses, $namespace->stmts);
|
|
|
|
}
|
|
|
|
|
2019-10-30 10:49:07 +01:00
|
|
|
/**
|
|
|
|
* @param FullyQualifiedObjectType[] $mainTypes
|
|
|
|
* @param FullyQualifiedObjectType[] $typesToRemove
|
|
|
|
* @return FullyQualifiedObjectType[]
|
|
|
|
*/
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-10-30 10:49:07 +01:00
|
|
|
return array_values($mainTypes);
|
2019-05-19 23:44:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-21 01:54:47 +01:00
|
|
|
* @param AliasedObjectType[]|FullyQualifiedObjectType[] $useImportTypes
|
2019-09-06 12:30:58 +02:00
|
|
|
* @param FullyQualifiedObjectType[] $functionUseImportTypes
|
2019-05-19 23:44:38 +02:00
|
|
|
* @return Use_[]
|
|
|
|
*/
|
2019-09-06 12:30:58 +02: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
|
|
|
}
|
2019-09-06 12:30:58 +02:00
|
|
|
|
2019-05-19 23:44:38 +02:00
|
|
|
return $newUses;
|
|
|
|
}
|
2019-09-06 12:30:58 +02:00
|
|
|
|
2019-10-30 10:49:07 +01:00
|
|
|
private function getNamespaceName(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();
|
|
|
|
}
|
|
|
|
|
2020-01-21 01:54:47 +01:00
|
|
|
private function isCurrentNamespace(string $namespaceName, ObjectType $objectType): bool
|
|
|
|
{
|
2019-10-30 10:49:07 +01:00
|
|
|
if ($namespaceName === null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-01-21 01:54:47 +01:00
|
|
|
$afterCurrentNamespace = Strings::after($objectType->getClassName(), $namespaceName . '\\');
|
2019-10-30 10:49:07 +01:00
|
|
|
if (! $afterCurrentNamespace) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ! Strings::contains($afterCurrentNamespace, '\\');
|
2019-09-06 12:30:58 +02:00
|
|
|
}
|
2019-05-19 23:44:38 +02:00
|
|
|
}
|