Skip to content

Commit

Permalink
Updated Rector to commit 5e29a35bb60f92bece6fede8080645e7ba7fc421
Browse files Browse the repository at this point in the history
rectorphp/rector-src@5e29a35 Performance: Reduce isObjectType calls (#3502)
  • Loading branch information
TomasVotruba committed Mar 22, 2023
1 parent 6438137 commit b5afb12
Show file tree
Hide file tree
Showing 58 changed files with 143 additions and 136 deletions.
9 changes: 8 additions & 1 deletion packages/NodeTypeResolver/NodeTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ final class NodeTypeResolver
* @var array<class-string<Node>, NodeTypeResolverInterface>
*/
private $nodeTypeResolvers = [];
/**
* @var array<string, bool>
*/
private $traitExistsCache = [];
/**
* @readonly
* @var \Rector\TypeDeclaration\PHPStan\ObjectTypeSpecifier
Expand Down Expand Up @@ -327,7 +331,10 @@ private function isObjectTypeOfObjectType(ObjectType $resolvedObjectType, Object
return \false;
}
$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 @@ -111,10 +111,10 @@ public function refactor(Node $node)
{
$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;
}
$this->processPositionWithDefaultValues($node, $addedArgument);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ public function refactor(Node $node)
{
$hasChanged = \false;
foreach ($this->replacedArguments as $replacedArgument) {
if (!$this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType($node, $replacedArgument->getObjectType())) {
if (!$this->isName($node->name, $replacedArgument->getMethod())) {
continue;
}
if (!$this->isName($node->name, $replacedArgument->getMethod())) {
if (!$this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType($node, $replacedArgument->getObjectType())) {
continue;
}
$replacedNode = $this->argumentDefaultValueReplacer->processReplaces($node, $replacedArgument);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ 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;
}
$arrayNode = $this->collectReturnArrayNodesFromClassMethod($node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,16 @@ 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;
}
if ($this->shouldSkipMethod($node, $method)) {
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 @@ -59,10 +59,10 @@ public function refactor(Node $node) : ?Node
if (!$node->args[0] instanceof Arg) {
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;
}
return new BooleanNot($node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,10 @@ 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 @@ -194,10 +194,10 @@ private function refactorReflectionParameterGetName(MethodCall $methodCall) : Te
}
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'));
}
/**
* @return \PhpParser\Node|\PhpParser\Node\Expr\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 @@ -47,10 +47,10 @@ 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;
}
if (!\array_key_exists(1, $node->args)) {
Expand Down
4 changes: 2 additions & 2 deletions rules/Removing/Rector/ClassMethod/ArgumentRemoverRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ public function refactor(Node $node)
{
$this->hasChanged = \false;
foreach ($this->removedArguments as $removedArgument) {
if (!$this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType($node, $removedArgument->getObjectType())) {
if (!$this->isName($node->name, $removedArgument->getMethod())) {
continue;
}
if (!$this->isName($node->name, $removedArgument->getMethod())) {
if (!$this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType($node, $removedArgument->getObjectType())) {
continue;
}
$this->processPosition($node, $removedArgument);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ 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;
}
if ($renameClassConstFetch instanceof RenameClassAndConstFetch) {
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 @@ -93,13 +93,13 @@ private function processFromPropertyFetch(PropertyFetch $propertyFetch) : ?Prope
{
$class = $this->betterNodeFinder->findParentType($propertyFetch, Class_::class);
foreach ($this->renamedProperties as $renamedProperty) {
if (!$this->isObjectType($propertyFetch->var, $renamedProperty->getObjectType())) {
continue;
}
$oldProperty = $renamedProperty->getOldProperty();
if (!$this->isName($propertyFetch, $oldProperty)) {
continue;
}
if (!$this->isObjectType($propertyFetch->var, $renamedProperty->getObjectType())) {
continue;
}
$nodeVarType = $this->nodeTypeResolver->getType($propertyFetch->var);
if ($nodeVarType instanceof ThisType && $class instanceof ClassLike) {
$this->renameProperty($class, $renamedProperty);
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 @@ -39,10 +39,10 @@ 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;
}
return $this->rename($node, $staticMethodRename);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ public function refactor(Node $node) : ?Node
/** @var Variable $propertyNode */
$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;
}
return $this->nodeFactory->createMethodCall($propertyNode, $propertyAssignToMethodCall->getNewMethodName(), [$node->expr]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ 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;
}
return $propertyToMethodCall;
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 @@ -58,10 +58,10 @@ 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;
}
if ($node->stmts === null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ 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;
}
$node->name = new Identifier($methodCallRenameWithAddedArgument->getNewMethod());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,10 @@ private function isMatch(MethodCall $methodCall, MethodCallToMethodCall $methodC
}
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 @@ -69,10 +69,10 @@ 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;
}
return $this->nodeFactory->createStaticCall($methodCallToStaticCall->getNewClass(), $methodCallToStaticCall->getNewMethod(), $node->args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ 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;
}
$node->var = $this->nodeFactory->createPropertyFetch('this', $parentCallToProperty->getProperty());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ 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;
}
return new FuncCall(new FullyQualified($staticCallToFunction->getFunction()), $node->args);
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 @@ -76,10 +76,10 @@ 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;
}
$methodCall->name = new Identifier($methodName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ public function refactor(Node $node) : ?Node
/** @var ClassLike $classLike */
$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;
}
$this->refactorClassMethodWithTypehintByParameterPosition($node, $addParamTypeDeclaration);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ 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;
}
$this->processClassMethodNodeWithTypehints($node, $methodReturnType->getReturnType(), $objectType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ 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;
}
$this->visibilityManipulator->changeNodeVisibility($node, $classConstantVisibilityChange->getVisibility());
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 = 'b411755c297647bf0350c849e35b63c0de5f20b2';
public const PACKAGE_VERSION = '5e29a35bb60f92bece6fede8080645e7ba7fc421';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-03-22 17:14:03';
public const RELEASE_DATE = '2023-03-22 21:16:27';
/**
* @var int
*/
Expand Down
2 changes: 1 addition & 1 deletion vendor/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@

require_once __DIR__ . '/composer/autoload_real.php';

return ComposerAutoloaderInita3d0e2cf6ad3b4fd50a70f178131d9af::getLoader();
return ComposerAutoloaderInit6557d86d25b72b29ae6a1db2d469e8bf::getLoader();
Loading

0 comments on commit b5afb12

Please sign in to comment.