WordPress extensions for PHPStan

Fund package maintenance!

Installs: 198 510

Dependents: 167

Suggesters: 6

Security: 0

Stars: 103

Watchers: 5

Forks: 15

Open Issues: 1


v0.7.5 2021-03-19 21:23 UTC


Build Status Packagist Packagist stats PHPStan

Static analysis for the WordPress ecosystem.


Add this package to your project.

composer require --dev szepeviktor/phpstan-wordpress

Make PHPStan find it automatically using phpstan/extension-installer.

composer require --dev phpstan/extension-installer

Or manually include it in your phpstan.neon.

    - vendor/szepeviktor/phpstan-wordpress/extension.neon


Needs no extra configuration. 😃 Simply configure PHPStan - for example - this way.

    level: 5
        - plugin.php
        - inc/

Please read PHPStan Config Reference.

💡 Use Composer autoloader or a custom autoloader!


Just start the analysis: vendor/bin/phpstan analyze then fix an error and GOTO 10!

You find futher information in the example directory e.g. example/phpstan.neon.dist

Usage in WooCommerce webshops

Please see WooCommerce Stubs

What this extension does

  • Makes it possible to run PHPStan on WordPress plugins and themes
  • Loads php-stubs/wordpress-stubs package
  • Defines some core constants
  • Handles special functions and classes e.g. is_wp_error()

Make your code testable

  • Write clean OOP code: 1 class per file, no other code in class files outside class Name { ... }
  • Structure your code: uniform class names (WPCS or PSR-4), keep classes in a separate directory inc/
  • Add proper PHPDoc blocks to classes, properties, methods, functions
  • Choose your main plugin file parts.
  • Avoid using core constants, use core functions
  • Avoid bad parts of PHP
    • functions: eval, extract, compact, list
    • type juggling: $a = '15'; if ($a) ...
  • If you need robust code try avoiding all kinds of type juggling (e.g. if needs a boolean), see Variable handling functions
  • If you are not bound by PHP 5 consider following Neutron Standard
  • Do not enable exit_error in WP_CLI::launch or WP_CLI::runcommand to keep your code testable