Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tv with consecutive callable single 4 #377

Merged
merged 3 commits into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\SingleWithConsecutiveToWithRector\Fixture;

use PHPUnit\Framework\TestCase;
use Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\SingleWithConsecutiveToWithRector\Source\SomeMockedClass;

final class SimplifyEqualTo extends TestCase
{
public function test()
{
$someServiceMock = $this->createMock(SomeMockedClass::class);
$someServiceMock->expects($this->exactly(3))
->method('prepare')
->withConsecutive(
[$this->equalTo(1)],
);
}
}

?>
-----
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\SingleWithConsecutiveToWithRector\Fixture;

use PHPUnit\Framework\TestCase;
use Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\SingleWithConsecutiveToWithRector\Source\SomeMockedClass;

final class SimplifyEqualTo extends TestCase
{
public function test()
{
$someServiceMock = $this->createMock(SomeMockedClass::class);
$someServiceMock->expects($this->exactly(3))
->method('prepare')
->with(
1,
);
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ final class WithAssertInside extends TestCase
{
$someServiceMock = $this->createMock(SomeMockedClass::class);
$someServiceMock->expects($this->exactly(3))
->method('prepare')->with($this->equalTo('first'), $this->equalTo('second'));
->method('prepare')->with('first', 'second');
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\Match_;
use PhpParser\Node\Expr\MethodCall;
Expand Down Expand Up @@ -122,7 +123,7 @@ public function refactor(Node $node): MethodCall|null
// we look for $this->assertSame(...)
$expectedExpr = $matchAndReturnMatch->getConsecutiveMatchExpr();

if ($expectedExpr instanceof Expr\Array_) {
if ($expectedExpr instanceof Array_) {
$args = $this->nodeFactory->createArgs($expectedExpr->items);
} else {
$args = [new Arg($expectedExpr)];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,20 @@ function (Node $node): bool {
}
);

// replace $this->equalsTo() with direct value
$this->traverseNodesWithCallable($firstArg->value, function (Node $node): ?Node {
if (! $node instanceof MethodCall) {
return null;
}

if (! $this->isName($node->name, 'equalTo')) {
return null;
}

return $node->getArgs()[0]
->value;
});

if ($hasAssertInside && $firstArg->value instanceof Array_) {
$args = $this->nodeFactory->createArgs($firstArg->value->items);
} else {
Expand Down