Deprecated now, removed later: managing a package API migration

An API migration window in a minimal PHP Composer fixture: deprecation, explicit locale, scanner checks, and native Console/DI without a new Symfony Kernel.

A migration path should be ready before the old interface is removed.

A deprecated marker is useful when consumers have time and a clear route to a replacement. If the old method stops working immediately, the warning merely becomes another way to report a breakage. If it stays forever without an owner or removal criteria, the library gradually supports several conflicting ways to do the same thing.

Consider moving from an implicit title language to an explicit argument. A local teaching package and several synthetic consumers are checked in a minimal PHP Composer fixture alongside one article_demo. No new Symfony application is created. The matrix shows what evidence is needed between deprecating an API and removing it.

Define exactly what is deprecated

Imagine a small package called demo/title-label. This is an invented name for the local exercise. In version 1.2.0, its final class TitleFormatter has a method named label(string $title). It strips boundary characters using trim(), rejects an empty result with InvalidArgumentException, and adds a Ukrainian prefix. Calling it with Canal path should produce Матеріал: Canal path.

Other languages are needed, but silently changing the old prefix according to the environment would break a consumer's expectation. The replacement is named labelFor(string $title, string $locale) and accepts the exact values uk, ru, and en. An unknown language is rejected. The old method remains a short route to the new one with an explicit uk.

The class is final, and the exercise permits neither external interface implementations nor subclasses. There are therefore no external implementations of an added method to migrate here. For an extensible library, the consumer inventory would include those implementations too. Deprecating calls alone is insufficient if the package also changes what an allowed adapter must implement.

Keep a real migration window

SemVer associates marking public API as deprecated with a minor release, and incompatible removal with a major release. That rule communicates the meaning of a change; it does not determine a sufficient support period for particular consumers. The interval between versions provides room for an available replacement, migration guidance, and application checks.

Package state Old label() New labelFor() Consumer task
1.2.0 Established behaviour Unavailable Record the initial expectations
1.3.0 Same behaviour plus a deprecation notice Available Move to an explicit language while staying on the compatible version
2.0.0 Removed Available Check the already migrated scenario without the old route

The version numbers belong only to the teaching package. A migration window does not require every product to update at the same moment. Each known consumer benefits from an owner, its current version, the location of old usage, and the replacement's verification status. A calendar deadline alone does not establish readiness.

For an open library, its owner may not know every application that installed it. An internal list cannot establish that all consumers have migrated. A published support policy, an accessible migration note, and a clear version boundary are needed. A consumer postponing an update may deliberately remain on an older supported branch. Releasing a new major version and ending support for the previous one are related but separate decisions.

Point the warning to a specific replacement

The method remains available in the transition version. The following excerpt belongs to TitleFormatter; the new method in the same class validates the language and the nonempty title.

    /** @deprecated since 1.3, use labelFor() with an explicit locale. */
    public function label(string $title): string
    {
        trigger_deprecation(
            'demo/title-label',
            '1.3',
            'Method "%s()" is deprecated; use "%s()" with an explicit locale.',
            __METHOD__,
            self::class . '::labelFor',
        );
        return $this->labelFor($title, 'uk');
    }

The notice identifies the package, the version introducing the deprecation, and the replacement. PHPDoc helps readers and tools, while trigger_deprecation() creates a signal when the method is called. This approach is described in Symfony's deprecation conventions. The version in the message marks when deprecation began; it is not an automatic removal deadline.

The wrapper changes neither the result type, the Ukrainian prefix, nor the exception for an empty title. It does add a diagnostic event: a strict verification environment may deliberately treat that event as a problem. Consequently, supporting the old call does not require every tool to remain silent. Preserve the product's behaviour while making the outstanding migration visible.

Migrate the call on the compatible version first

The migrated consumer passes the language explicitly:

    public function title(string $locale = 'uk'): string
    {
        return $this->formatter->labelFor('  Canal path  ', $locale);
    }

The first check preserves the Ukrainian result. Russian and English variants follow, along with rejection of an unsupported value. This separates changing how the method is called from introducing new product behaviour. Renaming the method and immediately changing the language everywhere would combine two different risks.

A migration note should show the old and new calls, the required argument, the earliest package version providing the replacement, and the planned removal. Here the new code requires at least 1.3.0; it should not work with the old 1.2.0. That negative check guards against accidentally pairing new code with an old dependency set.

Avoid hiding a missing method behind method_exists() and an indefinite fallback. A compatibility adapter can be justified for a product that truly supports several library generations. It then becomes a separate supported contract with its own boundaries and lifetime, rather than an unnoticed migration remnant.

Find usage through two channels

A plain text search finds documentation, comments, and string literals alongside real calls. In this exercise, a small scanner uses PHP lexical tokens: it looks for the literal name label after an object access operator and before a call parenthesis. It skips comments, respects case-insensitive method names, and handles ordinary and nullsafe calls.

This produces locations to inspect, not a complete type analysis. An unrelated object with a method of the same name also appears in the report. Calls through $method, callbacks, or reflection can fall outside it. In the synthetic set, the direct old consumer should be found and the dynamic one deliberately missed. This boundary is part of the exercise, not a concealed weakness in its evidence.

The second channel executes scenarios on the transition version. A local recorder uses a PHP error handler to collect E_USER_DEPRECATED and the invoked synthetic consumer's name without arguments. It should see both the direct and dynamic routes. The handler is installed only around the check and restored afterward; this is not a ready production logging policy.

No events means only that the old call was absent from the executed routes. A rare command, seasonal scenario, or separate site mode might never reach the check. An inventory of known consumers, source inspection, and behavioural coverage therefore complement each other.

Build a reproducible matrix

Use PHP 8.4+ with tokenizer, Composer, bash, and sha256sum. The runner creates only a minimal PHP consumer at a new absolute directory and refuses an existing path, including a symbolic link. Each library version has unchanged sources and its own composer.json.

MIGRATION_WORKSPACE="$(mktemp -d)"
bash run-lab.sh "$MIGRATION_WORKSPACE/migration"

Versions are selected sequentially through a Composer path repository, forcing copies with symlink: false and an exact version in require. After each selection, the check verifies the version actually installed. The same consumer classes deliberately remain as fixtures: they compare success and rejection, rather than describe a production application's final contents.

Each stage records the actual composer.lock, installed versions, and scenario results. Composer distinguishes choosing dependencies during an update from installing an already resolved lock file. A range in composer.json is therefore not a record of an exact environment.

Native ContainerBuilder composes dependencies for LabelCommand; Console Application and CommandTester execute the command. This checks wiring beyond a direct class call, without a Kernel or FrameworkBundle. The experiment used PHP 8.5.8, Composer 2.10.2, Console/DI 8.1.11, and deprecation-contracts 3.8.1.

Removal needs its own negative check

After moving to 2.0.0, the new call should retain its results, while both direct and dynamic old calls should fail because the method is missing. If they still work, the old route has not actually been removed, or different files are installed. The intentional rejection confirms the release boundary; it does not establish the readiness of an unmigrated consumer.

17 scenario groups were executed: 4 for the initial version, 6 each for the transition and new versions, and one scanner group with 8 variants. These are verification groups, not PHPUnit tests or a speed measurement. The result applies to synthetic consumers. API removal needs an available replacement and checks of specific supported paths.

  1. Vendors & Packages A package’s public contract extends beyond method signatures
  2. Vendors & Packages What a changelog should explain: from file lists to user behavior
  3. Conformance SemVer through a behaviour change: choosing a version without self-deception
  4. Platform Engineering Consumer contract tests: catch a break before upgrading the site