mirror of
https://github.com/rectorphp/rector.git
synced 2024-05-31 08:20:53 +00:00
[Defluent] Skip interface on DefluentReturnMethodCallRector (#584)
Co-authored-by: GitHub Action <action@github.com>
This commit is contained in:
parent
0515b036a8
commit
c1b12be00d
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Defluent\Rector\Return_\DefluentReturnMethodCallRector\Source;
|
||||
|
||||
interface MessageInterface
|
||||
{
|
||||
public function withStatus($status): self;
|
||||
}
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue
Block a user