Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CodeQuality] Add NarrowIdenticalWithConsecutiveRector #369

Merged
merged 1 commit into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/code_analysis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ jobs:
name: 'Active Classes'
run: composer class-leak

-
name: 'Finalise classes'
run: vendor/bin/swiss-knife finalize-classes src rules tests

name: ${{ matrix.actions.name }}
runs-on: ubuntu-latest
timeout-minutes: 10
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"symplify/vendor-patches": "^11.2",
"tomasvotruba/class-leak": "^0.2",
"tracy/tracy": "^2.10",
"rector/type-perfect": "^0.2"
"rector/type-perfect": "^0.2",
"rector/swiss-knife": "^0.2.35"
},
"autoload": {
"psr-4": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\NarrowIdenticalWithConsecutiveRector\Fixture;

use PHPUnit\Framework\TestCase;
use Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\NarrowIdenticalWithConsecutiveRector\Source\SomeMockedClass;

final class RepeatedSame extends TestCase
{
public function test()
{
$someServiceMock = $this->createMock(SomeMockedClass::class);
$someServiceMock->expects($this->exactly(3))
->method('prepare')
->withConsecutive(
[1],
[1],
[1],
);
}
}

?>
-----
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\NarrowIdenticalWithConsecutiveRector\Fixture;

use PHPUnit\Framework\TestCase;
use Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\NarrowIdenticalWithConsecutiveRector\Source\SomeMockedClass;

final class RepeatedSame extends TestCase
{
public function test()
{
$someServiceMock = $this->createMock(SomeMockedClass::class);
$someServiceMock->expects($this->exactly(3))
->method('prepare')->with([1]);
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\NarrowIdenticalWithConsecutiveRector\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],
[1],
[2],
[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\NarrowIdenticalWithConsecutiveRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class NarrowIdenticalWithConsecutiveRectorTest 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\NarrowIdenticalWithConsecutiveRector\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\NarrowIdenticalWithConsecutiveRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(NarrowIdenticalWithConsecutiveRector::class);
};
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
/**
* @see \Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertCompareOnCountableWithMethodToAssertCountRector\AssertCompareOnCountableWithMethodToAssertCountRectorTest
*/
class AssertCompareOnCountableWithMethodToAssertCountRector extends AbstractRector
final class AssertCompareOnCountableWithMethodToAssertCountRector extends AbstractRector
{
public function __construct(
private readonly TestsNodeAnalyzer $testsNodeAnalyzer
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?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 PhpParser\PrettyPrinter\Standard;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

final class NarrowIdenticalWithConsecutiveRector extends AbstractRector
{
public function __construct(
private readonly TestsNodeAnalyzer $testsNodeAnalyzer
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Narrow identical withConsecutive() to single 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],
[1],
[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;
}

$printerStandard = new Standard();

$cachedValues = [];
foreach ($node->getArgs() as $arg) {
$cachedValues[] = $printerStandard->prettyPrintExpr($arg->value);
}

$uniqueArgValues = array_unique($cachedValues);

// multiple unique values
if (count($uniqueArgValues) !== 1) {
return null;
}

$firstArg = $node->getArgs()[0];

// use simpler with() instead
$node->name = new Identifier('with');
$node->args = [new Arg($firstArg->value)];

return $node;
}
}
Loading