Skip to content

Commit bdc08e2

Browse files
committed
add custom Rector to rename Id to PrimaryKey
1 parent cf504ad commit bdc08e2

File tree

7 files changed

+149
-3
lines changed

7 files changed

+149
-3
lines changed

composer.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,9 @@
143143
"Tempest\\Support\\": "packages/support/src",
144144
"Tempest\\Validation\\": "packages/validation/src",
145145
"Tempest\\View\\": "packages/view/src",
146-
"Tempest\\Vite\\": "packages/vite/src"
146+
"Tempest\\Vite\\": "packages/vite/src",
147+
"Utils\\Rector\\Rector\\": "utils/rector/src/Rector",
148+
"Utils\\Rector\\Tests\\Rector\\": "utils/rector/tests/Rector/"
147149
},
148150
"files": [
149151
"packages/clock/src/functions.php",
@@ -177,7 +179,8 @@
177179
"packages/support/src/Str/functions.php",
178180
"packages/support/src/functions.php",
179181
"packages/view/src/functions.php",
180-
"packages/vite/src/functions.php"
182+
"packages/vite/src/functions.php",
183+
"vendor/rector/rector/preload.php"
181184
]
182185
},
183186
"autoload-dev": {

phpunit.xml.dist

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
<testsuite name="Unit">
1818
<directory suffix="Test.php">packages/**/tests</directory>
1919
</testsuite>
20+
<testsuite name="Rector">
21+
<directory suffix="Test.php">utils/rector/tests</directory>
22+
</testsuite>
2023
</testsuites>
2124
<coverage />
2225
<source>

rector.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Rector\Arguments\Rector\ClassMethod\ArgumentAdderRector;
66
use Rector\Caching\ValueObject\Storage\FileCacheStorage;
77
use Rector\CodingStyle\Rector\Encapsed\EncapsedStringsToSprintfRector;
8-
use Rector\CodingStyle\Rector\Stmt\NewlineAfterStatementRector;
98
use Rector\Config\RectorConfig;
109
use Rector\DeadCode\Rector\PropertyProperty\RemoveNullPropertyInitializationRector;
1110
use Rector\Php70\Rector\StaticCall\StaticCallOnNonStaticToInstanceCallRector;
@@ -23,6 +22,7 @@
2322
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector;
2423
use Rector\TypeDeclaration\Rector\Closure\ClosureReturnTypeRector;
2524
use Rector\TypeDeclaration\Rector\Empty_\EmptyOnNullableObjectToInstanceOfRector;
25+
use Utils\Rector\Rector\RenameIdToPrimaryKeyRector;
2626

2727
return RectorConfig::configure()
2828
->withCache('./.cache/rector', FileCacheStorage::class)
@@ -38,6 +38,7 @@
3838
])
3939
->withRules([
4040
ExplicitNullableParamTypeRector::class,
41+
RenameIdToPrimaryKeyRector::class,
4142
])
4243
->withSkip([
4344
AddOverrideAttributeToOverriddenMethodsRector::class,
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Utils\Rector\Rector;
6+
7+
use Composer\Semver\Semver;
8+
use PhpParser\Node;
9+
use PhpParser\Node\Name as NodeName;
10+
use PhpParser\Node\Name\FullyQualified as FullyQualifiedNodeName;
11+
use PhpParser\Node\Stmt\Property as PropertyStatement;
12+
use Rector\Rector\AbstractRector;
13+
use Tempest\Core\Kernel;
14+
15+
/**
16+
* @see \Utils\Rector\Tests\TypeDeclaration\Rector\RenameIdToPrimaryKeyRector\RenameIdToPrimaryKeyRectorTest
17+
*/
18+
final class RenameIdToPrimaryKeyRector extends AbstractRector
19+
{
20+
/**
21+
* @return array<class-string<Node>>
22+
*/
23+
public function getNodeTypes(): array
24+
{
25+
return [PropertyStatement::class];
26+
}
27+
28+
/**
29+
* @param PropertyStatement $node
30+
*/
31+
public function refactor(Node $node): ?Node
32+
{
33+
if (! Semver::satisfies(Kernel::VERSION, '^2.0.0')) {
34+
return null;
35+
}
36+
37+
if ($node->type === null) {
38+
return null;
39+
}
40+
41+
if (! ($node->type instanceof NodeName)) {
42+
return null;
43+
}
44+
45+
$className = $node->type->toString();
46+
if ($className !== 'Tempest\\Database\\Id') {
47+
return null;
48+
}
49+
50+
$node->type = new FullyQualifiedNodeName('Tempest\\Database\\PrimaryKey');
51+
52+
return $node;
53+
}
54+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\RenameIdToPrimaryKeyRector\Fixture;
4+
5+
use Tempest\Database\Id;
6+
7+
final class Publisher
8+
{
9+
use IsDatabaseModel;
10+
11+
public Id $id;
12+
13+
public string $name;
14+
15+
public string $description;
16+
}
17+
18+
?>
19+
-----
20+
<?php
21+
22+
namespace Rector\Tests\TypeDeclaration\Rector\RenameIdToPrimaryKeyRector\Fixture;
23+
24+
use Tempest\Database\PrimaryKey;
25+
26+
final class Publisher
27+
{
28+
use IsDatabaseModel;
29+
30+
public PrimaryKey $id;
31+
32+
public string $name;
33+
34+
public string $description;
35+
}
36+
37+
?>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Utils\Rector\Tests\Rector\RenameIdToPrimaryKeyRector;
6+
7+
use Composer\Semver\Semver;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
use Tempest\Core\Kernel;
11+
12+
final class RenameIdToPrimaryKeyRectorTest extends AbstractRectorTestCase
13+
{
14+
#[DataProvider('provideData')]
15+
public function test(string $filePath): void
16+
{
17+
if (! Semver::satisfies(Kernel::VERSION, '^2.0.0')) {
18+
$this->markTestSkipped('This test is only relevant for Tempest 2.0.0 and above.');
19+
}
20+
21+
$this->doTestFile($filePath);
22+
}
23+
24+
public static function provideData(): \Iterator
25+
{
26+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
27+
}
28+
29+
public function provideConfigFilePath(): string
30+
{
31+
return __DIR__ . '/config/configured_rule.php';
32+
}
33+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Utils\Rector\Rector\RenameIdToPrimaryKeyRector;
7+
8+
return RectorConfig::configure()->withRules([
9+
RenameIdToPrimaryKeyRector::class,
10+
])
11+
->withImportNames(
12+
importDocBlockNames: false,
13+
importShortClasses: false,
14+
removeUnusedImports: true
15+
);

0 commit comments

Comments
 (0)