AI Pair Programming

phpspec includes an interactive pair programming mode and AI-powered refactoring. Combine the BDD workflow with LLM assistance to write specs, features, and source code conversationally.

The pair command

Start an interactive REPL session:

$ bin/phpspec pair

Or send a single prompt without entering the REPL:

$ bin/phpspec pair --prompt "write a spec for a Calculator that adds two numbers"

Built-in commands

These work without AI configuration:

CommandDescription
describe <Class>Generate a spec file and optionally create the class
exemplify <Class> <method>Add a method example to an existing spec
run [path]Run specs and offer code generation for failures
clearClear the terminal
/helpShow available commands and AI status
/quit, /exitExit pair mode

AI configuration

Add an ai section to your config file:

# phpspec.yaml
ai:
  provider: anthropic
  api_key: YOUR_API_KEY
ProviderDefault Model
googlegemini-2.5-pro
anthropicclaude-sonnet-4-20250514
openaigpt-4o

You can override the model: model: gpt-4o-mini

What you can ask

> write a spec for a UserRepository that finds users by email
> create a feature scenario for user registration
> add a greet method example to the Greeter spec
> run my specs and tell me what's failing
> explain how the Loader class works
> read src/App/Calculator.php and suggest improvements

AI tools

The assistant has access to these tools during a pair session:

ToolDescription
generate_specWrite a .spec.php file
generate_featureWrite a Gherkin .feature file
generate_stepsWrite a .steps.php file
write_fileCreate a new file (class, interface, etc.)
update_fileModify an existing file
run_specsRun specs and return output
read_fileRead a project file for context
list_filesList directory contents

Smart routing

phpspec detects whether input is a command or an AI request:

> describe App/Calculator             # runs describe command
> describe what the Loader class does # routes to AI
> run spec/App                        # runs specs
> run my specs and explain failures   # routes to AI

Conversation history

The AI maintains context within a session. You can build on previous exchanges:

> write a spec for a Calculator
  (AI generates spec)

> now add a divide method that throws on division by zero
  (AI updates the spec, remembering Calculator context)

> run the spec
  (AI runs specs and reports results)

The next command

When you’re not sure what to work on next:

$ bin/phpspec next

Scans your project’s source files, specs, and features, then suggests the most impactful next step. Follows the scenario-first workflow — recommends a feature before a spec, and a spec before an implementation.

phpspec next
Analysing project...

Write a feature scenario for user registration.

Your project has a UserRepository class with a spec, but no
feature scenario covering the registration flow. Adding a
scenario first will drive the step definitions and any
missing specs.

The refactor command

AI-powered, behaviour-preserving refactoring:

$ bin/phpspec refactor "App\Calculator"
$ bin/phpspec refactor "App\Calculator::sum"
$ bin/phpspec refactor "spec/App/Calculator.spec.php"

How it works

  1. Baseline check — Runs specs first. If they fail, refactoring is refused.
  2. AI analysis — The LLM reads source and spec files, identifies a refactoring.
  3. Apply — Writes the refactored code.
  4. Verify — Runs specs again. If they fail, the original is restored.
  5. Report — Shows technique name, description, and a unified diff.

Target resolution

InputSource fileSpec file
App\Calculatorsrc/App/Calculator.phpspec/App/Calculator.spec.php
App\Calculator::sumsrc/App/Calculator.php (focused)spec/App/Calculator.spec.php
spec/App/Calculator.spec.phpsrc/App/Calculator.php (inferred)spec/App/Calculator.spec.php

Refactoring techniques

The AI chooses from standard techniques:

  • Extract Method
  • Inline Variable / Inline Temp
  • Rename (variable, method, class)
  • Extract Class / Move Method
  • Replace Conditional with Polymorphism
  • Introduce Parameter Object
  • Replace Magic Number with Constant
  • Simplify Conditional
  • Remove Dead Code

If the code is already clean, the AI reports that no refactoring is needed.

Requirements

  • AI must be configured in phpspec.yaml (same ai: section)
  • Requires one of: papi-ai/papi-core with a provider package
  • Suggested providers: papi-ai/google, papi-ai/anthropic, papi-ai/openai