File tree Expand file tree Collapse file tree 7 files changed +149
-3
lines changed
tests/Rector/RenameIdToPrimaryKeyRector Expand file tree Collapse file tree 7 files changed +149
-3
lines changed Original file line number Diff line number Diff line change 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" ,
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" : {
Original file line number Diff line number Diff line change 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 >
Original file line number Diff line number Diff line change 55use Rector \Arguments \Rector \ClassMethod \ArgumentAdderRector ;
66use Rector \Caching \ValueObject \Storage \FileCacheStorage ;
77use Rector \CodingStyle \Rector \Encapsed \EncapsedStringsToSprintfRector ;
8- use Rector \CodingStyle \Rector \Stmt \NewlineAfterStatementRector ;
98use Rector \Config \RectorConfig ;
109use Rector \DeadCode \Rector \PropertyProperty \RemoveNullPropertyInitializationRector ;
1110use Rector \Php70 \Rector \StaticCall \StaticCallOnNonStaticToInstanceCallRector ;
2322use Rector \TypeDeclaration \Rector \ClassMethod \ReturnNeverTypeRector ;
2423use Rector \TypeDeclaration \Rector \Closure \ClosureReturnTypeRector ;
2524use Rector \TypeDeclaration \Rector \Empty_ \EmptyOnNullableObjectToInstanceOfRector ;
25+ use Utils \Rector \Rector \RenameIdToPrimaryKeyRector ;
2626
2727return RectorConfig::configure ()
2828 ->withCache ('./.cache/rector ' , FileCacheStorage::class)
3838 ])
3939 ->withRules ([
4040 ExplicitNullableParamTypeRector::class,
41+ RenameIdToPrimaryKeyRector::class,
4142 ])
4243 ->withSkip ([
4344 AddOverrideAttributeToOverriddenMethodsRector::class,
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ ?>
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ );
You can’t perform that action at this time.
0 commit comments