-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CodeQuality] Add NarrowSingleWillReturnCallbackRector
- Loading branch information
1 parent
daac9f0
commit da5cbac
Showing
5 changed files
with
258 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
...ality/Rector/MethodCall/NarrowSingleWillReturnCallbackRector/Fixture/single_match.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\NarrowSingleWillReturnCallbackRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
final class SingleMatch extends TestCase | ||
{ | ||
public function test() | ||
{ | ||
$matcher = $this->exactly(3); | ||
|
||
$someServiceMock = $this->createMock(SomeMockedClass::class); | ||
$someServiceMock->expects($matcher) | ||
->method('prepare') | ||
->willReturnCallback(function (...$parameters) use ($matcher) { | ||
match ($matcher->getInvocationCount()) { | ||
1 => $this->assertSame([1], $parameters), | ||
}; | ||
}); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\NarrowSingleWillReturnCallbackRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
final class SingleMatch extends TestCase | ||
{ | ||
public function test() | ||
{ | ||
$matcher = $this->exactly(3); | ||
|
||
$someServiceMock = $this->createMock(SomeMockedClass::class); | ||
$someServiceMock->expects($matcher) | ||
->method('prepare') | ||
->with([1]); | ||
} | ||
} | ||
|
||
?> |
28 changes: 28 additions & 0 deletions
28
...hodCall/NarrowSingleWillReturnCallbackRector/NarrowSingleWillReturnCallbackRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\NarrowSingleWillReturnCallbackRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class NarrowSingleWillReturnCallbackRectorTest extends AbstractRectorTestCase | ||
{ | ||
#[DataProvider('provideData')] | ||
public function test(string $filePath): void | ||
{ | ||
$this->doTestFile($filePath); | ||
} | ||
|
||
public static function provideData(): Iterator | ||
{ | ||
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...Quality/Rector/MethodCall/NarrowSingleWillReturnCallbackRector/Source/SomeMockedClass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\NarrowSingleWillReturnCallbackRector\Source; | ||
|
||
class SomeMockedClass | ||
{ | ||
} |
10 changes: 10 additions & 0 deletions
10
...Quality/Rector/MethodCall/NarrowSingleWillReturnCallbackRector/config/configured_rule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\NarrowSingleWillReturnCallbackRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(NarrowSingleWillReturnCallbackRector::class); | ||
}; |
168 changes: 168 additions & 0 deletions
168
rules/CodeQuality/Rector/MethodCall/NarrowSingleWillReturnCallbackRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\PHPUnit\CodeQuality\Rector\MethodCall; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Arg; | ||
use PhpParser\Node\Expr\Closure; | ||
use PhpParser\Node\Expr\Match_; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PhpParser\Node\Identifier; | ||
use PhpParser\Node\Scalar\LNumber; | ||
use PhpParser\Node\Stmt\Expression; | ||
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; | ||
use Rector\Rector\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\NarrowSingleWillReturnCallbackRector\NarrowSingleWillReturnCallbackRectorTest | ||
*/ | ||
final class NarrowSingleWillReturnCallbackRector extends AbstractRector | ||
{ | ||
public function __construct( | ||
private readonly TestsNodeAnalyzer $testsNodeAnalyzer, | ||
) { | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition( | ||
'Narrow single-value match willReturnCallback() to with() and willReturn() call', | ||
[ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
use PHPUnit\Framework\TestCase; | ||
final class SomeTest extends TestCase | ||
{ | ||
public function run() | ||
{ | ||
$matcher = $this->exactly(1); | ||
$this->personServiceMock->expects($matcher) | ||
->willReturnCallback(function (...$parameters) use ($matcher) { | ||
match ($matcher->getInvocationCount()) { | ||
1 => $this->assertSame([1], $parameters), | ||
}; | ||
}); | ||
} | ||
} | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
use PHPUnit\Framework\TestCase; | ||
final class SomeTest extends TestCase | ||
{ | ||
public function run() | ||
{ | ||
$matcher = $this->exactly(1); | ||
$this->personServiceMock->expects($matcher) | ||
->with([1], $parameters); | ||
} | ||
} | ||
CODE_SAMPLE | ||
), | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* @return array<class-string<MethodCall>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [MethodCall::class]; | ||
} | ||
|
||
/** | ||
* @param MethodCall $node | ||
*/ | ||
public function refactor(Node $node): MethodCall|null | ||
{ | ||
if (! $this->testsNodeAnalyzer->isInTestClass($node)) { | ||
return null; | ||
} | ||
|
||
if (! $this->isName($node->name, 'willReturnCallback')) { | ||
return null; | ||
} | ||
|
||
if ($node->isFirstClassCallable()) { | ||
return null; | ||
} | ||
|
||
$firstArg = $node->getArgs()[0]; | ||
|
||
// skip as most likely nested array of unique values | ||
if ($firstArg->unpack) { | ||
return null; | ||
} | ||
|
||
if (! $firstArg->value instanceof Closure) { | ||
return null; | ||
} | ||
|
||
$closure = $firstArg->value; | ||
|
||
// handle easy path of single stmt first | ||
if (count($closure->stmts) !== 1) { | ||
return null; | ||
} | ||
|
||
$onlyStmts = $closure->stmts[0]; | ||
if (! $onlyStmts instanceof Expression) { | ||
return null; | ||
} | ||
|
||
if (! $onlyStmts->expr instanceof Match_) { | ||
return null; | ||
} | ||
|
||
$match = $onlyStmts->expr; | ||
|
||
// has more options | ||
if (count($match->arms) !== 1) { | ||
return null; | ||
} | ||
|
||
$onlyArm = $match->arms[0]; | ||
if ($onlyArm->conds === null || count($onlyArm->conds) !== 1) { | ||
return null; | ||
} | ||
|
||
$onlyArmCond = $onlyArm->conds[0]; | ||
if (! $onlyArmCond instanceof LNumber) { | ||
return null; | ||
} | ||
|
||
if ($onlyArmCond->value !== 1) { | ||
return null; | ||
} | ||
|
||
// we look for $this->assertSame(...) | ||
if (! $onlyArm->body instanceof MethodCall) { | ||
return null; | ||
} | ||
|
||
$methodCall = $onlyArm->body; | ||
if (! $this->isName($methodCall->var, 'this')) { | ||
return null; | ||
} | ||
|
||
if (! $this->isName($methodCall->name, 'assertSame')) { | ||
return null; | ||
} | ||
|
||
$expectedArg = $methodCall->getArgs()[0]; | ||
|
||
$node->name = new Identifier('with'); | ||
$node->args = [new Arg($expectedArg->value)]; | ||
|
||
return $node; | ||
} | ||
} |