Static analysis for the WordPress ecosystem.
- Set up Composer, add
szepeviktor/phpstan-wordpress
, autoload your plugin or theme, seeexample/composer.json
- Set up PHPStan, see
example/phpstan.neon.dist
- if you don't use Composer autoloading addautoload_files:
and/orautoload_directories:
- Get packages
composer update --optimize-autoloader
- Start analysis
vendor/bin/phpstan analyze
Please see WooCommerce Stubs
- 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()
- 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
- Start your plugin in a hook (
plugins_loaded
) - without direct execution
- Define constants, e.g.
- Avoid using core constants, use core functions or
MYPLUGIN_PATH
- 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 casting (e.g.
if
needs a boolean), see Variable handling functions - If you are not bound by PHP 5.x consider following Neutron Standard