Skip to content

Commit 01ddf88

Browse files
committed
Laravel rule does not validate if a table is indeed being created
1 parent ae25293 commit 01ddf88

4 files changed

Lines changed: 66 additions & 1 deletion

File tree

src/Rules/Laravel/EnforceCollationRule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function processNode(Node $node, Scope $scope): array
5454
}
5555

5656
$callName = $this->getStaticCallName($node);
57-
if ($callName !== 'create' && $callName !== 'table') {
57+
if ($callName !== 'create') {
5858
return [];
5959
}
6060

tests/Rules/Laravel/EnforceCollationRuleTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,25 @@ public function testDoesNotReportOutsideLaravelMigration(): void
8181
],
8282
);
8383
}
84+
85+
public function testTableUsageIsIgnored(): void
86+
{
87+
$this->analyse(
88+
[__DIR__ . '/fixtures/TableUsageWithoutCollation.php'],
89+
[]
90+
);
91+
}
92+
93+
public function testMixedExistenceCheckAndCreateOnlyReportsCreate(): void
94+
{
95+
$this->analyse(
96+
[__DIR__ . '/fixtures/MixedTableCheckAndCreate.php'],
97+
[
98+
[
99+
'Required: table collation must be "utf8mb4". Why: prevents environment-dependent defaults and keeps schema consistent. Fix: set the table collation explicitly in the migration.',
100+
16,
101+
],
102+
]
103+
);
104+
}
84105
}
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+
namespace PhpStanMigrationRules\Tests\Rules\Laravel\Fixtures;
6+
7+
use Illuminate\Database\Migrations\Migration;
8+
use Illuminate\Database\Schema\Blueprint;
9+
use Illuminate\Support\Facades\Schema;
10+
11+
final class MixedTableCheckAndCreate extends Migration
12+
{
13+
public function up(): void
14+
{
15+
if (!Schema::hasTable('users')) {
16+
Schema::create('users', function (Blueprint $table): void {
17+
$table->string('email');
18+
});
19+
}
20+
21+
Schema::table('users', function (Blueprint $table): void {
22+
$table->string('name');
23+
});
24+
}
25+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpStanMigrationRules\Tests\Rules\Laravel\Fixtures;
6+
7+
use Illuminate\Database\Migrations\Migration;
8+
use Illuminate\Database\Schema\Blueprint;
9+
use Illuminate\Support\Facades\Schema;
10+
11+
final class TableUsageWithoutCollation extends Migration
12+
{
13+
public function up(): void
14+
{
15+
Schema::table('users', function (Blueprint $table): void {
16+
$table->string('email');
17+
});
18+
}
19+
}

0 commit comments

Comments
 (0)