Skip to content

Commit 42115f0

Browse files
fix: ContainerBindConcreteWithClosureOnlyRector Rule when the abstract is a variable (#386)
* Update ContainerBindConcreteWithClosureOnlyRector Rule Prevent changes when the abstract class is a variable. * Update ContainerBindConcreteWithClosureOnlyRector Unit test * Fix CS --------- Co-authored-by: Jeff Meinesz <[email protected]>
1 parent 7e07220 commit 42115f0

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/Rector/MethodCall/ContainerBindConcreteWithClosureOnlyRector.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PhpParser\Node\Const_;
77
use PhpParser\Node\Expr\Closure;
88
use PhpParser\Node\Expr\MethodCall;
9+
use PhpParser\Node\Expr\Variable;
910
use PHPStan\Type\ObjectType;
1011
use PHPStan\Type\Type;
1112
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
@@ -74,6 +75,10 @@ public function refactor(Node $node): ?MethodCall
7475
$classString = $node->getArgs()[0]->value;
7576
$concreteNode = $node->getArgs()[1]->value;
7677

78+
if ($classString instanceof Variable) {
79+
return null;
80+
}
81+
7782
if (! $concreteNode instanceof Closure) {
7883
return null;
7984
}

tests/Rector/MethodCall/ContainerBindConcreteWithClosureOnlyRector/Fixture/fixture.php.inc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ $app->singleton(SomeInterface::class, function () {
1919
return new SomeClass();
2020
});
2121

22+
foreach ([SomeClass::class] as $item) {
23+
$app->singleton($item, function () use ($item) {
24+
return new $item();
25+
});
26+
}
27+
2228
?>
2329
-----
2430
<?php
@@ -42,4 +48,10 @@ $app->singleton(function (): \RectorLaravel\Tests\Rector\MethodCall\ContainerBin
4248
return new SomeClass();
4349
});
4450

51+
foreach ([SomeClass::class] as $item) {
52+
$app->singleton($item, function () use ($item) {
53+
return new $item();
54+
});
55+
}
56+
4557
?>

0 commit comments

Comments
 (0)