Skip to content

Commit

Permalink
Updated Rector to commit 24a32df
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jul 21, 2021
1 parent d15985c commit e3f9904
Show file tree
Hide file tree
Showing 56 changed files with 230 additions and 228 deletions.
5 changes: 3 additions & 2 deletions packages/NodeTypeResolver/PHPStan/Type/TypeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ public function createMixedPassedOrUnionType(array $types) : \PHPStan\Type\Type
return $this->createUnionOrSingleType($types);
}
/**
* @param Type[] $types
* @return Type[]
* @template TType as Type
* @param array<TType> $types
* @return array<TType>
*/
public function uniquateTypes(array $types, bool $keepConstant = \false) : array
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Rector\CodingStyle\ClassNameImport\ClassNameImportSkipper;
use Rector\Core\Configuration\Option;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\Provider\CurrentFileProvider;
use Rector\Core\ValueObject\Application\File;
use Rector\PostRector\Collector\UseNodesToAddCollector;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
Expand Down Expand Up @@ -41,12 +43,17 @@ final class NameImportingPhpDocNodeVisitor extends \RectorPrefix20210721\Symplif
* @var \Rector\PostRector\Collector\UseNodesToAddCollector
*/
private $useNodesToAddCollector;
public function __construct(\Rector\StaticTypeMapper\StaticTypeMapper $staticTypeMapper, \RectorPrefix20210721\Symplify\PackageBuilder\Parameter\ParameterProvider $parameterProvider, \Rector\CodingStyle\ClassNameImport\ClassNameImportSkipper $classNameImportSkipper, \Rector\PostRector\Collector\UseNodesToAddCollector $useNodesToAddCollector)
/**
* @var \Rector\Core\Provider\CurrentFileProvider
*/
private $currentFileProvider;
public function __construct(\Rector\StaticTypeMapper\StaticTypeMapper $staticTypeMapper, \RectorPrefix20210721\Symplify\PackageBuilder\Parameter\ParameterProvider $parameterProvider, \Rector\CodingStyle\ClassNameImport\ClassNameImportSkipper $classNameImportSkipper, \Rector\PostRector\Collector\UseNodesToAddCollector $useNodesToAddCollector, \Rector\Core\Provider\CurrentFileProvider $currentFileProvider)
{
$this->staticTypeMapper = $staticTypeMapper;
$this->parameterProvider = $parameterProvider;
$this->classNameImportSkipper = $classNameImportSkipper;
$this->useNodesToAddCollector = $useNodesToAddCollector;
$this->currentFileProvider = $currentFileProvider;
}
public function beforeTraverse(\PHPStan\PhpDocParser\Ast\Node $node) : void
{
Expand Down Expand Up @@ -74,39 +81,47 @@ public function enterNode(\PHPStan\PhpDocParser\Ast\Node $node) : ?\PHPStan\PhpD
if ($this->shouldSkipShortClassName($staticType)) {
return null;
}
return $this->processFqnNameImport($this->currentPhpParserNode, $node, $staticType);
$file = $this->currentFileProvider->getFile();
if (!$file instanceof \Rector\Core\ValueObject\Application\File) {
return null;
}
return $this->processFqnNameImport($this->currentPhpParserNode, $node, $staticType, $file);
}
public function setCurrentNode(\PhpParser\Node $phpParserNode) : void
{
$this->currentPhpParserNode = $phpParserNode;
}
private function processFqnNameImport(\PhpParser\Node $phpParserNode, \PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode $identifierTypeNode, \Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType $fullyQualifiedObjectType) : ?\PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode
private function processFqnNameImport(\PhpParser\Node $phpParserNode, \PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode $identifierTypeNode, \Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType $fullyQualifiedObjectType, \Rector\Core\ValueObject\Application\File $file) : ?\PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode
{
if ($this->classNameImportSkipper->shouldSkipNameForFullyQualifiedObjectType($phpParserNode, $fullyQualifiedObjectType)) {
return $identifierTypeNode;
if ($this->classNameImportSkipper->shouldSkipNameForFullyQualifiedObjectType($file, $phpParserNode, $fullyQualifiedObjectType)) {
return null;
}
$parent = $identifierTypeNode->getAttribute(\Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey::PARENT);
if ($parent instanceof \PHPStan\PhpDocParser\Ast\PhpDoc\TemplateTagValueNode) {
// might break
return null;
}
$newNode = new \PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode($fullyQualifiedObjectType->getShortName());
// should skip because its already used
if ($this->useNodesToAddCollector->isShortImported($phpParserNode, $fullyQualifiedObjectType)) {
if ($this->useNodesToAddCollector->isImportShortable($phpParserNode, $fullyQualifiedObjectType)) {
$newNode = new \PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode($fullyQualifiedObjectType->getShortName());
if ($newNode->name !== $identifierTypeNode->name) {
return $newNode;
}
return $identifierTypeNode;
if ($this->useNodesToAddCollector->isShortImported($file, $fullyQualifiedObjectType)) {
if (!$this->useNodesToAddCollector->isImportShortable($file, $fullyQualifiedObjectType)) {
return null;
}
return $identifierTypeNode;
if ($newNode->name !== $identifierTypeNode->name) {
$this->useNodesToAddCollector->addUseImport($fullyQualifiedObjectType);
return $newNode;
}
return null;
}
$this->useNodesToAddCollector->addUseImport($fullyQualifiedObjectType);
$newNode = new \PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode($fullyQualifiedObjectType->getShortName());
if ($newNode->name !== $identifierTypeNode->name) {
// do not import twice
if ($this->useNodesToAddCollector->isShortImported($file, $fullyQualifiedObjectType)) {
return null;
}
$this->useNodesToAddCollector->addUseImport($fullyQualifiedObjectType);
return $newNode;
}
return $identifierTypeNode;
return null;
}
private function shouldSkipShortClassName(\Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType $fullyQualifiedObjectType) : bool
{
Expand All @@ -126,7 +141,11 @@ private function processDoctrineAnnotationTagValueNode(\Rector\BetterPhpDocParse
}
$staticType = new \Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType($staticType->getClassName());
}
$shortentedIdentifierTypeNode = $this->processFqnNameImport($this->currentPhpParserNode, $identifierTypeNode, $staticType);
$file = $this->currentFileProvider->getFile();
if (!$file instanceof \Rector\Core\ValueObject\Application\File) {
return;
}
$shortentedIdentifierTypeNode = $this->processFqnNameImport($this->currentPhpParserNode, $identifierTypeNode, $staticType, $file);
if (!$shortentedIdentifierTypeNode instanceof \PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode) {
return;
}
Expand Down Expand Up @@ -154,7 +173,11 @@ private function enterSpacelessPhpDocTagNode(\Rector\BetterPhpDocParser\PhpDoc\S
}
$staticType = new \Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType($staticType->getClassName());
}
$importedName = $this->processFqnNameImport($this->currentPhpParserNode, $identifierTypeNode, $staticType);
$file = $this->currentFileProvider->getFile();
if (!$file instanceof \Rector\Core\ValueObject\Application\File) {
return null;
}
$importedName = $this->processFqnNameImport($this->currentPhpParserNode, $identifierTypeNode, $staticType, $file);
if ($importedName !== null) {
$spacelessPhpDocTagNode->name = '@' . $importedName->name;
return $spacelessPhpDocTagNode;
Expand Down
78 changes: 20 additions & 58 deletions packages/PostRector/Collector/UseNodesToAddCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
use Symplify\SmartFileSystem\SmartFileInfo;
final class UseNodesToAddCollector implements \Rector\PostRector\Contract\Collector\NodeCollectorInterface
{
/**
* @var string[][]
*/
private $removedShortUsesInFilePath = [];
/**
* @var array<string, FullyQualifiedObjectType[]>
*/
Expand Down Expand Up @@ -57,34 +53,15 @@ public function addFunctionUseImport($node, $fullyQualifiedObjectType) : void
$smartFileInfo = $file->getSmartFileInfo();
$this->functionUseImportTypesInFilePath[$smartFileInfo->getRealPath()][] = $fullyQualifiedObjectType;
}
/**
* @param \PhpParser\Node $node
* @param string $shortUse
*/
public function removeShortUse($node, $shortUse) : void
{
$file = $this->currentFileProvider->getFile();
if (!$file instanceof \Rector\Core\ValueObject\Application\File) {
return;
}
$smartFileInfo = $file->getSmartFileInfo();
$this->removedShortUsesInFilePath[$smartFileInfo->getRealPath()][] = $shortUse;
}
/**
* @param \Symplify\SmartFileSystem\SmartFileInfo $smartFileInfo
*/
public function clear($smartFileInfo) : void
{
// clear applied imports, so isActive() doesn't return any false positives
unset($this->useImportTypesInFilePath[$smartFileInfo->getRealPath()], $this->functionUseImportTypesInFilePath[$smartFileInfo->getRealPath()]);
}
/**
* @return AliasedObjectType[]|FullyQualifiedObjectType[]
* @param \Rector\Core\ValueObject\Application\File $file
* @param \PhpParser\Node $node
*/
public function getUseImportTypesByNode($node) : array
public function getUseImportTypesByNode($file, $node) : array
{
$filePath = $this->getRealPathFromNode();
$fileInfo = $file->getSmartFileInfo();
$filePath = $fileInfo->getRealPath();
$objectTypes = $this->useImportTypesInFilePath[$filePath] ?? [];
/** @var Use_[] $useNodes */
$useNodes = (array) $node->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::USE_NODES);
Expand All @@ -100,12 +77,13 @@ public function getUseImportTypesByNode($node) : array
return $objectTypes;
}
/**
* @param \Rector\Core\ValueObject\Application\File $file
* @param \PhpParser\Node $node
* @param \Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType $fullyQualifiedObjectType
*/
public function hasImport($node, $fullyQualifiedObjectType) : bool
public function hasImport($file, $node, $fullyQualifiedObjectType) : bool
{
$useImports = $this->getUseImportTypesByNode($node);
$useImports = $this->getUseImportTypesByNode($file, $node);
foreach ($useImports as $useImport) {
if ($useImport->equals($fullyQualifiedObjectType)) {
return \true;
Expand All @@ -114,17 +92,15 @@ public function hasImport($node, $fullyQualifiedObjectType) : bool
return \false;
}
/**
* @param \PhpParser\Node $node
* @param \Rector\Core\ValueObject\Application\File $file
* @param \Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType $fullyQualifiedObjectType
*/
public function isShortImported($node, $fullyQualifiedObjectType) : bool
public function isShortImported($file, $fullyQualifiedObjectType) : bool
{
$filePath = $this->getRealPathFromNode();
if ($filePath === null) {
return \false;
}
$fileInfo = $file->getSmartFileInfo();
$filePath = $fileInfo->getRealPath();
$shortName = $fullyQualifiedObjectType->getShortName();
if ($this->isShortClassImported($filePath, $shortName)) {
if ($this->isShortClassImported($file, $shortName)) {
return \true;
}
$fileFunctionUseImportTypes = $this->functionUseImportTypesInFilePath[$filePath] ?? [];
Expand All @@ -136,12 +112,13 @@ public function isShortImported($node, $fullyQualifiedObjectType) : bool
return \false;
}
/**
* @param \PhpParser\Node $node
* @param \Rector\Core\ValueObject\Application\File $file
* @param \Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType $fullyQualifiedObjectType
*/
public function isImportShortable($node, $fullyQualifiedObjectType) : bool
public function isImportShortable($file, $fullyQualifiedObjectType) : bool
{
$filePath = $this->getRealPathFromNode();
$fileInfo = $file->getSmartFileInfo();
$filePath = $fileInfo->getRealPath();
$fileUseImportTypes = $this->useImportTypesInFilePath[$filePath] ?? [];
foreach ($fileUseImportTypes as $fileUseImportType) {
if ($fullyQualifiedObjectType->equals($fileUseImportType)) {
Expand Down Expand Up @@ -172,26 +149,11 @@ public function getFunctionImportsByFileInfo($smartFileInfo) : array
{
return $this->functionUseImportTypesInFilePath[$smartFileInfo->getRealPath()] ?? [];
}
/**
* @return string[]
* @param \Symplify\SmartFileSystem\SmartFileInfo $smartFileInfo
*/
public function getShortUsesByFileInfo($smartFileInfo) : array
{
return $this->removedShortUsesInFilePath[$smartFileInfo->getRealPath()] ?? [];
}
private function getRealPathFromNode() : ?string
{
$file = $this->currentFileProvider->getFile();
if (!$file instanceof \Rector\Core\ValueObject\Application\File) {
return null;
}
$smartFileInfo = $file->getSmartFileInfo();
return $smartFileInfo->getRealPath();
}
private function isShortClassImported(string $filePath, string $shortName) : bool
private function isShortClassImported(\Rector\Core\ValueObject\Application\File $file, string $shortName) : bool
{
$fileUseImports = $this->useImportTypesInFilePath[$filePath] ?? [];
$fileInfo = $file->getSmartFileInfo();
$realPath = $fileInfo->getRealPath();
$fileUseImports = $this->useImportTypesInFilePath[$realPath] ?? [];
foreach ($fileUseImports as $fileUseImport) {
if ($fileUseImport->getShortName() === $shortName) {
return \true;
Expand Down
21 changes: 14 additions & 7 deletions packages/PostRector/Rector/NameImportingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,25 @@ private function processNodeName(\PhpParser\Node\Name $name, \Rector\Core\ValueO
// @todo test if old stmts or new stmts! or both? :)
/** @var Use_[] $currentUses */
$currentUses = $this->betterNodeFinder->findInstanceOf($file->getNewStmts(), \PhpParser\Node\Stmt\Use_::class);
if ($this->shouldImportName($name, $file, $currentUses)) {
return $this->nameImporter->importName($name, $file, $currentUses);
}
return null;
}
/**
* @param Use_[] $currentUses
*/
private function shouldImportName(\PhpParser\Node\Name $name, \Rector\Core\ValueObject\Application\File $file, array $currentUses) : bool
{
if (\substr_count($name->toCodeString(), '\\') <= 1) {
return $this->nameImporter->importName($name, $currentUses);
return \true;
}
if (!$this->classNameImportSkipper->isFoundInUse($name, $currentUses)) {
return $this->nameImporter->importName($name, $currentUses);
return \true;
}
if ($this->classNameImportSkipper->isAlreadyImported($name, $currentUses)) {
return $this->nameImporter->importName($name, $currentUses);
return \true;
}
if ($this->reflectionProvider->hasFunction(new \PhpParser\Node\Name($name->getLast()), null)) {
return $this->nameImporter->importName($name, $currentUses);
}
return null;
return $this->reflectionProvider->hasFunction(new \PhpParser\Node\Name($name->getLast()), null);
}
}
8 changes: 3 additions & 5 deletions packages/PostRector/Rector/UseAddingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,18 @@ public function beforeTraverse(array $nodes) : ?array
$smartFileInfo = $file->getSmartFileInfo();
$useImportTypes = $this->useNodesToAddCollector->getObjectImportsByFileInfo($smartFileInfo);
$functionUseImportTypes = $this->useNodesToAddCollector->getFunctionImportsByFileInfo($smartFileInfo);
$removedShortUses = $this->useNodesToAddCollector->getShortUsesByFileInfo($smartFileInfo);
$oldToNewClasses = $this->renamedClassesDataCollector->getOldToNewClasses();
// nothing to import or remove
if ($useImportTypes === [] && $functionUseImportTypes === [] && $removedShortUses === [] && $oldToNewClasses === []) {
if ($useImportTypes === [] && $functionUseImportTypes === [] && $oldToNewClasses === []) {
return $nodes;
}
/** @var FullyQualifiedObjectType[] $useImportTypes */
$useImportTypes = $this->typeFactory->uniquateTypes($useImportTypes);
$this->useNodesToAddCollector->clear($smartFileInfo);
// A. has namespace? add under it
$namespace = $this->betterNodeFinder->findFirstInstanceOf($nodes, \PhpParser\Node\Stmt\Namespace_::class);
if ($namespace instanceof \PhpParser\Node\Stmt\Namespace_) {
// first clean
$this->useImportsRemover->removeImportsFromNamespace($namespace, $removedShortUses);
//$this->useImportsRemover->removeImportsFromNamespace($namespace, $removedShortUses);
// then add, to prevent adding + removing false positive of same short use
$this->useImportsAdder->addImportsToNamespace($namespace, $useImportTypes, $functionUseImportTypes);
return $nodes;
Expand All @@ -92,7 +90,7 @@ public function beforeTraverse(array $nodes) : ?array
if ($firstNode instanceof \Rector\Core\PhpParser\Node\CustomNode\FileWithoutNamespace) {
$nodes = $firstNode->stmts;
}
$removedShortUses = \array_merge($removedShortUses, $this->renamedClassesDataCollector->getOldClasses());
$removedShortUses = $this->renamedClassesDataCollector->getOldClasses();
// B. no namespace? add in the top
// first clean
$nodes = $this->useImportsRemover->removeImportsFromStmts($nodes, $removedShortUses);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ public function getUseNode() : \PhpParser\Node\Stmt\Use_
public function getFunctionUseNode() : \PhpParser\Node\Stmt\Use_
{
$name = new \PhpParser\Node\Name($this->getClassName());
$useUse = new \PhpParser\Node\Stmt\UseUse($name, null, \PhpParser\Node\Stmt\Use_::TYPE_FUNCTION);
$useUse = new \PhpParser\Node\Stmt\UseUse($name, null);
$name->setAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::PARENT_NODE, $useUse);
return new \PhpParser\Node\Stmt\Use_([$useUse]);
$use = new \PhpParser\Node\Stmt\Use_([$useUse]);
$use->type = \PhpParser\Node\Stmt\Use_::TYPE_FUNCTION;
return $use;
}
public function getShortNameLowered() : string
{
Expand Down
8 changes: 7 additions & 1 deletion rules/CodingStyle/Application/UseImportsAdder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PhpParser\Node\Stmt\Use_;
use PHPStan\Type\ObjectType;
use Rector\CodingStyle\ClassNameImport\UsedImportsResolver;
use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory;
use Rector\StaticTypeMapper\ValueObject\Type\AliasedObjectType;
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
final class UseImportsAdder
Expand All @@ -19,9 +20,14 @@ final class UseImportsAdder
* @var \Rector\CodingStyle\ClassNameImport\UsedImportsResolver
*/
private $usedImportsResolver;
public function __construct(\Rector\CodingStyle\ClassNameImport\UsedImportsResolver $usedImportsResolver)
/**
* @var \Rector\NodeTypeResolver\PHPStan\Type\TypeFactory
*/
private $typeFactory;
public function __construct(\Rector\CodingStyle\ClassNameImport\UsedImportsResolver $usedImportsResolver, \Rector\NodeTypeResolver\PHPStan\Type\TypeFactory $typeFactory)
{
$this->usedImportsResolver = $usedImportsResolver;
$this->typeFactory = $typeFactory;
}
/**
* @param Stmt[] $stmts
Expand Down
Loading

0 comments on commit e3f9904

Please sign in to comment.