Skip to content

Commit a1ebd0b

Browse files
committed
[CodeQuality] Skip with() multiple arguments in VoidMethodWithCallbackToWillReturnCallbackRector
1 parent 7e13bfb commit a1ebd0b

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\VoidMethodWithCallbackToWillReturnCallbackRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\VoidMethodWithCallbackToWillReturnCallbackRector\Source\SomeEntityManager;
7+
8+
final class SkipMultipleWithArguments extends TestCase
9+
{
10+
public function test(): void
11+
{
12+
$this->createMock(SomeEntityManager::class)
13+
->method('persist')
14+
->with(
15+
$this->callback(function ($entity): bool {
16+
$this->assertInstanceOf(\stdClass::class, $entity);
17+
18+
return true;
19+
}),
20+
false
21+
);
22+
}
23+
}

rules/CodeQuality/Rector/Class_/VoidMethodWithCallbackToWillReturnCallbackRector.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,14 @@ public function refactor(Node $node): ?Class_
180180

181181
private function matchCallbackClosure(MethodCall $withMethodCall): ?Closure
182182
{
183-
$withFirstArg = $withMethodCall->getArgs()[0] ?? null;
183+
$withArgs = $withMethodCall->getArgs();
184+
185+
// a 2nd+ ->with() argument matches a further parameter, keep the callback matcher as is
186+
if (count($withArgs) > 1) {
187+
return null;
188+
}
189+
190+
$withFirstArg = $withArgs[0] ?? null;
184191
if (! $withFirstArg instanceof Arg) {
185192
return null;
186193
}

0 commit comments

Comments
 (0)