You write the rules in plain language:
R1. A deposit is a positive amount.
R2. A deposit adds the amount to the balance.
R3. The balance is never negative.
Your agent turns them into contracts, the code and the tests:
use PhpDeal\Attribute\{Ensure, Invariant, Verify};
#[Invariant('$this->balance >= 0')] // R3
class Account
{
private float $balance = 0.0;
#[Verify('$amount > 0')] // R1
#[Ensure('$this->balance === $__old->balance + $amount')] // R2
public function deposit(float $amount): void
{
$this->balance += $amount;
}
}
When the code or its caller breaks a rule, PhpDeal stops the call and names the rule:
new Account()->deposit(-10);
// PhpDeal\Exception\PreconditionViolation:
// Precondition "$amount > 0" violated in Account->deposit(-10.0)
Contents
Why AI Design by Contract?
An AI agent writes code faster than anyone can review it. The hard question is no longer how to implement a
feature but whether the code does what the business asked. The usual answers fall short:
- Review does not scale to every line an agent writes.
- Tests written by the same agent share its reading of the requirements. When the agent misreads a rule, the
code and the tests agree with each other, and both are wrong.
- Requirements in a ticket are forgotten by the next change, whether a person or an agent makes it.
Design by Contract closes that gap. Every business rule becomes
an executable contract that lives next to the code it constrains:
- Preconditions (
#[Verify]) are what the caller must guarantee before a call.
- Postconditions (
#[Ensure]) are what the method promises after it returns.
- Invariants (
#[Invariant]) are what is always true of an object between calls.
PhpDeal checks them on every call. That gives each party something it lacked:
| For... | Contracts give you |
|---|
| You | A one-line, reviewable statement of each rule, traced to the specification, instead of hundreds of lines of code |
| The agent | An immediate, exact signal: which rule broke, in which method, with which arguments. It fixes the code instead of guessing |
| The tests | An oracle on every call. Any test that drives the object through valid calls also checks every postcondition and invariant |
| The next change | Rules that stay enforced. A later edit, by a person or an agent, can not quietly break one |
A broken contract is a bug, reported at the exact place where it happens, before a corrupt state travels through
the application.
How it works