webkult/phpstan-ai-formatter
Composer 安装命令:
composer require webkult/phpstan-ai-formatter
包简介
Compact PHPStan error formatter designed for AI coding assistants. Transforms verbose PHPStan output into token-efficient format. Ideal for Claude, ChatGPT, Copilot and similar tools. Saves context window space while preserving all essential error information.
README 文档
README
🤖 AI-optimized error formatter for PHPStan - Reduce output by up to 80% for AI context windows.
Perfect for Claude, ChatGPT, GitHub Copilot, Cursor, and other AI coding assistants.
🚀 Quick Start
Option 1: MCP Server (Recommended for AI Assistants)
Enable your AI assistant to run PHPStan directly:
npm install -g @webkult/phpstan-ai-mcp-server
Then configure your AI tool (Claude, Cursor, etc.) - see MCP Setup Guide
Option 2: PHPStan Extension (Command Line)
Install in your PHP project:
composer require --dev webkult/phpstan-ai-formatter phpstan analyse --error-format=ai
Why?
Standard PHPStan output is verbose and wastes precious tokens when working with AI assistants:
------ -----------------------------------------------------------------------
Line src/UserService.php
------ -----------------------------------------------------------------------
23 Parameter #1 $user of method App\UserRepository::save() expects
App\Entity\User, App\Entity\User|null given.
45 Method App\UserService::getUser() should return App\Entity\User
but returns App\Entity\User|null.
------ -----------------------------------------------------------------------
This formatter produces compact, token-efficient output:
src/UserService.php:23 | Method param $user: User|null ≠ User
src/UserService.php:45 | getUser() → User|null (expected User)
Result: ~80% fewer tokens, same diagnostic value!
Installation
composer require --dev webkult/phpstan-ai-formatter
The extension is automatically registered via phpstan/extension-installer.
Usage
Compact Text Format
Best for human-readable AI conversations:
phpstan analyse --error-format=ai
Output example:
PHPStan Analysis (config: phpstan.neon)
Found: 2 errors, 0 warnings
Errors
------
src/UserService.php:23 | Method param $user: User|null ≠ User
→ Consider using strict types
src/UserService.php:45 | getUser() → User|null (expected User)
Structured JSON Format
Best for programmatic AI processing:
phpstan analyse --error-format=ai-json
Output example:
{
"totals": {
"errors": 2,
"file_errors": 2,
"warnings": 0,
"internal_errors": 0
},
"files": {
"src/UserService.php": [
{
"line": 23,
"message": "Method param $user: User|null ≠ User",
"tip": "Consider using strict types",
"ignorable": true
},
{
"line": 45,
"message": "getUser() → User|null (expected User)",
"ignorable": false
}
]
},
"warnings": []
}
MCP Server Integration
Model Context Protocol (MCP) enables AI assistants to run PHPStan directly without manual commands.
Supported AI Tools
- ✅ Claude Desktop - Official Anthropic desktop app
- ✅ Claude Code - Official CLI tool
- ✅ Cursor - AI-first code editor
- ✅ VSCode with Cline - AI coding assistant extension
- ✅ Windsurf - Codeium's AI IDE
- ✅ Continue.dev - Open-source AI assistant
How It Works
- Install MCP server:
npm install -g @webkult/phpstan-ai-mcp-server - Configure your AI tool (one-time setup)
- Ask your AI: "Analyze my PHP code for errors"
- AI runs PHPStan automatically and suggests fixes
Complete setup guide: MCP Server Setup
Use Cases
🤖 AI-Assisted Code Review
# Generate compact report phpstan analyse --error-format=ai > phpstan-report.txt # Send to your AI assistant cat phpstan-report.txt | pbcopy
Then paste into Claude/ChatGPT with your prompt:
"Here's my PHPStan analysis. Help me fix these issues systematically."
🔄 CI/CD Integration
Add to your GitHub Actions workflow:
- name: Run PHPStan run: | vendor/bin/phpstan analyse --error-format=ai-json > phpstan.json - name: Comment on PR with AI analysis run: | # Send compact JSON to your AI service for automated code review curl -X POST $AI_REVIEW_ENDPOINT -d @phpstan.json
📝 Documentation Generation
phpstan analyse --error-format=ai-json | jq '.files | to_entries[]' | \ ai-tool "Generate documentation for fixing these common errors"
Token Savings Comparison
Tested on a typical Laravel project with 50 errors:
| Format | Token Count | Reduction |
|---|---|---|
Standard table |
~12,500 | baseline |
Standard json |
~8,200 | 34% |
| AI compact | ~2,800 | 78% |
| AI JSON | ~2,400 | 81% |
Token counts measured using GPT-4 tokenizer
Message Shortening Examples
The formatter intelligently abbreviates common patterns:
| Original | Shortened |
|---|---|
Parameter #1 $user of method save() expects User, User|null given |
Method param $user: User|null ≠ User |
Method getUser() should return User but returns User|null |
getUser() → User|null (expected User) |
Call to an undefined method Repository::find() |
Undefined method: find() |
Access to an undefined property User::$email |
Undefined property: $email |
string|int|float|bool|array|null |
string|int|float|... |
Configuration
The formatter works out of the box with zero configuration. However, you can customize PHPStan's behavior:
# phpstan.neon includes: - vendor/webkult/phpstan-ai-formatter/extension.neon parameters: level: max paths: - src
Requirements
- PHP 7.4 or higher
- PHPStan 1.0 or higher
Tips for AI Workflows
- Use JSON format for automation - Easier to parse programmatically
- Use text format for conversations - More natural for chat interfaces
- Pipe directly to AI tools - Skip manual copy/paste
- Combine with git diff - Only analyze changed files
- Set up pre-commit hooks - Get instant AI feedback
Example: Git Pre-commit Hook
#!/bin/bash # .git/hooks/pre-commit # Get changed PHP files FILES=$(git diff --cached --name-only --diff-filter=ACM | grep '\.php$') if [ -z "$FILES" ]; then exit 0 fi # Run PHPStan on changed files only vendor/bin/phpstan analyse --error-format=ai $FILES # Optionally: Send to AI for auto-review # vendor/bin/phpstan analyse --error-format=ai-json $FILES | your-ai-tool
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Development Setup
git clone https://github.com/webkult/phpstan-ai-formatter.git
cd phpstan-ai-formatter
composer install
Running Tests
composer test
License
MIT License - see LICENSE file for details.
Acknowledgments
- Built for the PHPStan static analysis tool
- Optimized for Claude, ChatGPT, and other AI assistants
- Inspired by the need for more efficient AI-assisted development workflows
Related Projects
- PHPStan - The main static analysis tool
- Larastan - PHPStan for Laravel
- Psalm - Alternative static analysis tool
Made with 🤖 for developers working with AI assistants
Found this useful? Star the repo and share with your team! ⭐
webkult/phpstan-ai-formatter 适用场景与选型建议
webkult/phpstan-ai-formatter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 02 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「formatter」 「static-analysis」 「ai」 「PHPStan」 「ChatGpt」 「llm」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 webkult/phpstan-ai-formatter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 webkult/phpstan-ai-formatter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 webkult/phpstan-ai-formatter 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Doctrine Criteria extensions for PHPStan
PHPStan for Silverstripe
PHPStan extensions for Containers
Yii2 phone formatter and validator.
Custom laravel monolog logger for datadog logs management, both api and agent ways
Tool for dividing stub files for static analysis
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 52
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-02-11