Skip to content
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 config/sets/phpunit-code-quality.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\BareCreateMockAssignToDirectUseRector;
use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\DataProviderArrayItemsNewLinedRector;
use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\EntityDocumentCreateMockToDirectNewRector;
use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\NoSetupWithParentCallOverrideRector;
use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\RemoveEmptyTestMethodRector;
use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\ReplaceTestAnnotationWithPrefixedFunctionRector;
use Rector\PHPUnit\CodeQuality\Rector\Expression\AssertArrayCastedObjectToAssertSameRector;
Expand Down Expand Up @@ -134,5 +135,8 @@
EntityDocumentCreateMockToDirectNewRector::class,
ReplaceAtMethodWithDesiredMatcherRector::class,
BareCreateMockAssignToDirectUseRector::class,

// readability
NoSetupWithParentCallOverrideRector::class,
]);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\NoSetupWithParentCallOverrideRector\Fixture;

use PHPUnit\Framework\TestCase;

final class KeepIfNoParentCall extends TestCase
{
#[\Override]
protected function setUp(): void
{
$some = 'value';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\NoSetupWithParentCallOverrideRector\Fixture;

use PHPUnit\Framework\TestCase;

final class SkipDifferentMethod extends TestCase
{
#[\Override]
protected function testThat(): void
{
parent::testThat();

$some = 'value';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\NoSetupWithParentCallOverrideRector\Fixture;

use PHPUnit\Framework\TestCase;

final class SomeFileTest extends TestCase
{
#[\Override]
protected function setUp(): void
{
parent::setUp();

$some = 'value';
}
}

?>
-----
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\NoSetupWithParentCallOverrideRector\Fixture;

use PHPUnit\Framework\TestCase;

final class SomeFileTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
$some = 'value';
}
}

?>
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\ClassMethod\NoSetupWithParentCallOverrideRector;

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

final class NoSetupWithParentCallOverrideRectorTest 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,9 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\NoSetupWithParentCallOverrideRector;

return RectorConfig::configure()
->withRules([NoSetupWithParentCallOverrideRector::class]);
46 changes: 46 additions & 0 deletions rules/CodeQuality/NodeAnalyser/ParentCallDetector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\CodeQuality\NodeAnalyser;

use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use Rector\NodeNameResolver\NodeNameResolver;

final readonly class ParentCallDetector
{
public function __construct(
private NodeNameResolver $nodeNameResolver
) {
}

public function hasParentCall(ClassMethod $classMethod): bool
{
$methodName = $classMethod->name->toString();

foreach ((array) $classMethod->stmts as $stmt) {
if (! $stmt instanceof Expression) {
continue;
}

if (! $stmt->expr instanceof StaticCall) {
continue;
}

$staticCall = $stmt->expr;
if (! $this->nodeNameResolver->isName($staticCall->class, 'parent')) {
continue;
}

if (! $this->nodeNameResolver->isName($staticCall->name, $methodName)) {
continue;
}

return true;
}

return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\CodeQuality\Rector\ClassMethod;

use PhpParser\Node;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\Doctrine\NodeAnalyzer\AttributeFinder;
use Rector\PHPUnit\CodeQuality\NodeAnalyser\ParentCallDetector;
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\ClassMethod\NoSetupWithParentCallOverrideRector\NoSetupWithParentCallOverrideRectorTest
*/
final class NoSetupWithParentCallOverrideRector extends AbstractRector
{
public function __construct(
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
private readonly ParentCallDetector $parentCallDetector,
private readonly AttributeFinder $attributeFinder,
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Remove override, if setUp() references parent::setUp() call to improve readability',
[
new CodeSample(
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;

final class SomeTest extends TestCase
{
#[\Override]
protected function setUp(): void
{
parent::setUp();

$value = 100;
}
}
CODE_SAMPLE

,
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;

final class SomeTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();

$value = 100;
}
}
CODE_SAMPLE
),
]
);
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [ClassMethod::class];
}

/**
* @param ClassMethod $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->testsNodeAnalyzer->isInTestClass($node)) {
return null;
}

if (! $this->isName($node, 'setUp')) {
return null;
}

if (! $this->parentCallDetector->hasParentCall($node)) {
return null;
}

if (! $this->attributeFinder->hasAttributeByClasses($node, ['Override'])) {
return null;
}

$hasChanged = false;

foreach ($node->attrGroups as $attributeGroupKey => $attrGroup) {
foreach ($attrGroup->attrs as $attributeKey => $attribute) {
if (! $this->isName($attribute->name, 'Override')) {
continue;
}

unset($attrGroup->attrs[$attributeKey]);
$hasChanged = true;
}

if ($attrGroup->attrs === []) {
unset($node->attrGroups[$attributeGroupKey]);
}
}

if (! $hasChanged) {
return null;
}

return $node;
}
}