Skip to content

Add AI agent detection to auto-switch to raw output#4939

Closed
pushpak1300 wants to merge 1 commit intophpstan:2.1.xfrom
pushpak1300:2.1.x
Closed

Add AI agent detection to auto-switch to raw output#4939
pushpak1300 wants to merge 1 commit intophpstan:2.1.xfrom
pushpak1300:2.1.x

Conversation

@pushpak1300
Copy link

@pushpak1300 pushpak1300 commented Feb 15, 2026

This pull request adds AI agent detection that automatically switches PHPStan to raw error format and suppresses progress bar output when executed within Claude Code, Cursor, Gemini CLI, Codex, OpenCode, Augment, Replit, or Devin.

Why:

The default PHPStan output is designed for humans - colorful tables, progress bars with animations, and formatted summaries. AI agents need something different: minimal output that saves context window and reduces token cost, unambiguous error details with file paths and line numbers, and no progress bar animations that waste tokens on redraw noise.

Why raw format over JSON?

The raw format (file:line:message, one error per line) is significantly more token-efficient than JSON:

Scenario JSON Raw Token Savings
3 errors / 2 files ~185 tokens (678 chars) ~95 tokens (295 chars) ~49%
0 errors ~18 tokens (62 chars) 0 tokens (empty) 100%

JSON carries structural overhead per error (repeated keys like "message", "line", "ignorable", "identifier", plus nesting and punctuation). The raw format eliminates all of this while preserving the essential information agents need: file path, line number, and error message.

How it works:

  1. When Claude Code runs PHPStan, it sets the CLAUDECODE / CLAUDE_CODE environment variable
  2. When Cursor runs PHPStan, it sets CURSOR_TRACE_ID / CURSOR_AGENT
  3. Other agents set their own env vars (GEMINI_CLI, CODEX_SANDBOX, AI_AGENT, etc.)
  4. PHPStan detects these and automatically:
    • Switches to raw format (one error per line as file:line:message)
    • Suppresses progress bar output entirely (equivalent to --no-progress)

Detection is inlined (simple getenv() calls) to avoid any external dependency and maintain compatibility with all PHP versions PHPStan supports.

Output example:

/app/src/Controller/UserController.php:42:Parameter #1 $id expects int, string given.
/app/src/Service/PaymentService.php:18:Call to an undefined method proccess().

Explicit --error-format=table or config errorFormat: table always takes priority over agent detection.

Changes:

  • New AgentDetectedErrorFormatter class (follows CiDetectedErrorFormatter pattern) — delegates to RawErrorFormatter
  • Modified AnalyseCommand to check for agents when no explicit format is set
  • Modified ErrorsConsoleStyle to fully suppress progress bar for agents (no output at all, not just slower redraws)
  • Added tests for agent detection and raw output

@pushpak1300 pushpak1300 marked this pull request as ready for review February 15, 2026 12:56
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

@pushpak1300 pushpak1300 changed the title feat: adds AI agent detection to auto-switch to JSON output Add AI agent detection to auto-switch to JSON output Feb 15, 2026
@ondrejmirtes
Copy link
Member

@pushpak1300
Copy link
Author

pushpak1300 commented Feb 15, 2026

@ondrejmirtes Are you open for adding toon formatter ? happy to send the pr.

@ondrejmirtes
Copy link
Member

try the existing raw formatter, it's pretty minimal and works well I think

@pushpak1300 pushpak1300 changed the title Add AI agent detection to auto-switch to JSON output Add AI agent detection to auto-switch to raw output Feb 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants