5.next: Capability interfaces for reversible vs directional migrations#1086
Draft
dereuromark wants to merge 5 commits into
Draft
5.next: Capability interfaces for reversible vs directional migrations#1086dereuromark wants to merge 5 commits into
dereuromark wants to merge 5 commits into
Conversation
Introduce two marker interfaces so migrations can declare their style explicitly: - ReversibleMigrationInterface for migrations defining a single change() method (handled with the recording adapter for the down direction). - DirectionalMigrationInterface for migrations defining separate up() and down() methods. Environment now dispatches via instanceof first and keeps the existing method_exists fallback for migrations that have not adopted either interface yet, so the change is backwards compatible. The interfaces declare their method contracts via PHPDoc method tags only (not as real abstract methods), so adopting them is a single-line implements addition with no signature validation pressure. The 6.x release is expected to promote the PHPDoc method tags to real abstract method declarations and drop the method_exists fallback.
Bake-generated migrations now declare implements ReversibleMigrationInterface (change-style) or implements DirectionalMigrationInterface (up/down-style) out of the box. Updated templates: - skeleton.twig (reversible) - skeleton-anonymous.twig (reversible) - diff.twig (directional) - snapshot.twig (reversible or directional based on useChange) All bake comparison fixtures under tests/comparisons/ are updated to match the new output so the bake tests stay green.
Ship a rector rule that retrofits the capability interfaces onto existing migrations during the 5.x upgrade. For every class that extends BaseMigration (directly or transitively): - If the class defines change(), add implements ReversibleMigrationInterface. - If the class defines up() or down(), add implements DirectionalMigrationInterface. - Classes already implementing either interface are skipped. - Classes defining both styles are left alone, since the choice is a deliberate one. Abstract migration bases and anonymous migration classes are both supported so the interface propagates through inheritance and through bake's anonymous migration shape.
Add a dedicated upgrade guide at docs/en/upgrades/upgrading-to-capability-interfaces.md covering motivation, per-app before/after examples for both styles, the rector-driven automatic upgrade, the manual residuals (Phinx-style bases, dynamically generated classes, third-party plugins), and the 6.x forward direction. Wire the new page into the VitePress sidebar (toc_en.json) and update the Migration Methods guide so the inline examples already include the implements clause and link to the upgrade guide.
The rector rule extends rector/rector base classes which are installed on-demand through the rector-setup composer script rather than as a permanent dev dependency. PHPStan runs before that script in CI and therefore cannot resolve AbstractRector or the Symplify value objects. The rule is type-checked by rector itself when invoked, and downstream apps that wire it into their own rector.php have rector installed locally, so excluding the directory here is the minimal fix that keeps the existing on-demand dependency pattern intact.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Refs #1084.
Soft-introduces the capability interfaces proposed in #1084 in the 5.x cycle so the 6.x BC-breaking step lands on top of an already-adopted shape.
What this changes
Two new marker interfaces under the
Migrationsnamespace:ReversibleMigrationInterface— for migrations defining a singlechange()method.DirectionalMigrationInterface— for migrations defining separateup()anddown()methods.Method contracts are declared as PHPDoc method tags only (not real abstract methods):
Migration\Environment::executeMigrationnow dispatches viainstanceoffirst and keeps the existingmethod_exists()fallback for migrations that have not adopted the interfaces yet:Bake templates (
skeleton,skeleton-anonymous,diff,snapshot) now emit the matchingimplementsclause for newly generated migrations.A rector rule (
Migrations\Rector\AddMigrationCapabilityInterfaceRector) is shipped for the upgrade — it walks everyBaseMigrationsubclass in a user'sconfig/Migrationsfolder and adds the rightimplementsclause. Abstract bases and anonymous migration classes are both supported.A dedicated upgrade guide at
docs/en/upgrades/upgrading-to-capability-interfaces.mddocuments the motivation, per-app before/after examples, the rector-driven upgrade, manual residuals, and the 6.x direction.Framing note
function chnage()still silently no-ops today; once 6.x promotes the PHPDoc method tags to real abstract methods this becomes a static error.instanceof, IDEs and PHPStan resolvechange()/up()/down()directly.method_existslookup.MigrationInterfaceno longer need reflection to figure out the migration style.Why marker interfaces with PHPDoc method tags only
This is the soft-window shape. Marker interfaces mean:
implements ReversibleMigrationInterfaceworks even on a migration whosechange()has a non-matching signature. Zero BC pressure on existing code.instanceofnarrows the type.method_existsfallback. The BC break in 6.x then only bites users who adopted the interface in 5.x but mistyped the method signature — a much smaller cohort than today's proposal.Per-app upgrade
For migrations defining
change():For migrations defining
up()/down():Or run rector once:
with a
rector.phplike:Full walkthrough including manual residuals (Phinx-style bases, dynamically generated classes, third-party plugins) is in the new upgrade guide.
Forward direction (6.x)
A follow-up 6.x PR will:
method_exists()fallback inEnvironment.Apps that ran rector during 5.x see the 6.x bump as a no-op for their migration files.
Notes for reviewers
Environmentdispatch order isReversible → Directional → legacy change → legacy up/down. The directional interface check comes before the legacychangefallback so that a class implementingDirectionalMigrationInterfacethat also defineschange()is dispatched viaup()/down()— the interface declaration wins over method existence. Test coverage for this inEnvironmentTest::testDirectionalInterfaceWinsOverChangeMethod.tests/comparisons/were regenerated to match the new bake output. No content change beyond the newimplementsclause and matchinguseimport.voidreturns. The 6.x abstract method declaration should match.