Skip to content

Release 2.0.0

Latest

Choose a tag to compare

@lisachenko lisachenko released this 10 Nov 18:57
· 6 commits to master since this release
afba723

PhpDeal 2.0.0: AI Design by Contract for PHP

PhpDeal 2 is a complete rewrite for PHP 8.4+, Go! AOP 4 and AI-driven development. Your AI agent turns a business
specification into code with contracts and tests, and PhpDeal checks those contracts on every call.

use PhpDeal\Attribute\{Ensure, Invariant, Verify};

#[Invariant('$this->balance >= 0')]                           // R3: the balance is never negative
class Account
{
    private float $balance = 0.0;

    #[Verify('$amount > 0')]                                  // R1: a deposit is a positive amount
    #[Ensure('$this->balance === $__old->balance + $amount')] // R2: a deposit adds the amount
    public function deposit(float $amount): void
    {
        $this->balance += $amount;
    }
}

new Account()->deposit(-10);
// PhpDeal\Exception\PreconditionViolation: Precondition "$amount > 0" violated in Account->deposit(-10.0)

✨ Highlights

🤖 AI Design by Contract

AI agents write code faster than anyone can review it, and tests written by the same agent share its misreading of
the requirements. PhpDeal 2 makes the business rules themselves executable:

  • Agent Skill design-by-contract (#73). It teaches your coding agent the whole path:

    • extract and number the rules of a specification, with open questions instead of guesses;
    • map each rule to a precondition, a postcondition, an invariant, or explicit code for errors that users see;
    • write the classes, with each contract tagged by its rule ID;
    • derive PHPUnit tests: specification examples, precondition boundaries, business errors, and seeded random
      sequences of valid calls that the contracts check at every step;
    • report a table that maps each rule to its contract, code and test.
  • Claude Code plugin. The repository is a plugin marketplace:

    /plugin marketplace add php-deal/framework
    /plugin install php-deal@php-deal
    

    Other agents that read the Agent Skills format can use skills/design-by-contract, which
    Composer installs with the package.

  • Contract checker, skills/design-by-contract/scripts/lint-contracts.php. It catches mistakes inside contract
    strings that neither PHP nor PHPStan see:

    • syntax errors and misspelled variables;
    • $__old or $__result in the wrong contract, and $this in static methods;
    • class names that use imports do not resolve;
    • assignments, reads of the clock or random values, and in_array() without strict comparison.

    It runs without an AI too, in CI or by hand.

  • Contract-based testing (#36). An agent with no other context
    implemented a new specification from the skill alone: 46 contracts and 46 tests. Contracts caught all five bugs it
    then planted on purpose.

🏷️ Native attributes

Doctrine annotations are replaced by native PHP attributes in PhpDeal\Attribute: #[Verify], #[Ensure],
#[Invariant] and #[Inherit]. They are repeatable where it makes sense, and an argument can hold any PHP
expression, curly braces included.

🎯 Precise violations

  • New exceptions PreconditionViolation, PostconditionViolation and InvariantViolation, all extending
    ContractViolation.
  • A violation exposes the $contract, $className and $methodName, and its file and line point to the method that
    declares the contract.
  • A contract that is broken itself (a syntax error, a non-boolean result, $this in a static method) throws
    InvalidContractException.
  • Every exception implements PhpDeal\Exception\ExceptionInterface.

⚡ Faster and safer

  • Each expression is compiled once, and contracts are read once per method and class.
  • The object is cloned for $__old only when a postcondition uses it.
  • An invariant can call public methods of its own class without endless recursion.

🧬 Inheritance that does what it says

  • Class-level #[Inherit] enables the contracts of the parents for every public method.
  • Inherited preconditions are checked before the method body.
  • Parent contracts run in the scope of the parent and can read its private state.

🔍 PHPStan extension

Private properties and methods used only by contracts are no longer reported as unused. With
phpstan/extension-installer the extension is enabled automatically.

➕ Also new

  • Contracts on static methods.
  • Invariants are checked after the constructor.
  • ContractApplication::registerAspects() enables contracts from an existing Go! AOP kernel.

🐛 Fixed

  • Arrays and objects among the arguments no longer break the violation message.
  • Variadic arguments are passed to contracts as a list.
  • A trailing // comment in an expression no longer breaks it.

⚠️ Breaking changes

1.x 2.0
PHP 7.1 – 7.4 PHP 8.4+
goaop/framework ^2.3 goaop/framework ^4.0
@Contract\Verify("...") annotations #[Verify('...')] attributes
PhpDeal\Annotation PhpDeal\Attribute
attribute $value, __toString() $expression
broken contract → ContractViolation InvalidContractException (DomainException)
AbstractContractAspect, InheritCheckerAspect, Contract\Fetcher\Parent\* removed: logic in ContractFetcher

Upgrade checklist:

  1. Run on PHP 8.4+, require php-deal/framework:^2.0 and update the dependencies.
  2. Replace the annotations with attributes:
    -use PhpDeal\Annotation as Contract;
    +use PhpDeal\Attribute as Contract;
    
    -/** @Contract\Verify("$amount > 0") */
    +#[Contract\Verify('$amount > 0')]
     public function deposit(float $amount): void {}
  3. Delete the old cache directory: the cache format of Go! AOP 4 is different.
  4. Catch PhpDeal\Exception\InvalidContractException instead of DomainException for broken contracts. Existing
    catch (ContractViolation $e) blocks keep working.

UPGRADE-2.0.md lists every behaviour change.

🛠️ Tooling

PHPUnit 13, PHPStan 2 at level 10 with strict rules, PHP-CS-Fixer (PER-CS 2.0), and GitHub Actions on PHP 8.4 and
8.5 instead of Travis CI and Scrutinizer. An AGENTS.md guides AI agents that contribute to the project.

🙏 Thanks

To @lcetinsoy for the idea of contract-based test generation in #36, which this release finally delivers, and
for the documentation work in #35. To @Hywan and @icanhazstring for the discussion that shaped it. To @peter279k
(#37, #38) for contributions since 1.0.0.

Full changelog: 1.0.0...2.0.0