Skip to content

Commit 97ceda7

Browse files
authored
feat(upgrade): add upgrade package (#1534)
1 parent b1459db commit 97ceda7

18 files changed

+537
-1
lines changed

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"psr/http-factory": "^1.0",
3333
"psr/http-message": "^1.0|^2.0",
3434
"psr/log": "^3.0.0",
35+
"rector/rector": "^2.1",
3536
"symfony/cache": "^7.3",
3637
"symfony/mailer": "^7.2.6",
3738
"symfony/process": "^7.3",
@@ -66,7 +67,6 @@
6667
"phpstan/phpstan": "^2.0",
6768
"phpunit/phpunit": "^12.2.3",
6869
"predis/predis": "^3.0.0",
69-
"rector/rector": "^2.0-rc2",
7070
"spatie/phpunit-snapshot-assertions": "^5.1.8",
7171
"spaze/phpstan-disallowed-calls": "^4.0",
7272
"symfony/amazon-mailer": "^7.2.0",
@@ -103,6 +103,7 @@
103103
"tempest/router": "self.version",
104104
"tempest/storage": "self.version",
105105
"tempest/support": "self.version",
106+
"tempest/upgrade": "self.version",
106107
"tempest/validation": "self.version",
107108
"tempest/view": "self.version",
108109
"tempest/vite": "self.version"
@@ -143,6 +144,7 @@
143144
"Tempest\\Router\\": "packages/router/src",
144145
"Tempest\\Storage\\": "packages/storage/src",
145146
"Tempest\\Support\\": "packages/support/src",
147+
"Tempest\\Upgrade\\": "packages/upgrade/src",
146148
"Tempest\\Validation\\": "packages/validation/src",
147149
"Tempest\\View\\": "packages/view/src",
148150
"Tempest\\Vite\\": "packages/vite/src"
@@ -208,6 +210,7 @@
208210
"Tempest\\Router\\Tests\\": "packages/router/tests",
209211
"Tempest\\Storage\\Tests\\": "packages/storage/tests",
210212
"Tempest\\Support\\Tests\\": "packages/support/tests",
213+
"Tempest\\Upgrade\\Tests\\": "packages/upgrade/tests",
211214
"Tempest\\Validation\\Tests\\": "packages/validation/tests",
212215
"Tempest\\View\\Tests\\": "packages/view/tests",
213216
"Tempest\\Vite\\Tests\\": "packages/vite/tests",

mago.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ excludes = [
1111
"./vendor/composer",
1212
"**/.cache",
1313
"**/*.stub.php",
14+
"**/*.input.php",
15+
"**/*.expected.php",
1416
]
1517

1618
[format]

packages/upgrade/.gitattributes

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Exclude build/test files from the release
2+
.github/ export-ignore
3+
tests/ export-ignore
4+
.gitattributes export-ignore
5+
.gitignore export-ignore
6+
phpunit.xml export-ignore
7+
README.md export-ignore
8+
9+
# Configure diff output
10+
*.view.php diff=html
11+
*.php diff=php
12+
*.css diff=css
13+
*.html diff=html
14+
*.md diff=markdown

packages/upgrade/LICENSE.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2024 Brent Roose [email protected]
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

packages/upgrade/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
## Upgrade guide
2+
3+
4+
1. Make sure rector is installed:
5+
- `composer require rector/rector --dev`
6+
- Run `vendor/bin/rector` if you don't have a `rector.php` config file
7+
2. Add the necessary rector sets in your `rector.php` config file:
8+
9+
```php
10+
return RectorConfig::configure()
11+
// …
12+
->withSets([__DIR__ . '/vendor/tempest/framework/packages/upgrade/src/tempest2.php']);
13+
```
14+
15+
3. Run `vendor/bin/rector`

packages/upgrade/composer.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "tempest/upgrade",
3+
"description": "A collection of Rector rules for upgrading Tempest",
4+
"license": "MIT",
5+
"minimum-stability": "dev",
6+
"require": {
7+
"php": "^8.4",
8+
"rector/rector": "^2.1"
9+
},
10+
"autoload": {
11+
"psr-4": {
12+
"Tempest\\Upgrade\\": "src"
13+
}
14+
},
15+
"autoload-dev": {
16+
"psr-4": {
17+
"Tempest\\Upgrade\\Tests\\": "tests"
18+
}
19+
}
20+
}

packages/upgrade/phpunit.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.4/phpunit.xsd"
5+
bootstrap="vendor/autoload.php"
6+
executionOrder="depends,defects"
7+
beStrictAboutOutputDuringTests="true"
8+
displayDetailsOnPhpunitDeprecations="true"
9+
failOnPhpunitDeprecation="false"
10+
failOnRisky="true"
11+
failOnWarning="true"
12+
>
13+
<testsuites>
14+
<testsuite name="Tempest Support">
15+
<directory>tests</directory>
16+
</testsuite>
17+
</testsuites>
18+
<source restrictNotices="true" restrictWarnings="true" ignoreIndirectDeprecations="true">
19+
<include>
20+
<directory>src</directory>
21+
</include>
22+
</source>
23+
</phpunit>
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
namespace Tempest\Upgrade\Tempest2;
4+
5+
use PhpParser\Node;
6+
use PhpParser\Node\Name;
7+
use PhpParser\Node\Stmt\ClassMethod;
8+
use Rector\Rector\AbstractRector;
9+
10+
final class MigrationRector extends AbstractRector
11+
{
12+
public function getNodeTypes(): array
13+
{
14+
return [
15+
Node\Stmt\Class_::class,
16+
];
17+
}
18+
19+
public function refactor(Node $node): void
20+
{
21+
if (! ($node instanceof Node\Stmt\Class_)) {
22+
return;
23+
}
24+
25+
// Check whether this class implements Tempest\Database\DatabaseMigration
26+
$implements = $node->implements;
27+
28+
$implementsDatabaseMigration = array_find_key(
29+
$implements,
30+
static fn (Node\Name $name) => $name->toString() === 'Tempest\Database\DatabaseMigration',
31+
);
32+
33+
if ($implementsDatabaseMigration === null) {
34+
return;
35+
}
36+
37+
// Unset the old interface
38+
unset($implements[$implementsDatabaseMigration]);
39+
40+
// Add the new MigrateUp interface
41+
$implements[] = new Node\Name('\Tempest\Database\MigratesUp');
42+
$node->getMethod('up')->returnType = new Name('QueryStatement');
43+
44+
// Check whether the migration has a down method implemented or not
45+
$downStatements = $node->getMethod('down')->stmts;
46+
47+
$migratesDown = true;
48+
49+
foreach ($downStatements as $statement) {
50+
if (! ($statement instanceof Node\Stmt\Return_)) {
51+
continue;
52+
}
53+
54+
if (! ($statement->expr instanceof Node\Expr\ConstFetch)) {
55+
continue;
56+
}
57+
58+
$migratesDown = $statement->expr->name->toString() !== 'null';
59+
60+
break;
61+
}
62+
63+
if ($migratesDown) {
64+
// If the migration has a down method implemented, we'll add the new MigrateDown interface
65+
$implements[] = new Node\Name('\Tempest\Database\MigratesDown');
66+
$node->getMethod('down')->returnType = new Name('QueryStatement');
67+
} else {
68+
// If the migration does not have a down method implemented, we'll remove it entirely
69+
$statements = $node->stmts;
70+
71+
foreach ($node->stmts as $key => $statement) {
72+
if (! ($statement instanceof ClassMethod)) {
73+
continue;
74+
}
75+
76+
if ($statement->name->toString() !== 'down') {
77+
continue;
78+
}
79+
80+
unset($statements[$key]);
81+
82+
$node->stmts = $statements;
83+
}
84+
}
85+
86+
$node->implements = $implements;
87+
}
88+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Tempest\Upgrade\Tempest2;
4+
5+
use PhpParser\Node;
6+
use PhpParser\NodeVisitor;
7+
use Rector\Rector\AbstractRector;
8+
9+
final class RemoveDatabaseMigrationImportRector extends AbstractRector
10+
{
11+
public function getNodeTypes(): array
12+
{
13+
return [
14+
Node\UseItem::class,
15+
];
16+
}
17+
18+
public function refactor(Node $node): ?int
19+
{
20+
if (! ($node instanceof Node\UseItem)) {
21+
return null;
22+
}
23+
24+
if ($node->name->toString() === 'Tempest\Database\DatabaseMigration') {
25+
return NodeVisitor::REMOVE_NODE;
26+
}
27+
28+
return null;
29+
}
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Tempest\Upgrade\Tempest2;
4+
5+
use PhpParser\Node;
6+
use PhpParser\NodeVisitor;
7+
use Rector\Rector\AbstractRector;
8+
9+
final class RemoveIdImportRector extends AbstractRector
10+
{
11+
public function getNodeTypes(): array
12+
{
13+
return [
14+
Node\UseItem::class,
15+
];
16+
}
17+
18+
public function refactor(Node $node): ?int
19+
{
20+
if (! ($node instanceof Node\UseItem)) {
21+
return null;
22+
}
23+
24+
if ($node->name->toString() === 'Tempest\\Database\\Id') {
25+
return NodeVisitor::REMOVE_NODE;
26+
}
27+
28+
return null;
29+
}
30+
}

0 commit comments

Comments
 (0)