10000 [Renaming] Handle rename caller on trait on RenameMethodRector (#6944) · rectorphp/rector-src@728b284 · GitHub
[go: up one dir, main page]

Skip to content

Commit 728b284

Browse files
authored
[Renaming] Handle rename caller on trait on RenameMethodRector (#6944)
1 parent 30ecb36 commit 728b284

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Rector\Tests\Renaming\Rector\MethodCall\RenameMethodRector\Fixture;
4+
5+
trait RenameTraitMethodCall
6+
{
7+
public function method1()
8+
{
9+
}
10+
11+
public function method2()
12+
{
13+
$this->method1();
14+
}
15+
16+
public function withStatic()
17+
{
18+
self::method1();
19+
static::method1();
20+
}
21+
}
22+
23+
?>
24+
-----
25+
<?php
26+
27+
namespace Rector\Tests\Renaming\Rector\MethodCall\RenameMethodRector\Fixture;
28+
29+
trait RenameTraitMethodCall
30+
{
31+
public function method3()
32+
{
33+
}
34+
35+
public function method2()
36+
{
37+
$this->method3();
38+
}
39+
40+
public function withStatic()
41+
{
42+
self::method3();
43+
static::method3();
44+
}
45+
}
46+
47+
?>

rules-tests/Renaming/Rector/MethodCall/RenameMethodRector/config/configured_rule.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Rector\Renaming\Rector\MethodCall\RenameMethodRector;
77
use Rector\Renaming\ValueObject\MethodCallRename;
88
use Rector\Renaming\ValueObject\MethodCallRenameWithArrayKey;
9+
use Rector\Tests\Renaming\Rector\MethodCall\RenameMethodRector\Fixture\RenameTraitMethodCall;
910
use Rector\Tests\Renaming\Rector\MethodCall\RenameMethodRector\Fixture\RenameTraitMethod;
1011
use Rector\Tests\Renaming\Rector\MethodCall\RenameMethodRector\Source\AbstractType;
1112
use Rector\Tests\Renaming\Rector\MethodCall\RenameMethodRector\Source\CustomType;
@@ -39,5 +40,6 @@
3940
new MethodCallRename(SomeEnumWithMethod::class, 'oldEnumMethod', 'newEnumMethod'),
4041
new MethodCallRename(SomeTrait::class, '_test', 'test'),
4142
new MethodCallRename(RenameTraitMethod::class, 'run', 'execute'),
43+
new MethodCallRename(RenameTraitMethodCall::class, 'method1', 'method3'),
4244
]);
4345
};

src/NodeTypeResolver/NodeTypeResolver.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use PhpParser\Node\Expr\NullsafeMethodCall;
1616
use PhpParser\Node\Expr\StaticCall;
1717
use PhpParser\Node\Expr\Ternary;
18+
use PhpParser\Node\Expr\Variable;
1819
use PhpParser\Node\Name;
1920
use PhpParser\Node\Name\FullyQualified;
2021
use PhpParser\Node\NullableType;
@@ -32,6 +33,7 @@
3233
use PHPStan\Type\Constant\ConstantArrayType;
3334
use PHPStan\Type\Constant\ConstantBooleanType;
3435
use PHPStan\Type\Constant\ConstantStringType;
36+
use PHPStan\Type\ErrorType;
3537
use PHPStan\Type\MixedType;
3638
use PHPStan\Type\NeverType;
3739
use PHPStan\Type\NullType;
@@ -119,6 +121,27 @@ public function isObjectType(Node $node, ObjectType $requiredObjectType): bool
119121
}
120122

121123
$resolvedType = $this->getType($node);
124+
125+
// cover call $this on trait
126+
if ($resolvedType instanceof ErrorType && ($node instanceof Variable && $this->nodeNameResolver->isName(
127+
$node,
128+
'this'
129+
))) {
130+
$scope = $node->getAttribute(AttributeKey::SCOPE);
131+
if (! $scope instanceof Scope) {
132+
return false;
133+
}
134+
135+
$classReflection = $scope->getClassReflection();
136+
if (! $classReflection instanceof ClassReflection) {
137+
return false;
138+
}
139+
140+
if ($classReflection->isTrait()) {
141+
$resolvedType = new ObjectType($classReflection->getName());
142+
}
143+
}
144+
122145
if ($resolvedType instanceof MixedType) {
123146
return false;
124147
}

0 commit comments

Comments
 (0)
0