Skip to content

Commit

Permalink
[CodeQuality] Add NarrowSingleWillReturnCallbackRector
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Sep 29, 2024
1 parent daac9f0 commit da5cbac
Show file tree
Hide file tree
Showing 5 changed files with 258 additions and 0 deletions.
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]);
}
}

?>
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';
}
}
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
{
}
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);
};
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;
}
}

0 comments on commit da5cbac

Please sign in to comment.