Skip to content

Phpstan

In the world of PHP development, ensuring code quality and catching potential bugs early can save countless hours of debugging and troubleshooting. PHPStan is one of the most powerful tools available for this purpose. It provides static analysis for PHP code, helping developers detect issues before the code is even executed.


What is PHPStan?

PHPStan is a static analysis tool for PHP. Unlike traditional debugging tools that catch errors at runtime, PHPStan analyzes your code without executing it. It looks for potential issues, such as: - Undefined variables - Incorrect method calls - Type mismatches - Unused code - PHPDoc inconsistencies

It operates at different levels of strictness, allowing developers to progressively improve their code quality over time.

Why Use PHPStan?

βœ… Detect Errors Before Execution

PHPStan helps identify common PHP errors before your code runs, reducing the chances of production failures.

πŸš€ Improve Code Quality

By enforcing best practices and identifying unused code, PHPStan encourages cleaner, more maintainable codebases.

⚑ Enhance Performance

Finding and fixing inefficiencies during development leads to better performance and reduced debugging time.

🎯 Customize to Your Needs

PHPStan allows you to configure its rules and adjust strictness levels according to your project's needs.


Installing PHPStan

The simplest way to install PHPStan is via Composer. Run the following command in your project:

composer require --dev phpstan/phpstan

Once installed, you can check your code by running:

vendor/bin/phpstan analyse src/

Replace src/ with the directory containing your PHP code.


Configuring PHPStan

To customize PHPStan, create a phpstan.neon configuration file in your project's root directory:

parameters:
    level: 5  # Adjust the strictness level (0 to 9)
    paths:
        - src  # Directory to scan
    ignoreErrors:
        - '#Variable \$cfg might not be defined#'  # Example of ignoring specific errors

Then, run:

vendor/bin/phpstan analyse

Understanding PHPStan Levels

PHPStan operates at different levels of strictness, ranging from level 0 (basic checks) to level 9 (strictest checks). Here’s a quick overview:

Level Checks for
0 Basic syntax issues
1 Undefined variables, incorrect function calls
2 Undefined class properties
3 Function return types
4 More detailed type checks
5 Stricter array and object checks
6 Nullable types and advanced return value checks
7 More stringent checks on mixed types
8 Deep property analysis
9 Maximum strictness

Example Usage

Consider the following PHP code:

function greetUser(string $name) {
    return 'Hello, ' . $username; // Error: $username is undefined
}

echo greetUser('Alice');

Running PHPStan will output:

 ------ -------------------------------------
  Line   example.php
 ------ -------------------------------------
  2      Variable $username might not be defined.
 ------ -------------------------------------

To fix this, ensure the correct variable is used:

function greetUser(string $name) {
    return 'Hello, ' . $name;
}

Ignoring Specific Errors

If PHPStan reports false positives or issues you intentionally want to ignore, use ignoreErrors in phpstan.neon:

parameters:
  ignoreErrors:
    - '#Variable \$cfg might not be defined#'

This is useful for legacy codebases where fixing everything at once is impractical.


Advanced Features

PHPStan Extensions

PHPStan has various extensions for frameworks and tools, including: - Laravel: composer require --dev nunomaduro/larastan - Symfony: composer require --dev phpstan/phpstan-symfony - Doctrine: composer require --dev phpstan/phpstan-doctrine

Custom Rules

You can define custom rules to enforce your team’s coding standards. Check out the official PHPStan documentation for details.