Skip to content
WordPress extensions for PHPStan ⛏️
Branch: master
Clone or download

WordPress extensions for PHPStan

CircleCI Packagist


  1. Set up Composer, add szepeviktor/phpstan-wordpress, autoload your plugin or theme, see example/composer.json
  2. Set up PHPStan, see example/phpstan.neon - if you don't use Composer autoloading add autoload_files: and/or autoload_directories:
  3. Get packages composer update --classmap-authoritative
  4. Start analysis vendor/bin/phpstan analyze

Make you 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
  • Handle these only in your main plugin file
    • Define constants, e.g. MYPLUGIN_PATH
    • Call register_activation_hook, register_deactivation_hook, register_uninstall_hook
    • Class autoloading
    • Load translations
    • Support WP-CLI
    • Decide what to load
  • Avoid using core constants, use core functions or MYPLUGIN_PATH
  • Avoid bad parts of PHP
    • functions: extract, compact, list
  • If you need robust code try avoiding all kinds of type casting (e.g. if needs a boolean), see Variable handling functions
  • If you are not bound by PHP 5.x consider following Neutron PHP Standard
You can’t perform that action at this time.