-
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 (#374)
- Loading branch information
1 parent
daac9f0
commit ea5a3c1
Showing
8 changed files
with
347 additions
and
0 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
...Rector/MethodCall/AssertCompareOnCountableWithMethodToAssertCountRector/Fixture/count.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,19 @@ | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertCompareOnCountableWithMethodToAssertCountRector\Fixture; | ||
|
||
use Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertCompareOnCountableWithMethodToAssertCountRector\Source\Collection; | ||
|
||
final class Count extends \PHPUnit\Framework\TestCase | ||
{ | ||
public function test() | ||
{ | ||
$collection = new Collection(); | ||
$this->assertSame(5, $collection->count()); | ||
$this->assertEquals(5, $collection->count()); | ||
\PHPUnit\Framework\TestCase::assertSame(5, $collection->count()); | ||
self::assertSame(5, $collection->count()); | ||
} | ||
} | ||
|
||
?> |
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]); | ||
} | ||
} | ||
|
||
?> |
23 changes: 23 additions & 0 deletions
23
...y/Rector/MethodCall/NarrowSingleWillReturnCallbackRector/Fixture/skip_multi_value.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,23 @@ | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\NarrowSingleWillReturnCallbackRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
final class SkipMultiValue 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), | ||
2 => $this->assertSame([1], $parameters), | ||
}; | ||
}); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...ality/Rector/MethodCall/NarrowSingleWillReturnCallbackRector/Fixture/skip_non_one.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,22 @@ | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\NarrowSingleWillReturnCallbackRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
final class SkipNonOne 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()) { | ||
2 => $this->assertSame([1], $parameters), | ||
}; | ||
}); | ||
} | ||
} |
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); | ||
}; |
193 changes: 193 additions & 0 deletions
193
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,193 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\PHPUnit\CodeQuality\Rector\MethodCall; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Arg; | ||
use PhpParser\Node\Expr; | ||
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; | ||
} | ||
|
||
if (count($node->getArgs()) !== 1) { | ||
return null; | ||
} | ||
|
||
$firstArg = $node->getArgs()[0]; | ||
|
||
$match = $this->matchClosureSingleStmtMatch($firstArg->value); | ||
if (! $match instanceof Match_) { | ||
return null; | ||
} | ||
|
||
$matchArmBody = $this->matchSingleMatchArmBodyWithConditionOne($match); | ||
if (! $matchArmBody instanceof MethodCall) { | ||
return null; | ||
} | ||
|
||
// we look for $this->assertSame(...) | ||
if (! $this->isLocalMethodCall($matchArmBody, 'assertSame')) { | ||
return null; | ||
} | ||
|
||
$expectedArg = $matchArmBody->getArgs()[0]; | ||
|
||
$node->name = new Identifier('with'); | ||
$node->args = [new Arg($expectedArg->value)]; | ||
|
||
return $node; | ||
} | ||
|
||
private function matchClosureSingleStmtMatch(Expr $expr): null|Match_ | ||
{ | ||
if (! $expr instanceof Closure) { | ||
return null; | ||
} | ||
|
||
// handle easy path of single stmt first | ||
if (count($expr->stmts) !== 1) { | ||
return null; | ||
} | ||
|
||
$onlyStmts = $expr->stmts[0]; | ||
if (! $onlyStmts instanceof Expression) { | ||
return null; | ||
} | ||
|
||
if (! $onlyStmts->expr instanceof Match_) { | ||
return null; | ||
} | ||
|
||
return $onlyStmts->expr; | ||
} | ||
|
||
private function isLocalMethodCall(Expr $expr, string $methodName): bool | ||
{ | ||
if (! $expr instanceof MethodCall) { | ||
return false; | ||
} | ||
|
||
if (! $this->isName($expr->var, 'this')) { | ||
return false; | ||
} | ||
|
||
return $this->isName($expr->name, $methodName); | ||
} | ||
|
||
private function matchSingleMatchArmBodyWithConditionOne(Match_ $match): ?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 (! $this->isNumberOne($onlyArmCond)) { | ||
return null; | ||
} | ||
|
||
return $onlyArm->body; | ||
} | ||
|
||
private function isNumberOne(Expr $expr): bool | ||
{ | ||
if (! $expr instanceof LNumber) { | ||
return false; | ||
} | ||
|
||
return $expr->value === 1; | ||
} | ||
} |