A package’s public contract extends beyond method signatures

Use a reading-list model to define a package’s behavioural promises and assess changes that preserve or break compatibility for callers and implementers.

A simple outer surface conceals more relationships than are visible at first glance.
A simple outer surface conceals more relationships than are visible at first glance.

After a library update, the page still opens and all the types match. Yet cards with the same publication time change places, a missing translation is silently replaced with another one, and a storage failure looks like an empty selection. Nothing has broken at the method call. The product has changed for its reader.

When several sites use a shared capability, preserving the method name and return type is not enough. We need to describe the behaviour each consumer may rely on, who may implement the interface, and what may change internally. That makes a compatibility decision explainable before anyone installs the new version.

Consider an independent teaching example: a selection of published notes in a requested language. The contract and changes below form a proposed model for discussion, rather than the implementation of a particular site. The goal is to turn the vague requirement to “work as before” into specific promises.

The model below defines a contract and nine compatibility decisions. Three synthetic sets of expected IDs follow from that definition: UK returns note-a, note-b, RU returns note-a and EN returns an empty list with limit=2. These illustrate the contract rather than report results from an executed PHP implementation.

Start with the behaviour the page uses

Imagine a journal that displays its ten newest notes. The template receives cards in the correct order, displays each title and, when present, a short description. If the requested language version is missing, the page does not substitute text in another language. If reading is impossible, the product shows that the selection is temporarily unavailable.

A second consumer uses the same cards in a narrow panel containing three entries. Its own adapter may obtain data from a different store. Both products need results and errors to mean the same thing, while their routes, presentation and data sources differ.

A compact sketch of the shared boundary looks like this:

interface ReadingList
{
    /** @return list<ReadingCard> */
    public function newest(string $locale, int $limit = 10): array;
}

This declares an idea without implementing it. It shows the argument types, the default limit and an array response. PHP checks declared types at runtime; the detailed rules appear in the type declaration documentation. The list annotation explains the intention to readers and analysis tools, but does not itself make PHP validate the array elements.

The declaration does not tell us which languages are valid, what an empty result means, or whether the call writes anything. Those answers must be supplied separately. Otherwise, each site will fill the gaps with its own assumptions.

One table of commitments for every consumer

Let us establish the teaching version's baseline contract. Its scope is narrow enough to review. The source contains only synthetic notes; access rights, personalisation and multilingual search are outside this model.

Surface Baseline promise What the consumer relies on
Language Only the exact values uk, ru, en are accepted; other values are rejected through InvalidReadingQuery, without normalisation An invalid query does not turn into an arbitrary language choice
Quantity limit is an integer from 1 to 50; omitting it means 10; values outside the range produce InvalidReadingQuery The selection size does not change through silent truncation or a new default
Membership Only published notes with a translation in the requested language are returned, without duplicates Drafts and substituted translations do not appear in the panel
Card fields id and title are nonempty strings; excerpt is a nonempty string or null; text contains no markup A missing description has one defined representation; the template escapes text itself
Order Newer publications come first; ties use ascending bytewise id order; the limit is applied after sorting Reading unchanged data again produces the same sequence
Missing data If no notes match, the result is [] The product can display a valid empty state
Unavailable source A read failure produces ReadingSourceUnavailable; the exception type is stable, but its human-readable message is not The product can show a failure instead of telling readers that no material exists
Side effects Reading does not modify notes or send notifications; reads and technical metrics without note text are allowed Opening a page does not trigger a hidden business action
Configuration The source must be supplied explicitly; its absence produces InvalidReadingConfiguration before requests are served; there is no automatic fallback source Incorrect configuration does not connect a different dataset
Extension Consumers may implement ReadingList; a specific adapter's internals are outside the contract A custom adapter must remain usable after a compatible update

The table does not promise identical results after the data changes. It does not specify response time, the number of SQL queries or the structure of an internal cache. If the product needs a latency limit or an acceptable staleness rule, that requires a separate, measurable commitment. The word “newest” does not establish it.

Bytewise order is also a deliberate choice. It sorts identifiers, not their natural numeric meaning: for example, n-10 comes before n-2. Determinism is sufficient for this task. If the product needs different semantics, those should be established before consumers build expectations around the existing behaviour.

An empty list, a missing description and a failure have different consequences

Take three synthetic cards. The two newer cards were published simultaneously and have identifiers note-a and note-b; the third, note-c, is older. All three have Ukrainian text, while only the first has Russian text. Card note-b has no short description.

For a Ukrainian selection limited to 2, the contract requires the sequence note-a, note-b. For Russian, it requires the single card note-a. An English query produces an empty list while no English text exists. These are not three search modes: one promise is being applied to different inputs.

If a new version starts adding Ukrainian cards in place of missing Russian ones, it increases the number of items but changes the meaning of the language argument. If it replaces a missing description with an empty string, a template checking for null may leave an empty paragraph. If it conceals a storage outage behind an empty list, the interface presents the wrong explanation for the problem.

These differences matter even when every result remains an array. It is useful to examine the consumer's final decision: which cards it displays, whether a description belongs there, and whether it may say that no material exists. Successfully returning from the method does not answer those questions.

Compatibility also depends on interface implementers

For code that only calls a method, an additional optional argument may look safe. In this model, however, a site may supply its own implementation of the interface. That implementation's declaration must also satisfy the new contract. Looking only at existing calls is therefore insufficient.

Symfony's backward compatibility policy treats method calls, interface implementations and inheritance separately. Its tables do not allow simply adding a method, or even an optional argument, to an existing interface. This is one project's policy; for our model, it identifies a useful perspective for the review.

A subtler surface is the parameter name. PHP supports named arguments, so a call may contain limit: 3. Renaming the parameter to $count while preserving its type breaks that calling style if we support it. Our baseline contract does support it, so parameter names are stable too.

Two questions must be asked before making a change: will an old valid call still work, and will an old permitted implementation still be valid? The answers can differ. Similarly, the library should explicitly say whether consumers may extend concrete classes. A method's accessibility does not, by itself, explain the library's extension policy.

Evaluate changes before choosing a version number

Consider several proposals against that same baseline. “Compatible” here means that existing supported usage retains its promised meaning. These are conclusions drawn from the model, rather than results from running a new library.

Proposed change Decision against the baseline Reason
A different sorting algorithm with the same sequence, errors and side effects Compatible The internal way of obtaining the result was not promised
The default limit increases from 10 to 12 Incompatible An existing call without the argument receives a different quantity
Identifiers with equal publication times are returned in reverse order Incompatible Both order and membership of a limited selection change
A missing description becomes an empty string instead of null Incompatible The consumer loses the promised way of distinguishing states
A read failure returns [] instead of ReadingSourceUnavailable Incompatible Failure and missing data become indistinguishable
Another method is added to ReadingList Incompatible for permitted implementations Existing adapters are not required to have that method
An optional argument is added to the existing interface method Incompatible for permitted implementations An existing implementation declaration does not gain the argument automatically
A separate additional interface is introduced; existing calls and adapter requirements remain unchanged Compatible addition The new capability does not become mandatory for an existing consumer
The exception's human-readable message changes without changing its type or conditions Compatible in this model Its exact text was deliberately excluded from the machine contract

In the last case, a site that parses the message with a regular expression can still break. That is a dependency on an unsupported detail. It should be found and removed, while the known risk should not be hidden behind the statement that the contract is intact. If a machine-readable reason is needed, a stable code or a separate type would be a better commitment.

A compatible addition also has conditions. The new interface remains optional only if an existing constructor or container configuration does not start requiring it. The review must cover the entire supported usage path.

A release number communicates the decision; it does not make it

SemVer 2.0.0 requires a public API to be defined. For a stable API, incompatible changes require a major version, compatible functionality a minor version, and compatible fixes a patch. The behavioural commitment therefore comes before the number. The number alone does not prove implementation compatibility.

Bug fixes deserve particular care. If the implementation returned the wrong order while the contract always required the correct one, fixing it restores the promise. Yet a site may have added its own workaround. After the update, that workaround could reverse a now-correct list. Consumer verification remains necessary even when the argument for a compatible fix is sound.

Changing the promise itself under the name of a bug fix is a different situation. If arbitrary order was previously allowed and we now want to guarantee different items at the beginning, the impact on supported usage needs to be established first. An issue's title does not automatically make a behavioural change safe.

Preserve the contract in checks without freezing the implementation

Our selection supports concrete acceptance scenarios: three cards, two sharing a publication time; a missing translation; a missing description; an empty source; an unavailable source; an invalid limit; an existing custom interface implementation; and a call with a named argument. Expectations come from the table, not from the first result that happens to be produced.

Each scenario should protect a promise. Checking the identifier sequence is appropriate because order is defined. Checking the exact number of private helper calls is inappropriate unless it protects a separate constraint. Otherwise, internal optimisation becomes a test migration without changing anything useful for readers.

For the current unchanged dataset, checking data should also be distinguished from checking repeated calls. Receiving the same list twice does not prove the absence of writes or notifications. Side effects require separate observation in a future implementation. Nor does a contract suite prove that a real store withstands network failures; that belongs to the next level of integration verification.

A useful contract document is shorter than a description of the entire library: one scenario, defined inputs and results, boundary conditions, permitted extensions and examples of changes. When a release can show precisely which promises were preserved or revised, the shared capability becomes easier to maintain. Each site gains a clear basis for deciding whether to update.

  1. Conformance Deprecated now, removed later: managing a package API migration
  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

A project by the author · Weather Solution200

After reading, a look outside.

Temperature, rain and wind in your city. Check the forecast and plan the hours ahead.

Check the forecast
Next article