Skip to content
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,41 @@
<?php

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

use PHPUnit\Framework\TestCase;

final class SingleStmtVoidCallback extends TestCase
{
public function test()
{
$builder = $this->getMockBuilder('AnyType')->getMock();

$builder->expects($this->once())
->method('queueWebhooksByType')
->with($this->callback(function ($type): void {
$this->assertSame($type, 'some_value');
}));
}
}

?>
-----
<?php

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

use PHPUnit\Framework\TestCase;

final class SingleStmtVoidCallback extends TestCase
{
public function test()
{
$builder = $this->getMockBuilder('AnyType')->getMock();

$builder->expects($this->once())
->method('queueWebhooksByType')
->with($this->equalTo('some_value'));
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,20 @@ private function matchCallbackSoleAssertSameExpected(Expr $expr): ?Expr
}
}

// exactly assertSame() expression + "return true;"
// sole assertSame() expression, optionally followed by "return true;"
$closureStmts = $innerClosure->getStmts();
if (count($closureStmts) !== 2) {
if (count($closureStmts) === 2) {
[$firstStmt, $secondStmt] = $closureStmts;

if (! $secondStmt instanceof Return_ || ! $this->isTrueReturn($secondStmt)) {
return null;
}
} elseif (count($closureStmts) === 1) {
$firstStmt = $closureStmts[0];
} else {
return null;
}

[$firstStmt, $secondStmt] = $closureStmts;

if (! $firstStmt instanceof Expression) {
return null;
}
Expand All @@ -150,10 +156,6 @@ private function matchCallbackSoleAssertSameExpected(Expr $expr): ?Expr
return null;
}

if (! $secondStmt instanceof Return_ || ! $this->isTrueReturn($secondStmt)) {
return null;
}

$assertSameArgs = $firstStmtExpr->getArgs();
if (count($assertSameArgs) !== 2) {
return null;
Expand Down
Loading