Skip to content

Commit

Permalink
Performance: Reduce isObjectType calls (#3502)
Browse files Browse the repository at this point in the history
  • Loading branch information
keulinho committed Mar 22, 2023
1 parent b411755 commit 5e29a35
Show file tree
Hide file tree
Showing 24 changed files with 66 additions and 57 deletions.
11 changes: 10 additions & 1 deletion packages/NodeTypeResolver/NodeTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ final class NodeTypeResolver
*/
private array $nodeTypeResolvers = [];

/**
* @var array<string, bool>
*/
private array $traitExistsCache = [];

/**
* @param NodeTypeResolverInterface[] $nodeTypeResolvers
*/
Expand Down Expand Up @@ -350,7 +355,11 @@ private function isObjectTypeOfObjectType(ObjectType $resolvedObjectType, Object
}

$classReflection = $this->reflectionProvider->getClass($resolvedObjectType->getClassName());
if (\trait_exists($requiredObjectType->getClassName())) {
if (!isset($this->traitExistsCache[$classReflection->getName()])) {
$this->traitExistsCache[$classReflection->getName()] = \trait_exists($requiredObjectType->getClassName());
}

if ($this->traitExistsCache[$classReflection->getName()]) {
foreach ($classReflection->getAncestors() as $ancestorClassReflection) {
if ($ancestorClassReflection->hasTraitUse($requiredObjectType->getClassName())) {
return true;
Expand Down
4 changes: 2 additions & 2 deletions rules/Arguments/Rector/ClassMethod/ArgumentAdderRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ public function refactor(Node $node): MethodCall | StaticCall | ClassMethod | nu
$this->haveArgumentsChanged = false;

foreach ($this->addedArguments as $addedArgument) {
if (! $this->isObjectTypeMatch($node, $addedArgument->getObjectType())) {
if (! $this->isName($node->name, $addedArgument->getMethod())) {
continue;
}

if (! $this->isName($node->name, $addedArgument->getMethod())) {
if (! $this->isObjectTypeMatch($node, $addedArgument->getObjectType())) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,17 @@ public function refactor(Node $node): MethodCall | StaticCall | ClassMethod | nu
$hasChanged = false;

foreach ($this->replacedArguments as $replacedArgument) {
if (! $this->isName($node->name, $replacedArgument->getMethod())) {
continue;
}

if (! $this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType(
$node,
$replacedArgument->getObjectType()
)) {
continue;
}

if (! $this->isName($node->name, $replacedArgument->getMethod())) {
continue;
}

$replacedNode = $this->argumentDefaultValueReplacer->processReplaces($node, $replacedArgument);
if ($replacedNode instanceof Node) {
$hasChanged = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ public function refactor(Node $node): ?Node
{
$hasChanged = false;
foreach ($this->methodsToYields as $methodToYield) {
if (! $this->isObjectType($node, $methodToYield->getObjectType())) {
if (! $this->isName($node, $methodToYield->getMethod())) {
continue;
}

if (! $this->isName($node, $methodToYield->getMethod())) {
if (! $this->isObjectType($node, $methodToYield->getObjectType())) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,6 @@ public function refactor(Node $node): ?Node
$className = (string) $this->nodeNameResolver->getName($classLike);

foreach ($this->methodByParentTypes as $type => $method) {
if (! $this->isObjectType($classLike, new ObjectType($type))) {
continue;
}

// not itself
if ($className === $type) {
continue;
Expand All @@ -98,6 +94,10 @@ public function refactor(Node $node): ?Node
continue;
}

if (! $this->isObjectType($classLike, new ObjectType($type))) {
continue;
}

$node->stmts[] = $this->createParentStaticCall($method);

return $node;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ public function refactor(Node $node): ?Node
return null;
}

if (! $this->isObjectType($node->args[0]->value, $incompleteClassObjectType)) {
if ($this->shouldSkip($node)) {
return null;
}

if ($this->shouldSkip($node)) {
if (! $this->isObjectType($node->args[0]->value, $incompleteClassObjectType)) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,11 @@ private function shouldSkipMethodCall(MethodCall $methodCall): bool

private function isReflectionParameterGetTypeMethodCall(MethodCall $methodCall): bool
{
if (! $this->isObjectType($methodCall->var, new ObjectType('ReflectionParameter'))) {
if (! $this->isName($methodCall->name, 'getType')) {
return false;
}

return $this->isName($methodCall->name, 'getType');
return $this->isObjectType($methodCall->var, new ObjectType('ReflectionParameter'));
}

private function refactorReflectionParameterGetName(MethodCall $methodCall): Ternary
Expand All @@ -236,11 +236,11 @@ private function refactorReflectionParameterGetName(MethodCall $methodCall): Ter

private function isReflectionFunctionAbstractGetReturnTypeMethodCall(MethodCall $methodCall): bool
{
if (! $this->isObjectType($methodCall->var, new ObjectType('ReflectionFunctionAbstract'))) {
if (! $this->isName($methodCall->name, 'getReturnType')) {
return false;
}

return $this->isName($methodCall->name, 'getReturnType');
return $this->isObjectType($methodCall->var, new ObjectType('ReflectionFunctionAbstract'));
}

private function refactorReflectionFunctionGetReturnType(MethodCall $methodCall): Node | Ternary
Expand Down
4 changes: 2 additions & 2 deletions rules/Php82/Rector/New_/FilesystemIteratorSkipDotsRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?New_
{
if (! $this->isObjectType($node->class, new ObjectType('FilesystemIterator'))) {
if ($node->isFirstClassCallable()) {
return null;
}

if ($node->isFirstClassCallable()) {
if (! $this->isObjectType($node->class, new ObjectType('FilesystemIterator'))) {
return null;
}

Expand Down
8 changes: 4 additions & 4 deletions rules/Removing/Rector/ClassMethod/ArgumentRemoverRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,17 @@ public function refactor(Node $node): MethodCall | StaticCall | ClassMethod | nu
$this->hasChanged = false;

foreach ($this->removedArguments as $removedArgument) {
if (! $this->isName($node->name, $removedArgument->getMethod())) {
continue;
}

if (! $this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType(
$node,
$removedArgument->getObjectType()
)) {
continue;
}

if (! $this->isName($node->name, $removedArgument->getMethod())) {
continue;
}

$this->processPosition($node, $removedArgument);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ public function getNodeTypes(): array
public function refactor(Node $node): ?ClassConstFetch
{
foreach ($this->renameClassConstFetches as $renameClassConstFetch) {
if (! $this->isObjectType($node->class, $renameClassConstFetch->getOldObjectType())) {
if (! $this->isName($node->name, $renameClassConstFetch->getOldConstant())) {
continue;
}

if (! $this->isName($node->name, $renameClassConstFetch->getOldConstant())) {
if (! $this->isObjectType($node->class, $renameClassConstFetch->getOldObjectType())) {
continue;
}

Expand Down
6 changes: 3 additions & 3 deletions rules/Renaming/Rector/PropertyFetch/RenamePropertyRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,12 @@ private function processFromPropertyFetch(PropertyFetch $propertyFetch): ?Proper
$class = $this->betterNodeFinder->findParentType($propertyFetch, Class_::class);

foreach ($this->renamedProperties as $renamedProperty) {
if (! $this->isObjectType($propertyFetch->var, $renamedProperty->getObjectType())) {
$oldProperty = $renamedProperty->getOldProperty();
if (! $this->isName($propertyFetch, $oldProperty)) {
continue;
}

$oldProperty = $renamedProperty->getOldProperty();
if (! $this->isName($propertyFetch, $oldProperty)) {
if (! $this->isObjectType($propertyFetch->var, $renamedProperty->getObjectType())) {
continue;
}

Expand Down
4 changes: 2 additions & 2 deletions rules/Renaming/Rector/StaticCall/RenameStaticMethodRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ public function getNodeTypes(): array
public function refactor(Node $node): ?Node
{
foreach ($this->staticMethodRenames as $staticMethodRename) {
if (! $this->isObjectType($node->class, $staticMethodRename->getOldObjectType())) {
if (! $this->isName($node->name, $staticMethodRename->getOldMethod())) {
continue;
}

if (! $this->isName($node->name, $staticMethodRename->getOldMethod())) {
if (! $this->isObjectType($node->class, $staticMethodRename->getOldObjectType())) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ public function refactor(Node $node): ?Node
$propertyNode = $propertyFetchNode->var;

foreach ($this->propertyAssignsToMethodCalls as $propertyAssignToMethodCall) {
if (! $this->isObjectType($propertyFetchNode->var, $propertyAssignToMethodCall->getObjectType())) {
if (! $this->isName($propertyFetchNode, $propertyAssignToMethodCall->getOldPropertyName())) {
continue;
}

if (! $this->isName($propertyFetchNode, $propertyAssignToMethodCall->getOldPropertyName())) {
if (! $this->isObjectType($propertyFetchNode->var, $propertyAssignToMethodCall->getObjectType())) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,11 @@ private function processGetter(PropertyFetch $propertyFetch): ?Node
private function matchPropertyFetchCandidate(PropertyFetch $propertyFetch): ?PropertyFetchToMethodCall
{
foreach ($this->propertiesToMethodCalls as $propertyToMethodCall) {
if (! $this->isObjectType($propertyFetch->var, $propertyToMethodCall->getOldObjectType())) {
if (! $this->isName($propertyFetch, $propertyToMethodCall->getOldProperty())) {
continue;
}

if (! $this->isName($propertyFetch, $propertyToMethodCall->getOldProperty())) {
if (! $this->isObjectType($propertyFetch->var, $propertyToMethodCall->getOldObjectType())) {
continue;
}

Expand Down
4 changes: 2 additions & 2 deletions rules/Transform/Rector/ClassMethod/WrapReturnRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ public function getNodeTypes(): array
public function refactor(Node $node): ?Node
{
foreach ($this->typeMethodWraps as $typeMethodWrap) {
if (! $this->isObjectType($node, $typeMethodWrap->getObjectType())) {
if (! $this->isName($node, $typeMethodWrap->getMethod())) {
continue;
}

if (! $this->isName($node, $typeMethodWrap->getMethod())) {
if (! $this->isObjectType($node, $typeMethodWrap->getObjectType())) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ public function getNodeTypes(): array
public function refactor(Node $node): ?Node
{
foreach ($this->methodCallRenamesWithAddedArguments as $methodCallRenameWithAddedArgument) {
if (! $this->isObjectType($node->var, $methodCallRenameWithAddedArgument->getObjectType())) {
if (! $this->isName($node->name, $methodCallRenameWithAddedArgument->getOldMethod())) {
continue;
}

if (! $this->isName($node->name, $methodCallRenameWithAddedArgument->getOldMethod())) {
if (! $this->isObjectType($node->var, $methodCallRenameWithAddedArgument->getObjectType())) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,11 @@ private function isMatch(
return $this->isName($methodCall->name, $methodCallToMethodCall->getOldMethod());
}

if (! $this->isObjectType($methodCall->var, $oldTypeObject)) {
if (! $this->isName($methodCall->name, $methodCallToMethodCall->getOldMethod())) {
return false;
}

return $this->isName($methodCall->name, $methodCallToMethodCall->getOldMethod());
return $this->isObjectType($methodCall->var, $oldTypeObject);
}

private function matchNewPropertyName(MethodCallToMethodCall $methodCallToMethodCall, Class_ $class): ?string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ public function getNodeTypes(): array
public function refactor(Node $node): ?Node
{
foreach ($this->methodCallsToStaticCalls as $methodCallToStaticCall) {
if (! $this->isObjectType($node->var, $methodCallToStaticCall->getOldObjectType())) {
if (! $this->isName($node->name, $methodCallToStaticCall->getOldMethod())) {
continue;
}

if (! $this->isName($node->name, $methodCallToStaticCall->getOldMethod())) {
if (! $this->isObjectType($node->var, $methodCallToStaticCall->getOldObjectType())) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ public function getNodeTypes(): array
public function refactor(Node $node): ?Node
{
foreach ($this->parentCallToProperties as $parentCallToProperty) {
if (! $this->isObjectType($node->var, $parentCallToProperty->getObjectType())) {
if (! $this->isName($node->name, $parentCallToProperty->getMethod())) {
continue;
}

if (! $this->isName($node->name, $parentCallToProperty->getMethod())) {
if (! $this->isObjectType($node->var, $parentCallToProperty->getObjectType())) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ public function getNodeTypes(): array
public function refactor(Node $node): ?Node
{
foreach ($this->staticCallsToFunctions as $staticCallToFunction) {
if (! $this->isObjectType($node->class, $staticCallToFunction->getObjectType())) {
if (! $this->isName($node->name, $staticCallToFunction->getMethod())) {
continue;
}

if (! $this->isName($node->name, $staticCallToFunction->getMethod())) {
if (! $this->isObjectType($node->class, $staticCallToFunction->getObjectType())) {
continue;
}

Expand Down
4 changes: 2 additions & 2 deletions rules/Transform/Rector/String_/ToStringToMethodCallRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ private function processStringNode(String_ $string): ?Node
private function processMethodCall(MethodCall $methodCall): ?Node
{
foreach ($this->methodNamesByType as $type => $methodName) {
if (! $this->isObjectType($methodCall->var, new ObjectType($type))) {
if (! $this->isName($methodCall->name, '__toString')) {
continue;
}

if (! $this->isName($methodCall->name, '__toString')) {
if (! $this->isObjectType($methodCall->var, new ObjectType($type))) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ public function refactor(Node $node): ?Node
$classLike = $this->betterNodeFinder->findParentType($node, ClassLike::class);

foreach ($this->addParamTypeDeclarations as $addParamTypeDeclaration) {
if (! $this->isObjectType($classLike, $addParamTypeDeclaration->getObjectType())) {
if (! $this->isName($node, $addParamTypeDeclaration->getMethodName())) {
continue;
}

if (! $this->isName($node, $addParamTypeDeclaration->getMethodName())) {
if (! $this->isObjectType($classLike, $addParamTypeDeclaration->getObjectType())) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ public function getNodeTypes(): array
public function refactor(Node $node): ?Node
{
foreach ($this->methodReturnTypes as $methodReturnType) {
$objectType = $methodReturnType->getObjectType();
if (! $this->isObjectType($node, $objectType)) {
if (! $this->isName($node, $methodReturnType->getMethod())) {
continue;
}

if (! $this->isName($node, $methodReturnType->getMethod())) {
$objectType = $methodReturnType->getObjectType();
if (! $this->isObjectType($node, $objectType)) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ public function getNodeTypes(): array
public function refactor(Node $node): ?Node
{
foreach ($this->classConstantVisibilityChanges as $classConstantVisibilityChange) {
if (! $this->isObjectType($node, $classConstantVisibilityChange->getObjectType())) {
if (! $this->isName($node, $classConstantVisibilityChange->getConstant())) {
continue;
}

if (! $this->isName($node, $classConstantVisibilityChange->getConstant())) {
if (! $this->isObjectType($node, $classConstantVisibilityChange->getObjectType())) {
continue;
}

Expand Down

0 comments on commit 5e29a35

Please sign in to comment.