Skip to content

Commit

Permalink
Updated Rector to commit 363ae1bd444c365e38aef1cfcd69324b734c3c9c
Browse files Browse the repository at this point in the history
rectorphp/rector-src@363ae1b [Performance][PostRector] Only process FullyQualified on import Node name on NameImportingPostRector  (#5255)
  • Loading branch information
TomasVotruba committed Nov 16, 2023
1 parent 1d5d0dd commit 2da1774
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 78 deletions.
6 changes: 3 additions & 3 deletions packages/PostRector/Collector/UseNodesToAddCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use PhpParser\Node;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use Rector\Core\Provider\CurrentFileProvider;
use Rector\Core\ValueObject\Application\File;
use Rector\Naming\Naming\UseImportsResolver;
Expand Down Expand Up @@ -78,9 +78,9 @@ public function getUseImportTypesByNode(File $file, Node $node) : array
}
return $objectTypes;
}
public function hasImport(File $file, Name $name, FullyQualifiedObjectType $fullyQualifiedObjectType) : bool
public function hasImport(File $file, FullyQualified $fullyQualified, FullyQualifiedObjectType $fullyQualifiedObjectType) : bool
{
$useImports = $this->getUseImportTypesByNode($file, $name);
$useImports = $this->getUseImportTypesByNode($file, $fullyQualified);
foreach ($useImports as $useImport) {
if ($useImport->equals($fullyQualifiedObjectType)) {
return \true;
Expand Down
29 changes: 12 additions & 17 deletions packages/PostRector/Rector/NameImportingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
use Rector\Core\ValueObject\Application\File;
use Rector\Naming\Naming\AliasNameResolver;
use Rector\Naming\Naming\UseImportsResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockNameImporter;
final class NameImportingPostRector extends \Rector\PostRector\Rector\AbstractPostRector
{
Expand Down Expand Up @@ -93,7 +92,7 @@ public function enterNode(Node $node) : ?Node
if ($firstStmt instanceof FileWithoutNamespace && \current($firstStmt->stmts) instanceof InlineHTML) {
return null;
}
if ($node instanceof Name) {
if ($node instanceof FullyQualified) {
return $this->processNodeName($node, $file);
}
if (!$node instanceof Stmt && !$node instanceof Param) {
Expand All @@ -114,9 +113,9 @@ public function enterNode(Node $node) : ?Node
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node);
return $node;
}
private function processNodeName(Name $name, File $file) : ?Node
private function processNodeName(FullyQualified $fullyQualified, File $file) : ?Node
{
if ($name->isSpecialClassName()) {
if ($fullyQualified->isSpecialClassName()) {
return null;
}
$namespaces = \array_filter($file->getNewStmts(), static function (Stmt $stmt) : bool {
Expand All @@ -127,39 +126,35 @@ private function processNodeName(Name $name, File $file) : ?Node
}
/** @var Use_[]|GroupUse[] $currentUses */
$currentUses = $this->useImportsResolver->resolve();
if ($this->classNameImportSkipper->shouldSkipName($name, $currentUses)) {
if ($this->classNameImportSkipper->shouldSkipName($fullyQualified, $currentUses)) {
return null;
}
$nameInUse = $this->resolveNameInUse($name, $currentUses);
$nameInUse = $this->resolveNameInUse($fullyQualified, $currentUses);
if ($nameInUse instanceof Name) {
return $nameInUse;
}
return $this->nameImporter->importName($name, $file);
return $this->nameImporter->importName($fullyQualified, $file);
}
/**
* @param Use_[]|GroupUse[] $currentUses
*/
private function resolveNameInUse(Name $name, array $currentUses) : ?\PhpParser\Node\Name
private function resolveNameInUse(FullyQualified $fullyQualified, array $currentUses) : ?\PhpParser\Node\Name
{
$originalName = $name->getAttribute(AttributeKey::ORIGINAL_NAME);
if (!$originalName instanceof FullyQualified) {
return null;
}
$aliasName = $this->aliasNameResolver->resolveByName($name, $currentUses);
$aliasName = $this->aliasNameResolver->resolveByName($fullyQualified, $currentUses);
if (\is_string($aliasName)) {
return new Name($aliasName);
}
return $this->resolveLongNameInUseName($name, $currentUses);
return $this->resolveLongNameInUseName($fullyQualified, $currentUses);
}
/**
* @param Use_[]|GroupUse[] $currentUses
*/
private function resolveLongNameInUseName(Name $name, array $currentUses) : ?Name
private function resolveLongNameInUseName(FullyQualified $fullyQualified, array $currentUses) : ?Name
{
if (\substr_count($name->toCodeString(), '\\') === 1) {
if (\substr_count($fullyQualified->toCodeString(), '\\') === 1) {
return null;
}
$lastName = $name->getLast();
$lastName = $fullyQualified->getLast();
foreach ($currentUses as $currentUse) {
foreach ($currentUse->uses as $useUse) {
if ($useUse->name->getLast() !== $lastName) {
Expand Down
10 changes: 5 additions & 5 deletions rules/CodingStyle/ClassNameImport/ClassNameImportSkipper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use PhpParser\Node;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\GroupUse;
use PhpParser\Node\Stmt\Use_;
use PhpParser\Node\Stmt\UseUse;
Expand Down Expand Up @@ -45,13 +45,13 @@ public function shouldSkipNameForFullyQualifiedObjectType(File $file, Node $node
/**
* @param Use_[]|GroupUse[] $uses
*/
public function shouldSkipName(Name $name, array $uses) : bool
public function shouldSkipName(FullyQualified $fullyQualified, array $uses) : bool
{
if (\substr_count($name->toCodeString(), '\\') <= 1) {
if (\substr_count($fullyQualified->toCodeString(), '\\') <= 1) {
return \false;
}
$stringName = $name->toString();
$lastUseName = $name->getLast();
$stringName = $fullyQualified->toString();
$lastUseName = $fullyQualified->getLast();
$nameLastName = \strtolower($lastUseName);
foreach ($uses as $use) {
$prefix = $this->useImportsResolver->resolvePrefix($use);
Expand Down
60 changes: 22 additions & 38 deletions rules/CodingStyle/Node/NameImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Rector\CodingStyle\Node;

use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use Rector\CodingStyle\ClassNameImport\ClassNameImportSkipper;
use Rector\Core\Configuration\Option;
use Rector\Core\Configuration\Parameter\SimpleParameterProvider;
Expand Down Expand Up @@ -35,48 +36,43 @@ public function __construct(ClassNameImportSkipper $classNameImportSkipper, Stat
$this->staticTypeMapper = $staticTypeMapper;
$this->useNodesToAddCollector = $useNodesToAddCollector;
}
public function importName(Name $name, File $file) : ?Name
public function importName(FullyQualified $fullyQualified, File $file) : ?Name
{
if ($this->shouldSkipName($name)) {
if ($this->shouldSkipName($fullyQualified)) {
return null;
}
$staticType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($name);
$staticType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($fullyQualified);
if (!$staticType instanceof FullyQualifiedObjectType) {
return null;
}
return $this->importNameAndCollectNewUseStatement($file, $name, $staticType);
return $this->importNameAndCollectNewUseStatement($file, $fullyQualified, $staticType);
}
private function shouldSkipName(Name $name) : bool
private function shouldSkipName(FullyQualified $fullyQualified) : bool
{
$virtualNode = (bool) $name->getAttribute(AttributeKey::VIRTUAL_NODE);
$virtualNode = (bool) $fullyQualified->getAttribute(AttributeKey::VIRTUAL_NODE);
if ($virtualNode) {
return \true;
}
// is scalar name?
if (\in_array($name->toLowerString(), ['true', 'false', 'bool'], \true)) {
if (\in_array($fullyQualified->toLowerString(), ['true', 'false', 'bool'], \true)) {
return \true;
}
// namespace <name>
// use <name>;
if ($this->isNamespaceOrUseImportName($name)) {
return \true;
}
if ($this->isFunctionOrConstantImportWithSingleName($name)) {
if ($this->isFunctionOrConstantImportWithSingleName($fullyQualified)) {
return \true;
}
// Importing root namespace classes (like \DateTime) is optional
if (!SimpleParameterProvider::provideBoolParameter(Option::IMPORT_SHORT_CLASSES)) {
$stringName = $name->toString();
$stringName = $fullyQualified->toString();
if (\substr_count($stringName, '\\') === 0) {
return \true;
}
}
return \false;
}
private function importNameAndCollectNewUseStatement(File $file, Name $name, FullyQualifiedObjectType $fullyQualifiedObjectType) : ?Name
private function importNameAndCollectNewUseStatement(File $file, FullyQualified $fullyQualified, FullyQualifiedObjectType $fullyQualifiedObjectType) : ?Name
{
// the same end is already imported → skip
if ($this->classNameImportSkipper->shouldSkipNameForFullyQualifiedObjectType($file, $name, $fullyQualifiedObjectType)) {
if ($this->classNameImportSkipper->shouldSkipNameForFullyQualifiedObjectType($file, $fullyQualified, $fullyQualifiedObjectType)) {
return null;
}
if ($this->useNodesToAddCollector->isShortImported($file, $fullyQualifiedObjectType)) {
Expand All @@ -85,39 +81,27 @@ private function importNameAndCollectNewUseStatement(File $file, Name $name, Ful
}
return null;
}
$this->addUseImport($file, $name, $fullyQualifiedObjectType);
$this->addUseImport($file, $fullyQualified, $fullyQualifiedObjectType);
return $fullyQualifiedObjectType->getShortNameNode();
}
/**
* Skip:
* - namespace name
* - use import name
*/
private function isNamespaceOrUseImportName(Name $name) : bool
{
if ($name->getAttribute(AttributeKey::IS_NAMESPACE_NAME) === \true) {
return \true;
}
return $name->getAttribute(AttributeKey::IS_USEUSE_NAME) === \true;
}
private function isFunctionOrConstantImportWithSingleName(Name $name) : bool
private function isFunctionOrConstantImportWithSingleName(FullyQualified $fullyQualified) : bool
{
if ($name->getAttribute(AttributeKey::IS_CONSTFETCH_NAME) === \true) {
return \count($name->getParts()) === 1;
if ($fullyQualified->getAttribute(AttributeKey::IS_CONSTFETCH_NAME) === \true) {
return \count($fullyQualified->getParts()) === 1;
}
if ($name->getAttribute(AttributeKey::IS_FUNCCALL_NAME) === \true) {
return \count($name->getParts()) === 1;
if ($fullyQualified->getAttribute(AttributeKey::IS_FUNCCALL_NAME) === \true) {
return \count($fullyQualified->getParts()) === 1;
}
return \false;
}
private function addUseImport(File $file, Name $name, FullyQualifiedObjectType $fullyQualifiedObjectType) : void
private function addUseImport(File $file, FullyQualified $fullyQualified, FullyQualifiedObjectType $fullyQualifiedObjectType) : void
{
if ($this->useNodesToAddCollector->hasImport($file, $name, $fullyQualifiedObjectType)) {
if ($this->useNodesToAddCollector->hasImport($file, $fullyQualified, $fullyQualifiedObjectType)) {
return;
}
if ($name->getAttribute(AttributeKey::IS_FUNCCALL_NAME) === \true) {
if ($fullyQualified->getAttribute(AttributeKey::IS_FUNCCALL_NAME) === \true) {
$this->useNodesToAddCollector->addFunctionUseImport($fullyQualifiedObjectType);
} elseif ($name->getAttribute(AttributeKey::IS_CONSTFETCH_NAME) === \true) {
} elseif ($fullyQualified->getAttribute(AttributeKey::IS_CONSTFETCH_NAME) === \true) {
$this->useNodesToAddCollector->addConstantUseImport($fullyQualifiedObjectType);
} else {
$this->useNodesToAddCollector->addUseImport($fullyQualifiedObjectType);
Expand Down
4 changes: 2 additions & 2 deletions src/Application/VersionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '1595e34d12d856eab8e2478de357f0b1dff3556f';
public const PACKAGE_VERSION = '363ae1bd444c365e38aef1cfcd69324b734c3c9c';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-11-16 21:49:13';
public const RELEASE_DATE = '2023-11-17 00:34:24';
/**
* @var int
*/
Expand Down
16 changes: 8 additions & 8 deletions vendor/composer/installed.json
Original file line number Diff line number Diff line change
Expand Up @@ -1382,26 +1382,26 @@
},
{
"name": "react\/promise",
"version": "v2.10.0",
"version_normalized": "2.10.0.0",
"version": "v2.11.0",
"version_normalized": "2.11.0.0",
"source": {
"type": "git",
"url": "https:\/\/github.com\/reactphp\/promise.git",
"reference": "f913fb8cceba1e6644b7b90c4bfb678ed8a3ef38"
"reference": "1a8460931ea36dc5c76838fec5734d55c88c6831"
},
"dist": {
"type": "zip",
"url": "https:\/\/api.github.com\/repos\/reactphp\/promise\/zipball\/f913fb8cceba1e6644b7b90c4bfb678ed8a3ef38",
"reference": "f913fb8cceba1e6644b7b90c4bfb678ed8a3ef38",
"url": "https:\/\/api.github.com\/repos\/reactphp\/promise\/zipball\/1a8460931ea36dc5c76838fec5734d55c88c6831",
"reference": "1a8460931ea36dc5c76838fec5734d55c88c6831",
"shasum": ""
},
"require": {
"php": ">=5.4.0"
},
"require-dev": {
"phpunit\/phpunit": "^9.5 || ^5.7 || ^4.8.36"
"phpunit\/phpunit": "^9.6 || ^5.7 || ^4.8.36"
},
"time": "2023-05-02T15:15:43+00:00",
"time": "2023-11-16T16:16:50+00:00",
"type": "library",
"installation-source": "dist",
"autoload": {
Expand Down Expand Up @@ -1445,7 +1445,7 @@
],
"support": {
"issues": "https:\/\/github.com\/reactphp\/promise\/issues",
"source": "https:\/\/github.com\/reactphp\/promise\/tree\/v2.10.0"
"source": "https:\/\/github.com\/reactphp\/promise\/tree\/v2.11.0"
},
"funding": [
{
Expand Down
Loading

0 comments on commit 2da1774

Please sign in to comment.