Framework-agnostic PHP library for automatically fixing PHPStan errors using static analysis rules. Works with Laravel, Symfony, CodeIgniter, and native PHP projects.
- Automatically detects and fixes common PHPStan errors
- Framework-agnostic (works with any PHP project)
- Offline-friendly (no AI or network access required)
- Suggest mode (preview changes) and Apply mode (write changes)
- Supports multiple fix strategies for different error types
- Configuration system - Control how each error type is handled (fix, ignore, or report)
composer require --dev lukaszzychal/phpstan-fixerBy default, the tool runs in suggest mode - it shows what would be changed without modifying files:
vendor/bin/phpstan-fixerThis is equivalent to:
vendor/bin/phpstan-fixer --mode=suggestPreview proposed fixes without modifying files (same as default):
vendor/bin/phpstan-fixer --mode=suggestNote: Suggest mode is safe to run - it only shows what would be changed and does NOT modify your files.
Apply fixes directly to files:
vendor/bin/phpstan-fixer --mode=applyWarning: Apply mode will modify your source files. Always review changes in suggest mode first!
If you already have a PHPStan JSON output file:
vendor/bin/phpstan-fixer --input=phpstan-output.json --mode=applySpecify a custom PHPStan command:
vendor/bin/phpstan-fixer --phpstan-command="vendor/bin/phpstan analyse src tests --level=5 --error-format=json" --mode=applyYou can configure how different error types are handled using a configuration file. Create phpstan-fixer.yaml or phpstan-fixer.json in your project root:
YAML Format (phpstan-fixer.yaml):
rules:
"Access to an undefined property":
action: "fix" # fix, ignore, or report
"Method has no return type":
action: "fix"
"Unknown class":
action: "ignore" # Don't fix and don't show
"Extra arguments":
action: "report" # Don't fix, but show in output
# Wildcard patterns
"Call to an undefined method *":
action: "fix"
# Regex patterns
"/.*magic.*/":
action: "report"
default:
action: "fix" # Default action for unmatched errorsJSON Format (phpstan-fixer.json):
{
"rules": {
"Access to an undefined property": {
"action": "fix"
},
"Method has no return type": {
"action": "fix"
},
"Unknown class": {
"action": "ignore"
},
"Extra arguments": {
"action": "report"
}
},
"default": {
"action": "fix"
}
}Configuration Actions:
fix(default) - Attempt to automatically fix the errorignore- Don't fix and don't display the error (silent ignore)report- Don't fix, but display in original PHPStan format
Using Configuration:
# Auto-discover configuration file
vendor/bin/phpstan-fixer
# Specify configuration file explicitly
vendor/bin/phpstan-fixer --config=phpstan-fixer.yamlPattern Matching:
- Exact match:
"Access to an undefined property"- matches exactly - Wildcard:
"Access to an undefined *"- matches with*as wildcard - Regex:
"/Access to an undefined \\w+/"- full PCRE regex pattern
YAML Support:
For YAML configuration files, you need either:
ext-yamlPHP extension (install viapecl install yaml), orsymfony/yamlpackage (add tocomposer.json)
đ Detailed Guide: See PHPStan Fixers Guide for comprehensive documentation on each fixer, including problem descriptions, examples, and usage scenarios.
The library automatically fixes the following types of PHPStan errors:
-
Missing Return Type (
MissingReturnDocblockFixer)- Adds
@returnannotations when return type is missing
- Adds
-
Missing Parameter Type (
MissingParamDocblockFixer)- Adds
@paramannotations for parameters without types
- Adds
-
Undefined Properties (
MissingPropertyDocblockFixer)- Adds
@propertyor@varannotations for undefined properties
- Adds
-
Eloquent Pivot Property (
UndefinedPivotPropertyFixer)- Adds
@property-readannotation for Laravel Eloquent$pivotproperty
- Adds
-
Collection Generics (
CollectionGenericDocblockFixer)- Adds generic type parameters to Collection types (e.g.,
Collection<int, mixed>)
- Adds generic type parameters to Collection types (e.g.,
-
Undefined Variables (
UndefinedVariableFixer)- Adds inline
@varannotations for undefined variables
- Adds inline
-
Missing Use Statements (
MissingUseStatementFixer)- Adds
usestatements for undefined classes
- Adds
-
Undefined Methods (
UndefinedMethodFixer)- Adds
@methodannotations for magic methods
- Adds
-
Missing Throws Annotation (
MissingThrowsDocblockFixer)- Adds
@throwsannotations when exceptions are thrown
- Adds
-
Callable Type Invocation (
CallableTypeFixer)- Adds
@param-immediately-invoked-callableor@param-later-invoked-callableannotations
- Adds
-
Mixin Annotation (
MixinFixer)- Adds
@mixin ClassNameannotation for classes using magic methods (__call,__get,__set) to delegate calls
- Adds
Before:
function calculateSum($a, $b) {
return $a + $b;
}After:
/**
* @return mixed
*/
function calculateSum($a, $b) {
return $a + $b;
}Before:
class User {
public function getName() {
return $this->name; // PHPStan error: undefined property
}
}After:
/**
* @property string $name
*/
class User {
public function getName() {
return $this->name;
}
}Before:
/**
* @return Collection
*/
function getItems() {
return collect([]);
}After:
/**
* @return Collection<int, mixed>
*/
function getItems() {
return collect([]);
}The tool works out of the box with default settings. All fix strategies are enabled by default.
- Runs PHPStan (or reads existing JSON output)
- Parses PHPStan JSON output into structured Issue objects
- Matches each issue to appropriate fix strategies
- Applies fixes using AST parsing and PHPDoc manipulation
- Shows preview (suggest mode) or writes changes (apply mode)
- PHP 8.0 or higher
- PHPStan (installed via Composer)
- nikic/php-parser (automatically installed)
vendor/bin/phpunitThe project uses GitHub Actions for continuous integration:
- CI Workflow: Runs tests on PHP 8.0-8.3, static analysis, and code style checks
- Release Workflow: Automatically creates GitHub releases on version tags
- Self-Test Workflow: Tests PHPStan Fixer on its own codebase
See .github/workflows/ for details.
This package uses PHP Compatibility Tester to ensure compatibility across different frameworks and PHP versions:
- Packagist: lukaszzychal/php-compatibility-tester
- GitHub: lukaszzychal/php-compatibility-tester
Compatibility tests run automatically:
- Monthly: On the 1st day of each month via GitHub Actions
- Manually: Trigger via GitHub Actions UI (workflow_dispatch)
- Locally: Run
vendor/bin/compatibility-tester testto test locally
This automatically tests phpstan-fixer against various frameworks (Laravel 11/12, Symfony 7/8, CodeIgniter 4/5) and PHP versions (8.1-8.4) to ensure it works correctly in different environments. Test reports are available as GitHub Actions artifacts.
To set up compatibility testing in your project:
-
Run the init command:
vendor/bin/compatibility-tester init
-
The command will:
- Create
.compatibility.ymlconfiguration file - Copy PHPUnit test templates to
tests/compatibility/ - Copy GitHub Actions workflow to
.github/workflows/compatibility-tests.yml - Copy test scripts to
scripts/
- Create
-
Edit
.compatibility.ymlto configure:- Your package name
- PHP versions to test
- Frameworks and versions
- Test scripts to run
Note: If the example configuration file is not found in the package, you can use the example from vendor/lukaszzychal/php-compatibility-tester/tests/fixtures/test-package/.compatibility.yml as a reference.
Contributions are welcome! Please feel free to submit a Pull Request.
There are three ways to run the tool:
-
vendor/bin/phpstan-fixer(default, no parameters)- Equivalent to
--mode=suggest - Shows what would be changed
- Does NOT modify files
- Safe to run - preview only
- Equivalent to
-
vendor/bin/phpstan-fixer --mode=suggest- Explicit suggest mode (same as default)
- Shows proposed changes
- Does NOT modify files
- Safe to run - preview only
-
vendor/bin/phpstan-fixer --mode=apply- Actually writes changes to files
- Modifies source code
- Use with caution - creates permanent changes
Recommendation: Always run with --mode=suggest first to preview changes before applying them.
If a fixer strategy cannot automatically fix an error, it will be displayed at the end of the output in PHPStan format. These are errors that require manual intervention or are not yet supported by any fixer strategy.
MIT License - see LICENSE file for details.
Ĺukasz Zychal
- Email: lukasz.zychal.dev@gmail.com
- GitHub Issues: Report issues and bugs
- Discussions: Join the discussion
Contributions are welcome! Please feel free to submit a Pull Request.
For bug reports and feature requests, please use the GitHub Issues page.