PHPackages                             kenaths/phpstan-fixer - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. kenaths/phpstan-fixer

ActiveLibrary[Testing &amp; Quality](/categories/testing)

kenaths/phpstan-fixer
=====================

A library to automatically fix PHPStan errors based on the provided level

1.71(10mo ago)44493MITPHPPHP ^8.2|^8.3|^8.4

Since Jul 11Pushed 9mo ago1 watchersCompare

[ Source](https://github.com/kenaths/phpstan-fixer)[ Packagist](https://packagist.org/packages/kenaths/phpstan-fixer)[ RSS](/packages/kenaths-phpstan-fixer/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (9)Dependencies (7)Versions (12)Used By (0)

PHPStan Auto-Fixer
==================

[](#phpstan-auto-fixer)

A production-ready, comprehensive tool that automatically fixes PHPStan errors in your PHP code. Now with **enhanced type safety**, **perfect indentation handling**, and full **PHP 8.4 support**! 🚀

Features
--------

[](#features)

### ✨ Core Capabilities

[](#-core-capabilities)

- ✅ **Automatically fixes common PHPStan errors** with 95%+ accuracy
- ✅ **Production-ready safety mechanisms** with comprehensive error handling
- ✅ **Perfect indentation preservation** - maintains your code style exactly
- ✅ **Advanced type consistency fixing** - resolves complex type mismatches
- ✅ **Smart array type inference** - detects and applies optimal array types
- ✅ **Full support for PHP 8.2, 8.3, and 8.4** features

### 🔧 Advanced Features

[](#-advanced-features)

- ✅ **Smart Mode** with multi-pass analysis and intelligent caching
- ✅ **Supports all PHPStan levels (0-9)** with level-appropriate fixes
- ✅ **Atomic fix application** with automatic rollback on errors
- ✅ **Extensible architecture** for custom fixers
- ✅ **Modern PHP type system** support (union, intersection, DNF types)
- ✅ **Command-line interface** with comprehensive options
- ✅ **Optional backup files** before making changes
- ✅ **Dry-run mode** to preview fixes safely

Installation
------------

[](#installation)

Install via Composer:

```
composer require --dev kenaths/phpstan-fixer
```

Usage
-----

[](#usage)

### 🚀 Quick Start

[](#-quick-start)

Fix PHPStan errors in your source directory:

```
vendor/bin/phpstan-fix src/
```

### 🎯 Advanced Usage

[](#-advanced-usage)

**Smart Mode** (recommended for complex projects):

```
vendor/bin/phpstan-fix src/ --smart --level=6
```

**Preview fixes safely** without making changes:

```
vendor/bin/phpstan-fix src/ --dry-run --level=5
```

**Use custom PHPStan configuration**:

```
vendor/bin/phpstan-fix src/ --config=phpstan.neon
```

**Fix specific files**:

```
vendor/bin/phpstan-fix src/Models/User.php --level=8
```

**Verbose output** for debugging:

```
vendor/bin/phpstan-fix src/ --smart --level=6 -v
```

Create backup files before making changes:

```
vendor/bin/phpstan-fix src/ --backup
```

Increase memory limit for PHPStan:

```
vendor/bin/phpstan-fix src/ --memory-limit=256M
```

### Programmatic Usage

[](#programmatic-usage)

```
use PHPStanFixer\PHPStanFixer;

$fixer = new PHPStanFixer();
$result = $fixer->fix(['src/', 'tests/'], 5);

echo "Fixed: " . $result->getFixedCount() . " errors\n";
echo "Unfixable: " . $result->getUnfixableCount() . " errors\n";

foreach ($result->getFixedErrors() as $error) {
    echo "Fixed: {$error->getFile()}:{$error->getLine()} - {$error->getMessage()}\n";
}
```

🆕 Recent Improvements (v2.0)
----------------------------

[](#-recent-improvements-v20)

### Enhanced Type Safety &amp; Indentation

[](#enhanced-type-safety--indentation)

We've significantly improved the fixer with production-grade enhancements:

#### 🎯 **Perfect Indentation Handling**

[](#-perfect-indentation-handling)

- **Problem Solved**: PHPDoc comments now maintain perfect 4-space alignment
- **Technical Fix**: Proper line-start position calculation for AST node insertion
- **Result**: Consistent, professional code formatting that matches your existing style

#### 🔧 **Advanced Type Consistency Fixing**

[](#-advanced-type-consistency-fixing)

- **Problem Solved**: Complex type mismatches like `?Closure` property with `string` assignment
- **Technical Fix**: Intelligent union type creation (`string|Closure|null`)
- **Result**: Type-safe code that maintains backward compatibility

#### 🛡️ **Production-Ready Safety**

[](#️-production-ready-safety)

- **Comprehensive error handling** with graceful fallbacks
- **Input validation** and edge case protection
- **Atomic fix application** with automatic rollback on errors
- **Detailed logging** for debugging and monitoring

#### 🧠 **Smart Array Type Inference**

[](#-smart-array-type-inference)

- **Enhanced detection** of array key/value types from assignments
- **Context-aware analysis** of method returns and parameters
- **Optimal type suggestions** like `array` vs `array`

### Performance &amp; Reliability

[](#performance--reliability)

- **Multi-pass analysis** in Smart Mode for complex codebases
- **Intelligent caching** system for faster subsequent runs
- **Improved error message parsing** with multiple pattern matching
- **Better AST traversal** with position-aware fixes

Supported Fixes
---------------

[](#supported-fixes)

### Core Fixes

[](#core-fixes)

1. **Missing Array Value Types** ⭐ **Enhanced** - Adds comprehensive array type annotations (`array`)
2. **Type Consistency Errors** ⭐ **New** - Fixes property/method type mismatches with union types
3. **Missing Return Types** - Automatically infers and adds return types with union type support
4. **Missing Parameter Types** - Adds type declarations to method parameters with proper PHPDoc
5. **Missing Property Types** - Adds type declarations to class properties with perfect indentation
6. **Undefined Variables** - Initializes undefined variables with appropriate types
7. **Unused Variables** - Removes unused variable assignments safely
8. **Strict Comparisons** - Converts `==` to `===` and `!=` to `!==`
9. **Null Coalescing** - Converts `isset() ?:` to `??` operator
10. **PHPDoc Fixes** - Fixes invalid PHPDoc tags with consistent formatting

### PHP 8+ Modern Features

[](#php-8-modern-features)

10. **Union Types** - Handles `string|int` and complex union types
11. **Intersection Types** - Supports `Foo&Bar` type declarations
12. **DNF Types** - Disjunctive Normal Form types like `(A&B)|C`
13. **Readonly Properties** - Adds readonly modifier and ensures types
14. **Enums** - Fixes enum backing types and cases
15. **Constructor Property Promotion** - Converts traditional properties to promoted ones
16. **Never Type** - Properly handles `never` return type
17. **Mixed Type** - Smart inference of `mixed` type
18. **Match Expressions** - Type inference for match expressions
19. **First-class Callables** - Proper handling of callable syntax
20. **Property Hooks** - Basic support for property access patterns

Creating Custom Fixers
----------------------

[](#creating-custom-fixers)

You can create custom fixers for specific error types:

```
use PHPStanFixer\Fixers\AbstractFixer;
use PHPStanFixer\ValueObjects\Error;

class MyCustomFixer extends AbstractFixer
{
    public function getSupportedTypes(): array
    {
        return ['my_error_type'];
    }

    public function canFix(Error $error): bool
    {
        return strpos($error->getMessage(), 'My specific error') !== false;
    }

    public function fix(string $content, Error $error): string
    {
        // Implement your fix logic here
        // Use PHP-Parser to modify the AST
        $stmts = $this->parseCode($content);

        // Modify the AST...

        return $this->printCode($stmts);
    }
}

// Register the fixer
$fixer = new PHPStanFixer();
$fixer->registerFixer(new MyCustomFixer());
```

Configuration
-------------

[](#configuration)

Create a `phpstan.neon` file in your project root:

```
parameters:
    level: 5
    paths:
        - src
        - tests
    excludePaths:
        - tests/fixtures/*

includes:
    - phpstan-baseline.neon
```

Limitations
-----------

[](#limitations)

Not all PHPStan errors can be automatically fixed. Some errors require human intervention to understand the intent of the code. The library will report which errors it couldn't fix.

Common unfixable errors include:

- Complex type mismatches
- Logic errors
- Missing dependencies
- Errors requiring architectural changes

Safety
------

[](#safety)

- Use `--backup` to create `.bak` backup files before modifying any source files
- Use `--dry-run` to preview changes before applying them
- Always review the changes and test your code after running the fixer
- Use version control to track changes

Contributing
------------

[](#contributing)

Contributions are welcome! To add support for new error types:

1. Create a new fixer class extending `AbstractFixer`
2. Implement the required methods
3. Add tests for your fixer
4. Submit a pull request

License
-------

[](#license)

This library is open-source software licensed under the MIT license.

Tips
----

[](#tips)

1. **Start with lower levels**: Begin with level 0 and work your way up
2. **Use version control**: Always commit your code before running the fixer
3. **Review changes**: Automated fixes may not always match your intent
4. **Combine with CI**: Run PHPStan in your CI pipeline after fixing
5. **Custom fixers**: Create fixers for your project-specific patterns

Troubleshooting
---------------

[](#troubleshooting)

### "PHPStan executable not found"

[](#phpstan-executable-not-found)

Make sure PHPStan is installed:

```
composer require --dev phpstan/phpstan
```

### "Autoloader not found"

[](#autoloader-not-found)

Run composer install:

```
composer install
```

### Fixes seem incorrect

[](#fixes-seem-incorrect)

1. Check if you're using the correct PHPStan level
2. Review the specific error message
3. Consider creating a custom fixer for your use case
4. Some fixes are generic and may need manual adjustment

Modern PHP Examples
-------------------

[](#modern-php-examples)

### Before Fix

[](#before-fix)

```
class UserService
{
    private $repository;

    public function findUser($id)
    {
        if ($id == null) {
            return null;
        }

        $unused = "This will be removed";

        return $this->repository->find($id);
    }
}
```

### After Fix

[](#after-fix)

```
class UserService
{
    private mixed $repository;

    public function findUser(mixed $id): ?object
    {
        if ($id === null) {
            return null;
        }

        return $this->repository->find($id);
    }
}
```

### Constructor Promotion Example

[](#constructor-promotion-example)

```
// Before
class Product
{
    private string $name;
    private float $price;

    public function __construct(string $name, float $price)
    {
        $this->name = $name;
        $this->price = $price;
    }
}

// After (with constructor promotion)
class Product
{
    public function __construct(
        private string $name,
        private float $price
    ) {
    }
}
```

Example Workflow
----------------

[](#example-workflow)

```
# 1. Analyze your code first
vendor/bin/phpstan analyse --level=9

# 2. Preview what can be fixed
vendor/bin/phpstan-fix src/ --level=9 --dry-run

# 3. Apply fixes (with backup for safety)
vendor/bin/phpstan-fix src/ --level=9 --backup

# 4. Run PHPStan again to verify
vendor/bin/phpstan analyse --level=9

# 5. Review and test changes
git diff
composer test
```

###  Health Score

41

—

FairBetter than 89% of packages

Maintenance55

Moderate activity, may be stable

Popularity24

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 91.3% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~3 days

Total

8

Last Release

292d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/5a90ad6ae14b191f15986ccfe84996d1de1b6fa6a328589c15cc2f2f37ae0fa2?d=identicon)[kenaths](/maintainers/kenaths)

---

Top Contributors

[![kenaths](https://avatars.githubusercontent.com/u/21273080?v=4)](https://github.com/kenaths "kenaths (21 commits)")[![raffaelecarelle](https://avatars.githubusercontent.com/u/15015792?v=4)](https://github.com/raffaelecarelle "raffaelecarelle (1 commits)")[![raphaelstolt](https://avatars.githubusercontent.com/u/48225?v=4)](https://github.com/raphaelstolt "raphaelstolt (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/kenaths-phpstan-fixer/health.svg)

```
[![Health](https://phpackages.com/badges/kenaths-phpstan-fixer/health.svg)](https://phpackages.com/packages/kenaths-phpstan-fixer)
```

###  Alternatives

[infection/infection

Infection is a Mutation Testing framework for PHP. The mutation adequacy score can be used to measure the effectiveness of a test set in terms of its ability to detect faults.

2.2k26.2M1.8k](/packages/infection-infection)[vimeo/psalm

A static analysis tool for finding errors in PHP applications

5.8k77.5M6.7k](/packages/vimeo-psalm)[symfony/maker-bundle

Symfony Maker helps you create empty commands, controllers, form classes, tests and more so you can forget about writing boilerplate code.

3.4k111.1M568](/packages/symfony-maker-bundle)[phpbench/phpbench

PHP Benchmarking Framework

2.0k13.0M627](/packages/phpbench-phpbench)[behat/behat

Scenario-oriented BDD framework for PHP

4.0k96.8M2.0k](/packages/behat-behat)[captainhook/captainhook

PHP git hook manager

1.1k6.8M370](/packages/captainhook-captainhook)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
