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
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"psr/http-factory": "^1.0",
"psr/http-message": "^1.0|^2.0",
"psr/log": "^3.0.0",
"rector/rector": "^2.1",
"symfony/cache": "^7.3",
"symfony/mailer": "^7.2.6",
"symfony/process": "^7.3",
Expand Down Expand Up @@ -66,7 +67,6 @@
"phpstan/phpstan": "^2.0",
"phpunit/phpunit": "^12.2.3",
"predis/predis": "^3.0.0",
"rector/rector": "^2.0-rc2",
"spatie/phpunit-snapshot-assertions": "^5.1.8",
"spaze/phpstan-disallowed-calls": "^4.0",
"symfony/amazon-mailer": "^7.2.0",
Expand Down Expand Up @@ -103,6 +103,7 @@
"tempest/router": "self.version",
"tempest/storage": "self.version",
"tempest/support": "self.version",
"tempest/upgrade": "self.version",
"tempest/validation": "self.version",
"tempest/view": "self.version",
"tempest/vite": "self.version"
Expand Down Expand Up @@ -143,6 +144,7 @@
"Tempest\\Router\\": "packages/router/src",
"Tempest\\Storage\\": "packages/storage/src",
"Tempest\\Support\\": "packages/support/src",
"Tempest\\Upgrade\\": "packages/upgrade/src",
"Tempest\\Validation\\": "packages/validation/src",
"Tempest\\View\\": "packages/view/src",
"Tempest\\Vite\\": "packages/vite/src"
Expand Down Expand Up @@ -208,6 +210,7 @@
"Tempest\\Router\\Tests\\": "packages/router/tests",
"Tempest\\Storage\\Tests\\": "packages/storage/tests",
"Tempest\\Support\\Tests\\": "packages/support/tests",
"Tempest\\Upgrade\\Tests\\": "packages/upgrade/tests",
"Tempest\\Validation\\Tests\\": "packages/validation/tests",
"Tempest\\View\\Tests\\": "packages/view/tests",
"Tempest\\Vite\\Tests\\": "packages/vite/tests",
Expand Down
2 changes: 2 additions & 0 deletions mago.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ excludes = [
"./vendor/composer",
"**/.cache",
"**/*.stub.php",
"**/*.input.php",
"**/*.expected.php",
]

[format]
Expand Down
14 changes: 14 additions & 0 deletions packages/upgrade/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Exclude build/test files from the release
.github/ export-ignore
tests/ export-ignore
.gitattributes export-ignore
.gitignore export-ignore
phpunit.xml export-ignore
README.md export-ignore

# Configure diff output
*.view.php diff=html
*.php diff=php
*.css diff=css
*.html diff=html
*.md diff=markdown
9 changes: 9 additions & 0 deletions packages/upgrade/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
The MIT License (MIT)

Copyright (c) 2024 Brent Roose [email protected]

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:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

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.
15 changes: 15 additions & 0 deletions packages/upgrade/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## Upgrade guide


1. Make sure rector is installed:
- `composer require rector/rector --dev`
- Run `vendor/bin/rector` if you don't have a `rector.php` config file
2. Add the necessary rector sets in your `rector.php` config file:

```php
return RectorConfig::configure()
// …
->withSets([__DIR__ . '/vendor/tempest/framework/packages/upgrade/src/tempest2.php']);
```

3. Run `vendor/bin/rector`
20 changes: 20 additions & 0 deletions packages/upgrade/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "tempest/upgrade",
"description": "A collection of Rector rules for upgrading Tempest",
"license": "MIT",
"minimum-stability": "dev",
"require": {
"php": "^8.4",
"rector/rector": "^2.1"
},
"autoload": {
"psr-4": {
"Tempest\\Upgrade\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Tempest\\Upgrade\\Tests\\": "tests"
}
}
}
23 changes: 23 additions & 0 deletions packages/upgrade/phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.4/phpunit.xsd"
bootstrap="vendor/autoload.php"
executionOrder="depends,defects"
beStrictAboutOutputDuringTests="true"
displayDetailsOnPhpunitDeprecations="true"
failOnPhpunitDeprecation="false"
failOnRisky="true"
failOnWarning="true"
>
<testsuites>
<testsuite name="Tempest Support">
<directory>tests</directory>
</testsuite>
</testsuites>
<source restrictNotices="true" restrictWarnings="true" ignoreIndirectDeprecations="true">
<include>
<directory>src</directory>
</include>
</source>
</phpunit>
88 changes: 88 additions & 0 deletions packages/upgrade/src/Tempest2/MigrationRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace Tempest\Upgrade\Tempest2;

use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\Rector\AbstractRector;

final class MigrationRector extends AbstractRector
{
public function getNodeTypes(): array
{
return [
Node\Stmt\Class_::class,
];
}

public function refactor(Node $node): void
{
if (! ($node instanceof Node\Stmt\Class_)) {
return;
}

// Check whether this class implements Tempest\Database\DatabaseMigration
$implements = $node->implements;

$implementsDatabaseMigration = array_find_key(
$implements,
static fn (Node\Name $name) => $name->toString() === 'Tempest\Database\DatabaseMigration',
);

if ($implementsDatabaseMigration === null) {
return;
}

// Unset the old interface
unset($implements[$implementsDatabaseMigration]);

// Add the new MigrateUp interface
$implements[] = new Node\Name('\Tempest\Database\MigratesUp');
$node->getMethod('up')->returnType = new Name('QueryStatement');

// Check whether the migration has a down method implemented or not
$downStatements = $node->getMethod('down')->stmts;

$migratesDown = true;

foreach ($downStatements as $statement) {
if (! ($statement instanceof Node\Stmt\Return_)) {
continue;
}

if (! ($statement->expr instanceof Node\Expr\ConstFetch)) {
continue;
}

$migratesDown = $statement->expr->name->toString() !== 'null';

break;
}

if ($migratesDown) {
// If the migration has a down method implemented, we'll add the new MigrateDown interface
$implements[] = new Node\Name('\Tempest\Database\MigratesDown');
$node->getMethod('down')->returnType = new Name('QueryStatement');
} else {
// If the migration does not have a down method implemented, we'll remove it entirely
$statements = $node->stmts;

foreach ($node->stmts as $key => $statement) {
if (! ($statement instanceof ClassMethod)) {
continue;
}

if ($statement->name->toString() !== 'down') {
continue;
}

unset($statements[$key]);

$node->stmts = $statements;
}
}

$node->implements = $implements;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Tempest\Upgrade\Tempest2;

use PhpParser\Node;
use PhpParser\NodeVisitor;
use Rector\Rector\AbstractRector;

final class RemoveDatabaseMigrationImportRector extends AbstractRector
{
public function getNodeTypes(): array
{
return [
Node\UseItem::class,
];
}

public function refactor(Node $node): ?int
{
if (! ($node instanceof Node\UseItem)) {
return null;
}

if ($node->name->toString() === 'Tempest\Database\DatabaseMigration') {
return NodeVisitor::REMOVE_NODE;
}

return null;
}
}
30 changes: 30 additions & 0 deletions packages/upgrade/src/Tempest2/RemoveIdImportRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Tempest\Upgrade\Tempest2;

use PhpParser\Node;
use PhpParser\NodeVisitor;
use Rector\Rector\AbstractRector;

final class RemoveIdImportRector extends AbstractRector
{
public function getNodeTypes(): array
{
return [
Node\UseItem::class,
];
}

public function refactor(Node $node): ?int
{
if (! ($node instanceof Node\UseItem)) {
return null;
}

if ($node->name->toString() === 'Tempest\\Database\\Id') {
return NodeVisitor::REMOVE_NODE;
}

return null;
}
}
68 changes: 68 additions & 0 deletions packages/upgrade/src/tempest2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

use Rector\Config\RectorConfig;
use Rector\Renaming\Rector\Name\RenameClassRector;
use Rector\Renaming\Rector\PropertyFetch\RenamePropertyRector;
use Rector\Renaming\ValueObject\RenameProperty;
use Tempest\Upgrade\Tempest2\MigrationRector;
use Tempest\Upgrade\Tempest2\RemoveDatabaseMigrationImportRector;
use Tempest\Upgrade\Tempest2\RemoveIdImportRector;

return static function (RectorConfig $config): void {
$config->importNames();
$config->importShortClasses();

$config->rule(MigrationRector::class);

$config->ruleWithConfiguration(RenameClassRector::class, [
'Tempest\Database\Id' => 'Tempest\Database\PrimaryKey',
'Tempest\CommandBus\AsyncCommand' => 'Tempest\CommandBus\Async',
'Tempest\Validation\Rules\AlphaNumeric' => 'Tempest\Validation\Rules\IsAlphaNumeric',
'Tempest\Validation\Rules\ArrayList' => 'Tempest\Validation\Rules\IsArrayList',
'Tempest\Validation\Rules\BeforeDate' => 'Tempest\Validation\Rules\IsBeforeDate',
'Tempest\Validation\Rules\Between' => 'Tempest\Validation\Rules\IsBetween',
'Tempest\Validation\Rules\BetweenDates' => 'Tempest\Validation\Rules\IsBetweenDates',
'Tempest\Validation\Rules\Count' => 'Tempest\Validation\Rules\HasCount',
'Tempest\Validation\Rules\DateTimeFormat' => 'Tempest\Validation\Rules\HasDateTimeFormat',
'Tempest\Validation\Rules\DivisibleBy' => 'Tempest\Validation\Rules\IsDivisibleBy',
'Tempest\Validation\Rules\Email' => 'Tempest\Validation\Rules\IsEmail',
'Tempest\Validation\Rules\EndsWith' => 'Tempest\Validation\Rules\EndsWith',
'Tempest\Validation\Rules\Even' => 'Tempest\Validation\Rules\IsEvenNumber',
'Tempest\Validation\Rules\HexColor' => 'Tempest\Validation\Rules\IsHexColor',
'Tempest\Validation\Rules\IP' => 'Tempest\Validation\Rules\IsIP',
'Tempest\Validation\Rules\IPv4' => 'Tempest\Validation\Rules\IsIPv4',
'Tempest\Validation\Rules\IPv6' => 'Tempest\Validation\Rules\IsIPv6',
'Tempest\Validation\Rules\In' => 'Tempest\Validation\Rules\IsIn',
'Tempest\Validation\Rules\Json' => 'Tempest\Validation\Rules\IsJsonString',
'Tempest\Validation\Rules\Length' => 'Tempest\Validation\Rules\HasLength',
'Tempest\Validation\Rules\Lowercase' => 'Tempest\Validation\Rules\IsLowercase',
'Tempest\Validation\Rules\MACAddress' => 'Tempest\Validation\Rules\IsMacAddress',
'Tempest\Validation\Rules\MultipleOf' => 'Tempest\Validation\Rules\IsMultipleOf',
'Tempest\Validation\Rules\NotEmpty' => 'Tempest\Validation\Rules\IsNotEmptyString',
'Tempest\Validation\Rules\NotIn' => 'Tempest\Validation\Rules\IsNotIn',
'Tempest\Validation\Rules\NotNull' => 'Tempest\Validation\Rules\IsNotNull',
'Tempest\Validation\Rules\Numeric' => 'Tempest\Validation\Rules\IsNumeric',
'Tempest\Validation\Rules\Odd' => 'Tempest\Validation\Rules\IsOddNumber',
'Tempest\Validation\Rules\Password' => 'Tempest\Validation\Rules\IsPassword', // @mago-expect security/no-literal-password
'Tempest\Validation\Rules\PhoneNumber' => 'Tempest\Validation\Rules\IsPhoneNumber',
'Tempest\Validation\Rules\RegEx' => 'Tempest\Validation\Rules\MatchesRegEx',
'Tempest\Validation\Rules\Time' => 'Tempest\Validation\Rules\IsTime',
'Tempest\Validation\Rules\Timestamp' => 'Tempest\Validation\Rules\IsUnixTimestamp',
'Tempest\Validation\Rules\Timezone' => 'Tempest\Validation\Rules\IsTimezone',
'Tempest\Validation\Rules\Ulid' => 'Tempest\Validation\Rules\IsUlid',
'Tempest\Validation\Rules\Uppercase' => 'Tempest\Validation\Rules\IsUppercase',
'Tempest\Validation\Rules\Url' => 'Tempest\Validation\Rules\IsUrl',
'Tempest\Validation\Rules\Uuid' => 'Tempest\Validation\Rules\IsUuid',
]);

$config->ruleWithConfiguration(RenamePropertyRector::class, [
new RenameProperty(
type: 'Tempest\Database\PrimaryKey',
oldProperty: 'id',
newProperty: 'value',
),
]);

$config->rule(RemoveIdImportRector::class);
$config->rule(RemoveDatabaseMigrationImportRector::class);
};
Loading
Loading