Skip to content

Commit 78e99a4

Browse files
authored
[Php70] Skip rewriting to $this on static method or static closure on StaticCallOnNonStaticToInstanceCallRector (#8026)
* [Php70] Fix rewriting to $this on static method or static closure * fix * Fix * update static closure prove * fix
1 parent aab2092 commit 78e99a4

3 files changed

Lines changed: 40 additions & 0 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php70\Rector\StaticCall\StaticCallOnNonStaticToInstanceCallRector\Fixture;
4+
5+
class SkipStaticClosureSameClassCall
6+
{
7+
public function boot(): void
8+
{
9+
// static closure — no $this, must not rewrite
10+
$callback = static function (): void {
11+
SkipStaticClosureSameClassCall::doWork();
12+
};
13+
}
14+
15+
public function doWork(): void
16+
{
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php70\Rector\StaticCall\StaticCallOnNonStaticToInstanceCallRector\Fixture;
4+
5+
class SkipStaticMethodSameClassCall
6+
{
7+
public static function run(): string
8+
{
9+
// enclosing function is static — no $this, must not rewrite
10+
return SkipStaticMethodSameClassCall::doWork();
11+
}
12+
13+
public function doWork(): string
14+
{
15+
return 'work done';
16+
}
17+
}

‎rules/Php70/Rector/StaticCall/StaticCallOnNonStaticToInstanceCallRector.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ public function refactor(Node $node): ?Node
126126

127127
$classReflection = $scope->getClassReflection();
128128
if ($classReflection instanceof ClassReflection && $classReflection->getName() === $className) {
129+
// detect any scope where $this is unavailable or possibly unavailable
130+
if (! $scope->hasVariableType('this')->yes()) {
131+
return null;
132+
}
133+
129134
return new MethodCall(new Variable('this'), $node->name, $node->args);
130135
}
131136

0 commit comments

Comments
 (0)