-
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 SingleWithConsecutiveToWithRector
- Loading branch information
1 parent
4c3932b
commit 7d29f98
Showing
8 changed files
with
213 additions
and
0 deletions.
There are no files selected for viewing
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
43 changes: 43 additions & 0 deletions
43
...Quality/Rector/MethodCall/SingleWithConsecutiveToWithRector/Fixture/repeated_same.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,43 @@ | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\SingleWithConsecutiveToWithRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\SingleWithConsecutiveToWithRector\Source\SomeMockedClass; | ||
|
||
final class RepeatedSame extends TestCase | ||
{ | ||
public function test() | ||
{ | ||
$someServiceMock = $this->createMock(SomeMockedClass::class); | ||
$someServiceMock->expects($this->exactly(3)) | ||
->method('prepare') | ||
->withConsecutive( | ||
[1], | ||
); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\SingleWithConsecutiveToWithRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\SingleWithConsecutiveToWithRector\Source\SomeMockedClass; | ||
|
||
final class RepeatedSame extends TestCase | ||
{ | ||
public function test() | ||
{ | ||
$someServiceMock = $this->createMock(SomeMockedClass::class); | ||
$someServiceMock->expects($this->exactly(3)) | ||
->method('prepare') | ||
->with( | ||
[1], | ||
); | ||
} | ||
} | ||
|
||
?> |
20 changes: 20 additions & 0 deletions
20
.../Rector/MethodCall/SingleWithConsecutiveToWithRector/Fixture/skip_multiple_values.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,20 @@ | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\SingleWithConsecutiveToWithRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\NarrowIdenticalWithConsecutiveRector\Source\SomeMockedClass; | ||
|
||
final class SkipMultipleValues extends TestCase | ||
{ | ||
public function test() | ||
{ | ||
$someServiceMock = $this->createMock(SomeMockedClass::class); | ||
$someServiceMock->expects($this->exactly(3)) | ||
->method('prepare') | ||
->withConsecutive( | ||
[1], | ||
[2], | ||
); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...or/MethodCall/SingleWithConsecutiveToWithRector/SingleWithConsecutiveToWithRectorTest.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\SingleWithConsecutiveToWithRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class SingleWithConsecutiveToWithRectorTest 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
...odeQuality/Rector/MethodCall/SingleWithConsecutiveToWithRector/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\SingleWithConsecutiveToWithRector\Source; | ||
|
||
class SomeMockedClass | ||
{ | ||
} |
10 changes: 10 additions & 0 deletions
10
...odeQuality/Rector/MethodCall/SingleWithConsecutiveToWithRector/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\SingleWithConsecutiveToWithRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(SingleWithConsecutiveToWithRector::class); | ||
}; |
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
100 changes: 100 additions & 0 deletions
100
rules/CodeQuality/Rector/MethodCall/SingleWithConsecutiveToWithRector.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,100 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\PHPUnit\CodeQuality\Rector\MethodCall; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Arg; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PhpParser\Node\Expr\StaticCall; | ||
use PhpParser\Node\Identifier; | ||
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\SingleWithConsecutiveToWithRector\SingleWithConsecutiveToWithRectorTest | ||
*/ | ||
final class SingleWithConsecutiveToWithRector extends AbstractRector | ||
{ | ||
public function __construct( | ||
private readonly TestsNodeAnalyzer $testsNodeAnalyzer | ||
) { | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition( | ||
'Change single withConsecutive() to with() call', | ||
[ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
use PHPUnit\Framework\TestCase; | ||
final class SomeTest extends TestCase | ||
{ | ||
public function run() | ||
{ | ||
$this->personServiceMock->expects($this->exactly(3)) | ||
->method('prepare') | ||
->withConsecutive( | ||
[1], | ||
); | ||
} | ||
} | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
use PHPUnit\Framework\TestCase; | ||
final class SomeTest extends TestCase | ||
{ | ||
public function run() | ||
{ | ||
$this->personServiceMock->expects($this->exactly(3)) | ||
->method('prepare') | ||
->with([1]); | ||
} | ||
} | ||
CODE_SAMPLE | ||
), | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* @return array<class-string<MethodCall|StaticCall>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [MethodCall::class]; | ||
} | ||
|
||
/** | ||
* @param MethodCall|StaticCall $node | ||
*/ | ||
public function refactor(Node $node): MethodCall|StaticCall|null | ||
{ | ||
if (! $this->testsNodeAnalyzer->isInTestClass($node)) { | ||
return null; | ||
} | ||
|
||
if (! $this->isName($node->name, 'withConsecutive')) { | ||
return null; | ||
} | ||
|
||
if (count($node->getArgs()) !== 1) { | ||
return null; | ||
} | ||
|
||
$firstArg = $node->getArgs()[0]; | ||
|
||
// use simpler with() instead | ||
$node->name = new Identifier('with'); | ||
$node->args = [new Arg($firstArg->value)]; | ||
|
||
return $node; | ||
} | ||
} |