11# PHPStan Rules for Database Migrations
22
3- A collection of PHPStan rules to enforce best practices and standards in database migration files for various frameworks and tools .
3+ A collection of PHPStan rules to enforce best practices and standards in database migration files for Phinx and Laravel / Illuminate .
44
55## Installation
66
@@ -15,72 +15,69 @@ includes:
1515 - vendor/bellangelo/phpstan-migration-rules/extension.neon
1616```
1717
18- ## Rules
18+ ## Rule catalog
1919
20- ### Phinx
20+ Each rule below applies to migration files, regardless of framework, unless stated otherwise.
2121
22- ### EnforceCollationRule
22+ ### Rule: ` EnforceCollationRule `
23+
24+ | Field | Description |
25+ | ---| ---|
26+ | Purpose | Enforces that table definitions explicitly define a collation |
27+ | Why | Prevents relying on database defaults, which may differ between environments |
28+ | Framework support | Phinx, Laravel |
29+ | Default behavior | Requires a collation to be specified |
30+ | Configuration | ` requiredCollation ` (string) |
31+
32+ #### Configuration
2333
24- Enforces that all Phinx ` table() ` method calls specify a collation (default: ` utf8 ` )
2534``` yaml
2635parameters :
2736 phpstanMigrationRules :
28- phinx :
29- requiredCollation : utf8mb4
37+ requiredCollation : utf8mb4
3038` ` `
3139
32- ---
33-
34- ### ForbidAfterRule
35-
36- Forbids using the after column option in Phinx addColumn() calls, because it can trigger a full table rewrite (unsafe for large or production tables).
40+ #### Detection details
3741
38- No configuration is required.
42+ | Framework | How collation is detected |
43+ |---|---|
44+ | Phinx | ` table('name', ['collation' => '…'])` |
45+ | Laravel | `$table->collation('…')` or `$table->collation = '…'` inside the Blueprint callback |
3946
4047---
4148
42- ### ForbidMultipleTableCreationsRule
49+ # ## Rule: `ForbidAfterRule`
4350
44- Forbids creating more than one table in a single Phinx migration.
51+ | Field | Description |
52+ |---|---|
53+ | Purpose | Forbids column positioning via `after()` |
54+ | Why | May trigger full table rewrites or long locks, unsafe for large or production tables |
55+ | Framework support | Phinx, Laravel |
56+ | Configuration | None |
4557
46- A table creation is detected via calls to ` create()` on table instances.
58+ # ### Detection details
4759
48- No configuration is required.
60+ | Framework | Forbidden usage |
61+ |---|---|
62+ | Phinx | `addColumn(..., ['after' => 'column'])` |
63+ | Laravel | `$table->string('x')->after('y')` |
4964
5065---
5166
52- # ## Laravel
67+ # ## Rule: `ForbidMultipleTableCreationsRule`
5368
54- # ## EnforceCollationRule
55-
56- Enforces that Laravel Schema::create() and Schema::table() calls specify a table collation (default : utf8).
57-
58- The rule detects collation being set anywhere inside the Blueprint callback via either :
59-
60- - ` $table->collation('...')` , or
61- - ` $table->collation = '...'`
62-
63- ` ` ` yaml
64- parameters:
65- phpstanMigrationRules:
66- phinx:
67- requiredCollation: utf8mb4
68- ` ` `
69-
70- # ## ForbidAfterRule
71-
72- Forbids using Laravel’s `after()` column modifier in migrations.
73-
74- Using `after()` can force a full table rewrite or long locks (engine-dependent), which is unsafe for large or production tables.
75-
76- No configuration is required.
69+ | Field | Description |
70+ |---|---|
71+ | Purpose | Ensures each migration creates at most one table |
72+ | Why | Improves rollback safety and migration clarity |
73+ | Framework support | Phinx, Laravel |
74+ | Configuration | None |
7775
7876---
7977
80- # ## ForbidMultipleTableCreationsRule
81-
82- Forbids creating more than one table in a single Laravel migration.
83-
84- A table creation is detected via multiple `Schema::create()` calls inside the same migration.
78+ # ### Detection details
8579
86- No configuration is required.
80+ | Framework | What counts as a table creation |
81+ |---|---|
82+ | Phinx | Multiple calls to `create()` on table instances |
83+ | Laravel | Multiple `Schema::create()` calls in the same migration |
0 commit comments