ticketswap/phpstan-error-formatter
Minimalistic PHPStan error formatter with per-error clickable file/line links, non-wrapping output, naive syntax highlighting, and visually truncated long paths while preserving links. Easy install via Composer; set PHPStan errorFormat to ticketswap.
Install the package via Composer:
composer require --dev ticketswap/phpstan-error-formatter
Use phpstan/extension-installer for automatic configuration (recommended):
composer require --dev phpstan/extension-installer
Configure PHPStan in phpstan.neon:
parameters:
errorFormat: ticketswap
editorUrl: 'phpstorm://open?file=%%file%%&line=%%line%%' # Replace with your IDE's URL scheme
Run PHPStan as usual:
vendor/bin/phpstan analyse src
App\Models\User), and types (e.g., string, int).src/App/.../User.php).Daily Static Analysis
# Example GitHub Actions step
- name: Run PHPStan
run: vendor/bin/phpstan analyse --level=10 src
--error-format=ticketswap as a CLI flag for ad-hoc checks:
vendor/bin/phpstan analyse --error-format=ticketswap app/Http/Controllers/
IDE-Specific Configuration
phpstorm://open?file=%%file%%&line=%%line%%.vscode://file/%%file%%:%%line%%.subl://open?url=file://%%file%%&line:%%line%%.editorUrl to rely on clickable links (works in iTerm2, Alacritty, etc.).Team Onboarding
composer.json dev dependencies and document the editorUrl setup in CONTRIBUTING.md.## PHPStan Setup
1. Install dependencies: `composer install`.
2. Configure your IDE’s URL scheme in `phpstan.neon` (see [editorUrl docs](https://phpstan.org/user-guide/output-format)).
CI/CD Optimization
- name: Cache PHPStan
uses: actions/cache@v3
with:
path: ~/.phpstan
key: phpstan-${{ hashFiles('**/composer.lock') }}
--memory-limit=1G to reduce memory usage in pipelines.Custom Error Highlighting
// app/Extensions/CustomErrorFormatter.php
use TicketSwap\PhpStanErrorFormatter\TicketSwapErrorFormatter;
class CustomErrorFormatter extends TicketSwapErrorFormatter {
protected function getHighlightedText(string $text): string {
$text = parent::getHighlightedText($text);
// Add custom highlighting (e.g., for Laravel-specific types)
$text = preg_replace('/\bIlluminate\\\\\\\\.*?\\\\\\\\\\w+/', '<fg=cyan>$0</>', $text);
return $text;
}
}
phpstan.neon:
includes:
- vendor/ticketswap/phpstan-error-formatter/extension.neon
- app/Extensions/CustomErrorFormatter.neon
Terminal Link Support
cmd, some CI environments).file:// URLs as a fallback or configure editorUrl to open files manually.echo -e "\e]8;;https://example.com\aClick me\e]8;;\a" in your terminal to verify support.Path Truncation
/var/www/project/src/.../Deep/Nested/File.php) may truncate visually but remain clickable.editorUrl to handle absolute paths if needed (e.g., vscode://file/%%file%% works with relative paths).IDE-Specific Quirks
vscode:// URL scheme is enabled in settings.json:
{
"terminal.integrated.enablePersistentSessions": true
}
Windows Path Handling
C:\project\src\File.php) may break clickable links.editorUrl:
editorUrl: 'vscode://file/%%file:gs|\\|/%%:%%line%%'
PHPStan Level 10+ Noise
phpstan.neon:
rules:
PhpStan\Rules\UnusedVariableRule: false
Verify Formatter Activation
vendor/bin/phpstan analyse --error-format=ticketswap --no-progress
Inspect Raw Errors
--generate-report=json to debug raw PHPStan output:
vendor/bin/phpstan analyse --generate-report=json > phpstan-report.json
Custom Highlighting Debugging
error_log('Highlighted text: ' . $this->getHighlightedText($text));
CI/CD Logging
env:
FORCE_COLOR: 1 # Ensures color output in CI
Override Highlighting Rules
TicketSwapErrorFormatter to add custom regex patterns for highlighting (e.g., Laravel facades):
protected function getHighlightedText(string $text): string {
$text = parent::getHighlightedText($text);
$text = preg_replace('/\bFacade\\\\\\\\.*?::\w+/', '<fg=yellow>$0</>', $text);
return $text;
}
Custom Error Grouping
report parameter to group errors by file:
parameters:
report: 'grouped'
Integrate with Git Hooks
pre-commit hook to run PHPStan with the formatter:
# .git/hooks/pre-commit
#!/bin/sh
vendor/bin/phpstan analyse --error-format=ticketswap --level=8 --no-progress
if [ $? -ne 0 ]; then
echo "PHPStan errors found. Fix them before committing."
exit 1
fi
Combine with Other Tools
grep for specific errors:
vendor/bin/phpstan analyse --error-format=ticketswap 2>&1 | grep "Parameter #1"
Editor URL Schemes
open %%file%%:%%line%%.nautilus %%file%%:%%line%%.idea://open?file=%%file%%&line=%%line%%.Disable Path Truncation
protected bool $truncatePaths = false;
Force Decorated Output
parameters:
colors: true
Windows-Specific Fixes
str_replace to normalize paths in custom formatters:
$filePath = str_replace('\\', '/', $filePath
How can I help you explore Laravel packages today?