mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-20 08:05:29 +01:00
Apply HelperFunctionToConstructorInjectionRector only in non-st… (#2502)
Apply HelperFunctionToConstructorInjectionRector only in non-static class method scope
This commit is contained in:
commit
71bcf699ae
@ -223,12 +223,13 @@ PHP
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
// we can inject only in class context
|
||||
$classNode = $node->getAttribute(AttributeKey::CLASS_NODE);
|
||||
if (! $classNode instanceof Class_) {
|
||||
if ($this->shouldSkipFuncCall($node)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/** @var Class_ $classNode */
|
||||
$classNode = $node->getAttribute(AttributeKey::CLASS_NODE);
|
||||
|
||||
foreach ($this->functionToService as $function => $service) {
|
||||
if (! $this->isName($node, $function)) {
|
||||
continue;
|
||||
@ -262,4 +263,25 @@ PHP
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private function shouldSkipFuncCall(FuncCall $funcCall): bool
|
||||
{
|
||||
// we can inject only in class context
|
||||
$classNode = $funcCall->getAttribute(AttributeKey::CLASS_NODE);
|
||||
if (! $classNode instanceof Class_) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/** @var Node\Stmt\ClassMethod|null $classMethod */
|
||||
$classMethod = $funcCall->getAttribute(AttributeKey::METHOD_NODE);
|
||||
if ($classMethod === null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($classMethod->isStatic()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\Laravel\Tests\Rector\FuncCall\HelperFunctionToConstructorInjectionRector\Fixture;
|
||||
|
||||
class SkipStaticMethod
|
||||
{
|
||||
public static function go()
|
||||
{
|
||||
$value = config('value');
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user