Skip to content

Commit

Permalink
[Defluent] Skip interface on DefluentReturnMethodCallRector (#584)
Browse files Browse the repository at this point in the history
Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Aug 3, 2021
1 parent 0515b03 commit c1b12be
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Defluent\Rector\Return_\DefluentReturnMethodCallRector\Source;

interface MessageInterface
{
public function withStatus($status): self;
}
23 changes: 18 additions & 5 deletions rules/Defluent/NodeAnalyzer/FluentChainMethodCallNodeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
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;
Expand Down Expand Up @@ -85,14 +86,20 @@ public function isFluentClassMethodOfMethodCall(MethodCall $methodCall): bool
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);
}

Expand Down Expand Up @@ -235,6 +242,12 @@ public function isMethodCallReturnThis(MethodCall $methodCall): bool
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);
Expand Down

0 comments on commit c1b12be

Please sign in to comment.