Skip to content

One decorator lookup and one decorator map shape for the Sql platform - #257

Closed
tyrsson wants to merge 1 commit into
0.6.xfrom
rfc/256-platform-split
Closed

tyrsson wants to merge 1 commit into
0.6.xfrom
rfc/256-platform-split

Conversation

@tyrsson

@tyrsson tyrsson commented Nov 26, 2026

Copy link
Copy Markdown
Member

Types of changes

  • Documentation improvement
  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • RFC (Request for Comments, a discussion topic)
  • QA improvement (unit/integration tests, CS fixes, etc.) that does not change code behavior

Checklist

  • An issue has been created for this pull request.
  • I have added tests for my changes.
  • All new and existing tests passed.
  • Documentation has been updated accordingly.
  • This pull request targets the correct branch.

Description

First step of #256. It removes the pre-split platform resolution layer and gives the Sql platform one honest decorator contract. The RFC is split because its remaining steps either rename an @api base class that two driver packages extend, or move Sql92 out of the public surface across three packages.

One lookup, one map shape

AbstractPlatform no longer returns a decorator or a bare subject from one union-typed method. decoratorFor() returns the decorator registered for a subject, bound to it, or null:

$renderer = $this->decoratorFor($subject);
if (! $renderer instanceof SqlInterface) {
    $renderer = $subject;          // no decorator for this dialect
}

That deletes getTypeDecorator(), its PlatformDecoratorInterface|PreparableSqlInterface|SqlInterface return type, and requireDecoratorImplementing() with its per-call narrowing. A bare subject can no longer occupy a decorator position, because no method returns one there.

The decorator map has a single flat shape, array<class-string, PlatformDecoratorInterface>, in one class. The nested array<string, array<string, PlatformDecoratorInterface>> union that forced @var casts in getTypeDecorator() and decoratorsFor() is gone.

Registration enforces the invariant

setTypeDecorator() rejects a decorator that does not implement the type it is registered for, with Sql\Exception\RuntimeException::DECORATOR_WRONG_TYPE. A decorator that cannot render its subject now fails where it is registered rather than when it is used, which is what makes the render path's fallback unreachable for correct registration and safe for a subclass that injects directly into $decorators.

No platform-name registry

Removed from Sql\Platform\Platform: resolvePlatformName(), $cachedPlatformName, resolvePlatform(), getDefaultPlatform(), and the third $adapterOrPlatform argument on setTypeDecorator(). The dialect is fixed by the platform object, and no code in src/ ever populated the name-keyed map, so getDecorators() on a standard-SQL platform was an undefined key followed by a TypeError. It now returns [] because there is nothing to look up.

Platform keeps an ignored ?PlatformInterface constructor argument so new Platform($this) in Sql92 and in the pg driver keeps working, and it ships with no decorators: standard SQL needs none.

Guard rules

mago guard had no perimeter configuration, so it skipped every check. Two rules now encode the layering, and I confirmed both fire by adding a temporary violating import to each layer:

  • PhpDb\Adapter may not depend on PhpDb\Sql\Platform\AbstractPlatform.
  • PhpDb\Sql may not depend on PhpDb\Adapter\Platform\AbstractPlatform.

Motivation and Context

Refs #256.

getTypeDecorator() was introduced in 2014 (dda49f70) by folding two duplicated loops into one method. The loops each checked the capability needed for the call (instanceof PreparableSqlInterface, instanceof SqlInterface) and fell back to the subject at the call site. The folded version dropped that check and moved the fallback into the return type, which is where the union of bare interfaces came from. This restores the call-site fallback and the capability check, in the place the split now requires: registration.

What stays the same

  • The SQL produced by every first-party decorator. Both driver platforms resolve exactly as before; I checked PhpDb\Mysql\Sql\Platform and PhpDb\Sqlite\Sql\Platform against the new contract, including the "no decorator for this type" fallback.
  • Decorator resolution order: first registered match wins, by instanceof.
  • PlatformDecoratorInterface, setSubject(), and the public render methods.
  • getSqlPlatformDecorator() on PlatformInterface, so a driver still supplies its platform.

Breaking changes

  • getTypeDecorator() is replaced by decoratorFor(). Nothing in the fleet calls it.
  • Platform::setTypeDecorator() no longer takes the platform argument. No production caller used it; only tests did.
  • Sql\Exception\RuntimeException::forDecoratorNotImplementing() and DECORATOR_NOT_IMPLEMENTING are replaced by forDecoratorWrongType() and DECORATOR_WRONG_TYPE, because the condition moved from delegation to registration. Both were added in Clear Mago findings in the Sql platform layer #252 and are unreleased in 0.6.x.
  • Platform::getSqlString() with no platform argument no longer substitutes the adapter platform, since the object no longer holds one. Sql always passes a platform, so the render path through Sql is unchanged.
  • A decorator that does not implement its registered type is now rejected at registration. Third-party decorators that registered an incompatible pairing and never used it will now throw.

Not in this PR

  • Removing the Sql92 runtime default in AbstractSql::getSqlString() and moving Sql92 out of src. Needs 90 call sites in this package plus 17 in phpdb-validator and phpdb-paginator-adapter.
  • Renaming Sql\Platform\AbstractPlatform. phpdb-mysql and phpdb-sqlite extend it, so it needs a coordinated release.
  • SequenceFeature's vendor match. Needs a TableGateway feature shipped in phpdb-pgsql first.
  • Banning PhpDb\Sql\Platform\Platform from PhpDb\Adapter (guard). Sql92::getSqlPlatformDecorator() still returns it, so that rule lands with the Sql92 step.

How has this been tested?

Mago 1.59.0 on PHP 8.5.12:

  • format --check, lint, analyze and guard all exit 0.
  • lint --fail-on-out-of-sync-baseline and analyze --fail-on-out-of-sync-baseline exit 0. The analyzer baseline loses 9 outdated entries, all in Sql\Platform\Platform or PlatformDecoratorInterface.
  • Perimeter rules verified to fire, not skip: a temporary use PhpDb\Sql\Platform\AbstractPlatform in src/Adapter and a temporary use PhpDb\Adapter\Platform\AbstractPlatform in src/Sql each produce disallowed-use and disallowed-class-constant.

PHPUnit: 2864 tests, 4992 assertions, OK. PlatformTest and AbstractPlatformTest are rewritten for the new contract, including the fallback paths for a decorator that can only render or only prepare. Every changed src line is covered (Clover, statement lines).

Cross-package smoke test: PhpDb\Mysql\Sql\Platform registers and resolves Select, CreateTable and AlterTable, PhpDb\Sqlite\Sql\Platform registers Select, and both fall back for Insert and the DDL types they do not decorate.

Docs: new docs/book/sql/platform.md covers the two platform concepts, one platform per adapter, the decorator contract and registration rule, and the driver package checklist. Added to the mkdocs.yml nav.

AbstractPlatform now asks decoratorFor() for the decorator registered for a
subject and renders the subject itself when there is none. That removes the
union of bare Sql interfaces from the return type: a bare subject no longer
occupies a decorator position, so requireDecoratorImplementing() and its
per-call narrowing are gone.

setTypeDecorator() enforces the invariant at registration instead, rejecting a
decorator that does not implement the type it is registered for. A decorator
that cannot render its subject can no longer be registered, so the render path
has nothing left to check.

Sql\Platform\Platform drops the platform-name keyed registry: resolvePlatformName(),
cachedPlatformName, resolvePlatform(), getDefaultPlatform() and the third
setTypeDecorator() argument are gone, and the decorator map has one flat shape in
one class. Platform keeps its constructor argument so drivers that pass their
adapter platform keep working, and it now ships with no decorators, which is what
standard SQL needs.

TestPlatform confirms the absent default: the stand-in assets and both driver
platforms resolve and fall back exactly as before. Guard now enforces the two
layer rules the split depends on.

Refs #256

Signed-off-by: Joey Smith <jsmith@webinertia.net>

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@codecov

codecov Bot commented Nov 26, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@tyrsson

tyrsson commented Nov 27, 2026

Copy link
Copy Markdown
Member Author

Superseded by #258, which removes the decorator machinery rather than reshaping it. Closing so this shape is not reviewed.

@tyrsson tyrsson closed this Nov 27, 2026
@tyrsson
tyrsson deleted the rfc/256-platform-split branch November 27, 2026 07:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant