8000 [Performance] Use more performance spl_object_id() (#4876) · rectorphp/rector-src@d8d31de · GitHub
[go: up one dir, main page]

Skip to content

Commit d8d31de

Browse files
8000
[Performance] Use more performance spl_object_id() (#4876)
* [Performance] Use more performance spl_object_id() * [ci-review] Rector Rectify * [ci-review] Rector Rectify * fix index * fix index * rename variable * fix phpstan * fix phpstan * fix phpstan * final touch: rename variable * final touch: rename variable * really final touch: rename variable --------- Co-authored-by: GitHub Action <actions@github.com>
1 parent c03fd2a commit d8d31de

File tree

8 files changed

+40
-40
lines changed

8 files changed

+40
-40
lines changed

packages/BetterPhpDocParser/PhpDocInfo/PhpDocInfoFactory.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
final class PhpDocInfoFactory
2424
{
2525
/**
26-
* @var array<string, PhpDocInfo>
26+
* @var array<int, PhpDocInfo>
2727
*/
28-
private array $phpDocInfosByObjectHash = [];
28+
private array $phpDocInfosByObjectId = [];
2929

3030
public function __construct(
3131
private readonly PhpDocNodeMapper $phpDocNodeMapper,
@@ -57,10 +57,10 @@ public function createFromNodeOrEmpty(Node $node): PhpDocInfo
5757

5858
public function createFromNode(Node $node): ?PhpDocInfo
5959
{
60-
$objectHash = spl_object_hash($node);
60+
$objectId = spl_object_id($node);
6161

62-
if (isset($this->phpDocInfosByObjectHash[$objectHash])) {
63-
return $this->phpDocInfosByObjectHash[$objectHash];
62+
if (isset($this->phpDocInfosByObjectId[$objectId])) {
8000 63+
return $this->phpDocInfosByObjectId[$objectId];
6464
}
6565

6666
/** @see \Rector\BetterPhpDocParser\PhpDocParser\DoctrineAnnotationDecorator::decorate() */
@@ -84,7 +84,7 @@ public function createFromNode(Node $node): ?PhpDocInfo
8484
}
8585

8686
$phpDocInfo = $this->createFromPhpDocNode($phpDocNode, $tokenIterator, $node);
87-
$this->phpDocInfosByObjectHash[$objectHash] = $phpDocInfo;
87+
$this->phpDocInfosByObjectId[$objectId] = $phpDocInfo;
8888

8989
return $phpDocInfo;
9090
}

packages/BetterPhpDocParser/PhpDocParser/ClassAnnotationMatcher.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ public function __construct(
3333

3434
public function resolveTagFullyQualifiedName(string $tag, Node $node): string
3535
{
36- 628C
$uniqueHash = $tag . spl_object_hash($node);
37-
if (isset($this->fullyQualifiedNameByHash[$uniqueHash])) {
38-
return $this->fullyQualifiedNameByHash[$uniqueHash];
36+
$uniqueId = $tag . spl_object_id($node);
37+
if (isset($this->fullyQualifiedNameByHash[$uniqueId])) {
38+
return $this->fullyQualifiedNameByHash[$uniqueId];
3939
}
4040

4141
$tag = ltrim($tag, '@');
@@ -47,7 +47,7 @@ public function resolveTagFullyQualifiedName(string $tag, Node $node): string
4747
$fullyQualifiedClass = $tag;
4848
}
4949

50-
$this->fullyQualifiedNameByHash[$uniqueHash] = $fullyQualifiedClass;
50+
$this->fullyQualifiedNameByHash[$uniqueId] = $fullyQualifiedClass;
5151

5252
return $fullyQualifiedClass;
5353
}

packages/PhpDocParser/NodeVisitor/CallableNodeVisitor.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ final class CallableNodeVisitor extends NodeVisitorAbstract
1818
*/
1919
private $callable;
2020

21-
private ?string $nodeHashToRemove = null;
21+
private ?int $nodeIdToRemove = null;
2222

2323
/**
2424
* @param callable(Node $node): (int|Node|null) $callable
@@ -38,7 +38,7 @@ public function enterNode(Node $node): int|Node|null
3838
$newNode = $callable($node);
3939

4040
if ($newNode === NodeTraverser::REMOVE_NODE) {
41-
$this->nodeHashToRemove = spl_object_hash($originalNode);
41+
$this->nodeIdToRemove = spl_object_id($originalNode);
4242
return $originalNode;
4343
}
4444

@@ -51,8 +51,8 @@ public function enterNode(Node $node): int|Node|null
5151

5252
public function leaveNode(Node $node): int|Node
5353
{
54-
if ($this->nodeHashToRemove === spl_object_hash($node)) {
55-
$this->nodeHashToRemove = null;
54+
if ($this->nodeIdToRemove === spl_object_id($node)) {
55+
$this->nodeIdToRemove = null;
5656
return NodeTraverser::REMOVE_NODE;
5757
}
5858

rules-tests/DeadCode/Rector/ClassMethod/RemoveUnusedPrivateMethodRector/Fixture/skip_anonymous_class.php.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class SkipAnonymousClass
5151

5252
private function processClassNode(Class_ $classNode): Class_
5353
{
54-
$variableInfos = $this->propertiesByClass[spl_object_hash($classNode)] ?? [];
54+
$variableInfos = $this->propertiesByClass[spl_object_id($classNode)] ?? [];
5555
foreach ($variableInfos as $propertyInfo) {
5656
$this->classManipulator->addConstructorDependency($classNode, $propertyInfo);
5757
}

rules/Naming/Naming/ConflictingNameResolver.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
final class ConflictingNameResolver
1818
{
1919
/**
20-
* @var array<string, string[]>
20+
* @var array<int, string[]>
2121
*/
2222
private array $conflictingVariableNamesByClassMethod = [];
2323

@@ -64,10 +64,10 @@ private function resolveConflictingVariableNamesForNew(
6464
ClassMethod | Function_ | Closure | ArrowFunction $functionLike
6565
): array {
6666
// cache it!
67-
$classMethodHash = spl_object_hash($functionLike);
67+
$classMethodId = spl_object_id($functionLike);
6868

69-
if (isset($this->conflictingVariableNamesByClassMethod[$classMethodHash])) {
70-
return $this->conflictingVariableNamesByClassMethod[$classMethodHash];
69+
if (isset($this->conflictingVariableNamesByClassMethod[$classMethodId])) {
70+
return $this->conflictingVariableNamesByClassMethod[$classMethodId];
7171
}
7272

7373
$paramNames = $this->functionLikeManipulator->resolveParamNames($functionLike);
@@ -77,7 +77,7 @@ private function resolveConflictingVariableNamesForNew(
7777
$protectedNames = array_merge($paramNames, $newAssignNames, $nonNewAssignNames);
7878

7979
$protectedNames = $this->arrayFilter->filterWithAtLeastTwoOccurences($protectedNames);
80-
$this->conflictingVariableNamesByClassMethod[$classMethodHash] = $protectedNames;
80+
$this->conflictingVariableNamesByClassMethod[$classMethodId] = $protectedNames;
8181

8282
return $protectedNames;
8383
}

rules/Naming/Naming/OverridenExistingNamesResolver.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
final class OverridenExistingNamesResolver
1818
{
1919
/**
20-
* @var array<string, array<int, string>>
20+
* @var array<int, array<int, string>>
2121
*/
2222
private array $overridenExistingVariableNamesByClassMethod = [];
2323

@@ -65,10 +65,10 @@ public function hasNameInFunctionLikeForParam(
6565
*/
6666
private function resolveOveriddenNamesForNew(ClassMethod | Function_ | Closure $functionLike): array
6767
{
68-
$classMethodHash = spl_object_hash($functionLike);
68+
$classMethodId = spl_object_id($functionLike);
6969

70-
if (isset($this->overridenExistingVariableNamesByClassMethod[$classMethodHash])) {
71-
return $this->overridenExistingVariableNamesByClassMethod[$classMethodHash];
70+
if (isset($this->overridenExistingVariableNamesByClassMethod[$classMethodId])) {
71+
return $this->overridenExistingVariableNamesByClassMethod[$classMethodId];
7272
}
7373

7474
$currentlyUsedNames = [];
@@ -90,7 +90,7 @@ private function resolveOveriddenNamesForNew(ClassMethod | Function_ | Closure $
9090
$currentlyUsedNames = array_values($currentlyUsedNames);
9191
$currentlyUsedNames = $this->arrayFilter->filterWithAtLeastTwoOccurences($currentlyUsedNames);
9292

93-
$this->overridenExistingVariableNamesByClassMethod[$classMethodHash] = $currentlyUsedNames;
93+
$this->overridenExistingVariableNamesByClassMethod[$classMethodId] = $currentlyUsedNames;
9494

9595
return $currentlyUsedNames;
9696
}

src/NodeManipulator/ClassMethodAssignManipulator.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
final class ClassMethodAssignManipulator
1515
{
1616
/**
17-
* @var array<string, string[]>
17+
* @var array<int, string[]>
1818
*/
1919
private array $alreadyAddedClassMethodNames = [];
2020

@@ -37,8 +37,8 @@ public function addParameterAndAssignToMethod(
3737
$classMethod->params[] = $this->nodeFactory->createParamFromNameAndType($name, $type);
3838
$classMethod->stmts[] = new Expression($assign);
3939

40-
$classMethodHash = spl_object_hash($classMethod);
41-
$this->alreadyAddedClassMethodNames[$classMethodHash][] = $name;
40+
$classMethodId = spl_object_id($classMethod);
41+
$this->alreadyAddedClassMethodNames[$classMethodId][] = $name;
4242
}
4343

4444
private function hasMethodParameter(ClassMethod $classMethod, string $name): bool
@@ -49,11 +49,11 @@ private function hasMethodParameter(ClassMethod $classMethod, string $name): boo
4949
}
5050
}
5151

52-
$classMethodHash = spl_object_hash($classMethod);
53-
if (! isset($this->alreadyAddedClassMethodNames[$classMethodHash])) {
52+
$classMethodId = spl_object_id($classMethod);
53+
if (! isset($this->alreadyAddedClassMethodNames[$classMethodId])) {
5454
return false;
5555
}
5656

57-
return in_array($name, $this->alreadyAddedClassMethodNames[$classMethodHash], true);
57+
return in_array($name, $this->alreadyAddedClassMethodNames[$classMethodId], true);
5858
}
5959
}

src/Rector/AbstractRector.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,15 @@ abstract class AbstractRector extends NodeVisitorAbstract implements RectorInter
8282
private CurrentFileProvider $currentFileProvider;
8383

8484
/**
85-
* @var array<string, Node[]>
85+
* @var array<int, Node[]>
8686
*/
8787
private array $nodesToReturn = [];
8888

8989
private CreatedByRuleDecorator $createdByRuleDecorator;
9090

9191
private RectorOutput $rectorOutput;
9292

93-
private ?string $toBeRemovedNodeHash = null;
93+
private ?int $toBeRemovedNodeId = null;
9494

9595
public function autowire(
9696
NodeNameResolver $nodeNameResolver,
@@ -183,7 +183,7 @@ final public function enterNode(Node $node): int|Node|null
183183

184184
// @see NodeTraverser::* codes, e.g. removal of node of stopping the traversing
185185
if ($refactoredNode === NodeTraverser::REMOVE_NODE) {
186-
$this->toBeRemovedNodeHash = spl_object_hash($originalNode);
186+
$this->toBeRemovedNodeId = spl_object_id($originalNode);
187187

188188
// notify this rule changing code
189189
$rectorWithLineChange = new RectorWithLineChange(static::class, $originalNode->getLine());
@@ -234,14 +234,14 @@ public function leaveNode(Node $node): array|int|Node|null
234234
return null;
235235
}
236236

237-
$objectHash = spl_object_hash($node);
238-
if ($this->toBeRemovedNodeHash === $objectHash) {
239-
$this->toBeRemovedNodeHash = null;
237+
$objectId = spl_object_id($node);
238+
if ($this->toBeRemovedNodeId === $objectId) {
239+
$this->toBeRemovedNodeId = null;
240240

241241
return NodeTraverser::REMOVE_NODE;
242242
}
243243

244-
return $this->nodesToReturn[$objectHash] ?? $node;
244+
return $this->nodesToReturn[$objectId] ?? $node;
245245
}
246246

247247
protected function isName(Node $node, string $name): bool
@@ -349,10 +349,10 @@ private function postRefactorProcess(
349349
$this->refreshScopeNodes($refactoredNode, $filePath, $currentScope);
350350

351351
// search "infinite recursion" in https://github.com/nikic/PHP-Parser/blob/master/doc/component/Walking_the_AST.markdown
352-
$originalNodeHash = spl_object_hash($originalNode);
352+
$originalNodeId = spl_object_id($originalNode);
353353

354354
// will be replaced in leaveNode() the original node must be passed
355-
$this->nodesToReturn[$originalNodeHash] = $refactoredNode;
355+
$this->nodesToReturn[$originalNodeId] = $refactoredNode;
356356

357357
return $originalNode;
358358
}

0 commit comments

Comments
 (0)
0