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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"symplify/phpstan-extensions": "^11.1",
"symplify/easy-coding-standard": "^11.2",
"symplify/rule-doc-generator": "^11.2",
"rector/phpstan-rules": "^0.6.5",
"rector/phpstan-rules": "^0.6",
"phpstan/extension-installer": "^1.2",
"phpstan/phpstan-strict-rules": "^1.4.5",
"phpstan/phpstan-webmozart-assert": "^1.2.2",
Expand Down
2 changes: 1 addition & 1 deletion ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
__DIR__ . '/rector.php',
]);

$ecsConfig->skip(['*/Source/*', '*/Fixture/*']);
$ecsConfig->skip(['*/Source/*', '*/Fixture/*', '*/Expected/*']);

$ecsConfig->lineEnding("\n");
};
8 changes: 1 addition & 7 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ parameters:
- */Source/*
- *Source/*
- */Fixture/*
- */Expected/*

reportUnmatchedIgnoredErrors: false

Expand All @@ -30,10 +31,3 @@ parameters:
- '#Parameter \#1 \$node (.*?) of method Rector\\(.*?)\(\) should be contravariant with parameter \$node \(PhpParser\\Node\) of method Rector\\Core\\Contract\\Rector\\PhpRectorInterface\:\:refactor\(\)#'

- '#Cognitive complexity for "Rector\\PHPUnit\\Rector\\MethodCall\\DelegateExceptionArgumentsRector\:\:refactor\(\)" is 12, keep it under 10#'
- '#Class "Rector\\PHPUnit\\Rector\\MethodCall\\DelegateExceptionArgumentsRector" has invalid namespace category "MethodCall"\. Pick one of\: "StmtsAwareInterface"#'
- '#Parameter \#2 \$callable of method Rector\\Core\\NodeManipulator\\ForeachManipulator\:\:matchOnlyStmt\(\) expects callable\(PhpParser\\Node, PhpParser\\Node\\Stmt\\Foreach_\=\)\: PhpParser\\Node\|null, Closure\(PhpParser\\Node, PhpParser\\Node\\Stmt\\Foreach_\)\: PhpParser\\Node\\Expr\\MethodCall\|PhpParser\\Node\\Expr\\StaticCall\|null given#'

# solve later
# - '#Call to an undefined method PhpParser\\Node\\Expr\:\:getArgs\(\)#'
- '#Cognitive complexity for "Rector\\PHPUnit\\Rector\\ClassMethod\\CreateMockToAnonymousClassRector\:\:refactor\(\)" is \d+, keep it under 10#'

4 changes: 3 additions & 1 deletion rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Rector\Config\RectorConfig;
use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector;
use Rector\PHPUnit\Set\PHPUnitSetList;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;

Expand All @@ -16,6 +17,7 @@
// for tests
'*/Source/*',
'*/Fixture/*',
'*/Expected/*',

// object types
StringClassNameToClassConstantRector::class => [
Expand All @@ -33,7 +35,7 @@
__DIR__ . '/config/config.php',
LevelSetList::UP_TO_PHP_81,
SetList::DEAD_CODE,
\Rector\PHPUnit\Set\PHPUnitSetList::PHPUNIT_100,
PHPUnitSetList::PHPUNIT_100,
SetList::CODE_QUALITY,
SetList::CODING_STYLE,
SetList::EARLY_RETURN,
Expand Down
108 changes: 108 additions & 0 deletions src/Rector/ClassLike/RemoveTestSuffixFromAbstractTestClassesRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\Rector\ClassLike;

use PhpParser\Node;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Namespace_;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\Symfony\Printer\NeighbourClassLikePrinter;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\PHPUnit\Tests\Rector\ClassLike\RemoveTestSuffixFromAbstractTestClassesRector\RemoveTestSuffixFromAbstractTestClassesRectorTest
*/
final class RemoveTestSuffixFromAbstractTestClassesRector extends AbstractRector
{
public function __construct(
private readonly NeighbourClassLikePrinter $neighbourClassLikePrinter,
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Rename abstract test class suffix from "*Test" to "*TestCase"',
[
new CodeSample(
<<<'CODE_SAMPLE'
// tests/AbstractTest.php
use PHPUnit\Framework\TestCase;

abstract class AbstractTest extends TestCase
{
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
// tests/AbstractTestCase.php
use PHPUnit\Framework\TestCase;

abstract class AbstractTestCase extends TestCase
{
}
CODE_SAMPLE
),

]
);
}

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

/**
* @param Class_ $node
*/
public function refactor(Node $node): ?Node
{
if (! $node->isAbstract()) {
return null;
}

if (! $this->testsNodeAnalyzer->isInTestClass($node)) {
return null;
}

if (! $node->name instanceof Identifier) {
return null;
}

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

// rename class
$testCaseClassName = $node->name->toString() . 'Case';
$node->name = new Identifier($testCaseClassName);

$this->printNewNodes($node);

return $node;
}

private function printNewNodes(Class_ $class): void
{
$filePath = $this->file->getFilePath();

$parentNode = $class->getAttribute(AttributeKey::PARENT_NODE);
if (! $parentNode instanceof Namespace_) {
throw new ShouldNotHappenException();
}

$this->neighbourClassLikePrinter->printClassLike($class, $parentNode, $filePath, $this->file);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Rector\PHPUnit\Tests\Rector\ClassLike\RemoveTestSuffixFromAbstractTestClassesRector\Fixture;

abstract class ExtendsTestCase extends \PHPUnit\Framework\TestCase
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Rector\PHPUnit\Tests\Rector\ClassLike\RemoveTestSuffixFromAbstractTestClassesRector\Fixture;

use PHPUnit\Framework\TestCase;

abstract class ExtendsTest extends TestCase
{
}

?>
-----
<?php

namespace Rector\PHPUnit\Tests\Rector\ClassLike\RemoveTestSuffixFromAbstractTestClassesRector\Fixture;

use PHPUnit\Framework\TestCase;

abstract class ExtendsTestCase extends TestCase
{
}

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

declare(strict_types=1);
namespace Rector\PHPUnit\Tests\Rector\ClassLike\RemoveTestSuffixFromAbstractTestClassesRector\Fixture;

abstract class SkipAbstractClassWithoutSuffix
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\Tests\Rector\ClassLike\RemoveTestSuffixFromAbstractTestClassesRector;

use Iterator;
use Nette\Utils\FileSystem;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class RemoveTestSuffixFromAbstractTestClassesRectorTest extends AbstractRectorTestCase
{
public function testNoChanges(): void
{
$this->doTestFile(__DIR__ . '/Fixture/skip_abstract_class_without_suffix.php.inc');
}

public function testChanges(): void
{
$this->doTestFile(__DIR__ . '/Fixture/extends_test.php.inc');

$this->assertFileWasAdded(
__DIR__ . '/Fixture/ExtendsTestCase.php',
FileSystem::read(__DIR__ . '/Expected/ExtendsTestCase.php')
);
}

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,12 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\PHPUnit\Rector\ClassLike\RemoveTestSuffixFromAbstractTestClassesRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->import(__DIR__ . '/../../../../../config/config.php');

$rectorConfig->rule(RemoveTestSuffixFromAbstractTestClassesRector::class);
};