[Defluent] Skip interface on DefluentReturnMethodCallRector (#584)

Co-authored-by: GitHub Action <action@github.com>
This commit is contained in:
Abdul Malik Ikhsan 2021-08-03 22:19:26 +07:00 committed by GitHub
parent 0515b036a8
commit c1b12be00d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 5 deletions

View File

@ -0,0 +1,13 @@
<?php
namespace Rector\Tests\Defluent\Rector\Return_\DefluentReturnMethodCallRector\Fixture;
use Rector\Tests\Defluent\Rector\Return_\DefluentReturnMethodCallRector\Source\MessageInterface;
final class SkipInterface
{
public function run(MessageInterface $message)
{
return $message->withStatus(500);
}
}

View File

@ -0,0 +1,10 @@
<?php
declare(strict_types=1);
namespace Rector\Tests\Defluent\Rector\Return_\DefluentReturnMethodCallRector\Source;
interface MessageInterface
{
public function withStatus($status): self;
}

View File

@ -13,6 +13,7 @@ use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Interface_;
use PhpParser\Node\Stmt\Return_;
use PhpParser\NodeFinder;
use PHPStan\Analyser\MutatingScope;
@ -85,14 +86,20 @@ final class FluentChainMethodCallNodeAnalyzer
return false;
}
if ($calleeStaticType instanceof ObjectType) {
foreach (self::KNOWN_FACTORY_FLUENT_TYPES as $knownFactoryFluentType) {
if ($calleeStaticType->isInstanceOf($knownFactoryFluentType)->yes()) {
return false;
}
if (! $calleeStaticType instanceof ObjectType) {
return false;
}
foreach (self::KNOWN_FACTORY_FLUENT_TYPES as $knownFactoryFluentType) {
if ($calleeStaticType->isInstanceOf($knownFactoryFluentType)->yes()) {
return false;
}
}
if ($this->isInterface($calleeStaticType)) {
return false;
}
return ! $this->isMethodCallCreatingNewInstance($methodCall);
}
@ -235,6 +242,12 @@ final class FluentChainMethodCallNodeAnalyzer
return $inferFunctionLike instanceof ThisType;
}
private function isInterface(ObjectType $objectType): bool
{
$classLike = $this->astResolver->resolveClassFromObjectType($objectType);
return $classLike instanceof Interface_;
}
private function isMethodCallCreatingNewInstance(MethodCall $methodCall): bool
{
$classMethod = $this->astResolver->resolveClassMethodFromMethodCall($methodCall);