Skip to content

Commit a3f404b

Browse files
committed
feat: add a rector set for v2.9
1 parent 709449d commit a3f404b

9 files changed

+272
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the zenstruck/foundry package.
7+
*
8+
* (c) Kevin Bond <[email protected]>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
use Rector\Config\RectorConfig;
15+
use Rector\Removing\Rector\Class_\RemoveTraitUseRector;
16+
use Zenstruck\Foundry\Test\Factories;
17+
18+
return static function (RectorConfig $rectorConfig): void {
19+
$rectorConfig->ruleWithConfiguration(
20+
RemoveTraitUseRector::class,
21+
[
22+
Factories::class,
23+
]
24+
);
25+
};
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace Zenstruck\Foundry\Utils\Rector;
4+
5+
use PhpParser\Node;
6+
use PhpParser\NodeFinder;
7+
use Rector\Rector\AbstractRector;
8+
use Zenstruck\Foundry\Attribute\ResetDatabase as ResetDatabaseAttribute;
9+
use Zenstruck\Foundry\Test\ResetDatabase as ResetDatabaseTrait;
10+
11+
final class ResetDatabaseAttributeRector extends AbstractRector
12+
{
13+
public function __construct(
14+
private readonly NodeFinder $nodeFinder,
15+
) {
16+
}
17+
18+
/** @return array<class-string<Node>> */
19+
public function getNodeTypes(): array
20+
{
21+
return [Node\Stmt\Class_::class];
22+
}
23+
24+
/** @param Node\Stmt\Class_ $node */
25+
public function refactor(Node $node): Node|null
26+
{
27+
/** @var ?Node\Stmt\TraitUse $traitUseWithResetDatabase */
28+
$traitUseWithResetDatabase = $this->nodeFinder->findFirst($node->stmts, function (Node $node): bool {
29+
return $node instanceof Node\Stmt\TraitUse
30+
&& array_any($node->traits, fn(Node\Name $name) => $this->getName($name) === ResetDatabaseTrait::class);
31+
});
32+
33+
if (!$traitUseWithResetDatabase) {
34+
return null;
35+
}
36+
37+
$traitUseWithResetDatabase->traits = array_filter(
38+
$traitUseWithResetDatabase->traits,
39+
fn(Node\Name $name) => $this->getName($name) !== ResetDatabaseTrait::class
40+
);
41+
42+
if ($traitUseWithResetDatabase->traits === []) {
43+
$node->stmts = array_filter($node->stmts, fn(Node\Stmt $stmt) => $stmt !== $traitUseWithResetDatabase);
44+
}
45+
46+
$hasResetDatabaseTrait = (bool)$this->nodeFinder->findFirst($node->attrGroups, function (Node $node): bool {
47+
return $this->getName($node) === ResetDatabaseAttribute::class;
48+
});
49+
50+
if ($hasResetDatabaseTrait) {
51+
return $node;
52+
}
53+
54+
$node->attrGroups[] = new Node\AttributeGroup([
55+
new Node\Attribute(new Node\Name\FullyQualified(ResetDatabaseAttribute::class)),
56+
]);
57+
58+
return $node;
59+
}
60+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
4+
use Zenstruck\Foundry\Test\ResetDatabase;
5+
6+
#[OtherAttribute]
7+
class SomeClass extends KernelTestCase
8+
{
9+
use ResetDatabase;
10+
}
11+
12+
?>
13+
-----
14+
<?php
15+
16+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
17+
use Zenstruck\Foundry\Test\ResetDatabase;
18+
19+
#[OtherAttribute]
20+
#[\Zenstruck\Foundry\Attribute\ResetDatabase]
21+
class SomeClass extends KernelTestCase
22+
{
23+
}
24+
25+
?>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
4+
use Zenstruck\Foundry\Test\ResetDatabase;
5+
6+
class SomeClass extends KernelTestCase
7+
{
8+
use ResetDatabase;
9+
}
10+
11+
?>
12+
-----
13+
<?php
14+
15+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
16+
use Zenstruck\Foundry\Test\ResetDatabase;
17+
18+
#[\Zenstruck\Foundry\Attribute\ResetDatabase]
19+
class SomeClass extends KernelTestCase
20+
{
21+
}
22+
23+
?>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
4+
5+
class SomeClass extends KernelTestCase
6+
{
7+
}
8+
9+
?>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
4+
use Zenstruck\Foundry\Test\ResetDatabase;
5+
6+
#[\Zenstruck\Foundry\Attribute\ResetDatabase]
7+
class SomeClass extends KernelTestCase
8+
{
9+
use ResetDatabase;
10+
}
11+
12+
?>
13+
-----
14+
<?php
15+
16+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
17+
use Zenstruck\Foundry\Test\ResetDatabase;
18+
19+
#[\Zenstruck\Foundry\Attribute\ResetDatabase]
20+
class SomeClass extends KernelTestCase
21+
{
22+
}
23+
24+
?>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
4+
use Zenstruck\Foundry\Test\ResetDatabase;
5+
6+
class SomeClass extends KernelTestCase
7+
{
8+
use ResetDatabase, OtherTrait;
9+
}
10+
11+
class OtherClass extends KernelTestCase
12+
{
13+
use OtherTrait, ResetDatabase;
14+
}
15+
16+
class SomeOtherClass extends KernelTestCase
17+
{
18+
use OtherTrait;
19+
use ResetDatabase;
20+
}
21+
22+
?>
23+
-----
24+
<?php
25+
26+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
27+
use Zenstruck\Foundry\Test\ResetDatabase;
28+
29+
#[\Zenstruck\Foundry\Attribute\ResetDatabase]
30+
class SomeClass extends KernelTestCase
31+
{
32+
use OtherTrait;
33+
}
34+
35+
#[\Zenstruck\Foundry\Attribute\ResetDatabase]
36+
class OtherClass extends KernelTestCase
37+
{
38+
use OtherTrait;
39+
}
40+
41+
#[\Zenstruck\Foundry\Attribute\ResetDatabase]
42+
class SomeOtherClass extends KernelTestCase
43+
{
44+
use OtherTrait;
45+
}
46+
47+
?>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the zenstruck/foundry package.
7+
*
8+
* (c) Kevin Bond <[email protected]>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Zenstruck\Foundry\Utils\Rector\Tests\ResetDatabaseAttribute;
15+
16+
use PHPUnit\Framework\Attributes\DataProvider;
17+
use PHPUnit\Framework\Attributes\Test;
18+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
19+
20+
final class ResetDatabaseAttributeRectorTest extends AbstractRectorTestCase
21+
{
22+
#[Test]
23+
#[DataProvider('provideData')]
24+
public function test(string $filePath): void
25+
{
26+
$this->doTestFile($filePath);
27+
}
28+
29+
public static function provideData(): \Iterator
30+
{
31+
return self::yieldFilesFromDirectory(__DIR__.'/Fixtures');
32+
}
33+
34+
public function provideConfigFilePath(): string
35+
{
36+
return __DIR__.'/config.php';
37+
}
38+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the zenstruck/foundry package.
7+
*
8+
* (c) Kevin Bond <[email protected]>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
use Rector\Config\RectorConfig;
15+
use Zenstruck\Foundry\Utils\Rector\ResetDatabaseAttributeRector;
16+
17+
return static function (RectorConfig $rectorConfig): void {
18+
$rectorConfig->rules(
19+
[ResetDatabaseAttributeRector::class],
20+
);
21+
};

0 commit comments

Comments
 (0)