A static analyzer for PHP that minimizes false positives
open sourceMITfree
Phan is a static analyzer for PHP that prefers to avoid false positives: it tries to prove code incorrect rather than correct. It checks that methods, functions, classes, constants, properties and variables are defined and accessible, type safety and arity on calls and binary operations, return values, inheritance and signature compatibility, and backward compatibility across PHP 8.1 to 8.6, and can find dead, unreachable and unused code, unused variables and redundant conditions, fixing some issue types automatically. It understands union and generic types, array shapes and PHPDoc annotations, runs user and bundled plugins, and works in editors through the Language Server Protocol or its daemon mode. You install it with Composer and run phan --init to create a config.
Problems it solves
- find type errors in my PHP code before running it
- check my PHP codebase for dead code and unused variables
- catch PHP code that breaks on an older PHP 8 version
Features
- type safety and arity checks on calls, operations and return values
- backward compatibility checks for PHP 8.1 to 8.6
- dead, unreachable and unused code detection, with automatic fixes for some issues
- union types, generics (@template), array shapes and PHPDoc annotations
- user and bundled plugins
- editor integration through the Language Server Protocol and a daemon mode
Limitations
- Phan 6 needs PHP 8.1 or newer with the php-ast extension (a polyfill parser works without it, with slight differences in doc comment parsing)1
Alternatives
The same job
These do what Phan does.
- Psalm Free and open source static analysis tool for finding errors in PHP code
- PHPStan PHP static analysis that finds bugs without running your code
- Larastan Adds code analysis to Laravel, improving developer productivity and code quality
- WordPress Extensions for PHPStan Static analysis for WordPress plugins and themes with PHPStan
- mypy Optional static typing for Python
