Apply HelperFunctionToConstructorInjectionRector only in non-st… (#2502)

Apply HelperFunctionToConstructorInjectionRector only in non-static class method scope
This commit is contained in:
Tomas Votruba 2019-12-27 01:09:04 +01:00 committed by GitHub
commit 71bcf699ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 3 deletions

View File

@ -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;
}
}

View File

@ -0,0 +1,11 @@
<?php
namespace Rector\Laravel\Tests\Rector\FuncCall\HelperFunctionToConstructorInjectionRector\Fixture;
class SkipStaticMethod
{
public static function go()
{
$value = config('value');
}
}