diff --git a/rules-tests/CodeQuality/Rector/MethodCall/CallbackSingleAssertToSimplerRector/Fixture/single_stmt_void_callback.php.inc b/rules-tests/CodeQuality/Rector/MethodCall/CallbackSingleAssertToSimplerRector/Fixture/single_stmt_void_callback.php.inc new file mode 100644 index 00000000..51e4ac57 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/MethodCall/CallbackSingleAssertToSimplerRector/Fixture/single_stmt_void_callback.php.inc @@ -0,0 +1,41 @@ +getMockBuilder('AnyType')->getMock(); + + $builder->expects($this->once()) + ->method('queueWebhooksByType') + ->with($this->callback(function ($type): void { + $this->assertSame($type, 'some_value'); + })); + } +} + +?> +----- +getMockBuilder('AnyType')->getMock(); + + $builder->expects($this->once()) + ->method('queueWebhooksByType') + ->with($this->equalTo('some_value')); + } +} + +?> diff --git a/rules/CodeQuality/Rector/MethodCall/CallbackSingleAssertToSimplerRector.php b/rules/CodeQuality/Rector/MethodCall/CallbackSingleAssertToSimplerRector.php index 67fd6c7a..47ad4978 100644 --- a/rules/CodeQuality/Rector/MethodCall/CallbackSingleAssertToSimplerRector.php +++ b/rules/CodeQuality/Rector/MethodCall/CallbackSingleAssertToSimplerRector.php @@ -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; } @@ -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;