All Projects → szepeviktor → phpstan-wordpress

szepeviktor / phpstan-wordpress

Licence: MIT license
WordPress extensions for PHPStan ⛏️

Programming Languages

PHP
23972 projects - #3 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to phpstan-wordpress

phpstan-phpspec
PhpSpec extension for PHPStan
Stars: ✭ 19 (-89.56%)
Mutual labels:  static-analysis, phpstan, phpstan-extension
phpstan-dba
PHPStan based SQL static analysis and type inference for the database access layer
Stars: ✭ 163 (-10.44%)
Mutual labels:  static-analysis, phpstan, phpstan-extension
vim-phpstan
A Vim plugin for PHPStan - https://github.com/phpstan/phpstan. It calls `phpstan` to do static analysis of your PHP code and displays the errors in Vim's quickfix list.
Stars: ✭ 26 (-85.71%)
Mutual labels:  static-analysis, phpstan
phpstan-enum
Enum class reflection extension for PHPStan
Stars: ✭ 42 (-76.92%)
Mutual labels:  phpstan, phpstan-extension
phpstan-nette
Nette Framework class reflection extension for PHPStan & framework-specific rules
Stars: ✭ 87 (-52.2%)
Mutual labels:  static-analysis, phpstan
phpstan-webmozart-assert
PHPStan extension for webmozart/assert
Stars: ✭ 132 (-27.47%)
Mutual labels:  static-analysis, phpstan
phpstan
PHP Static Analysis in Github Actions.
Stars: ✭ 41 (-77.47%)
Mutual labels:  static-analysis, phpstan
phpstan-symfony
*DEPRECATED* Symfony extension for PHPStan
Stars: ✭ 42 (-76.92%)
Mutual labels:  static-analysis, phpstan
woocommerce-stubs
WooCommerce function and class declaration stubs for static analysis.
Stars: ✭ 49 (-73.08%)
Mutual labels:  static-analysis, phpstan
Phpstan
PHP Static Analysis Tool - discover bugs in your code without running it!
Stars: ✭ 10,534 (+5687.91%)
Mutual labels:  static-analysis, phpstan
phpstan-extensions
Extensions for PHPStan
Stars: ✭ 61 (-66.48%)
Mutual labels:  static-analysis, phpstan
cmake-init
The missing CMake project initializer
Stars: ✭ 1,071 (+488.46%)
Mutual labels:  static-analysis
subpy
Python subsets
Stars: ✭ 41 (-77.47%)
Mutual labels:  static-analysis
aparoid
Static and dynamic Android application security analysis
Stars: ✭ 62 (-65.93%)
Mutual labels:  static-analysis
IntelliJ-Luanalysis
Type-safe Lua IDE Plugin for IntelliJ IDEA
Stars: ✭ 118 (-35.16%)
Mutual labels:  static-analysis
dmn-check
A tool which performs static analyses on Decision Model Notation (DMN) files to detect bugs
Stars: ✭ 34 (-81.32%)
Mutual labels:  static-analysis
dlint
Dlint is a tool for encouraging best coding practices and helping ensure Python code is secure.
Stars: ✭ 130 (-28.57%)
Mutual labels:  static-analysis
phpcs-psr4-sniff
[READ-ONLY] PHP_CodeSniffer sniff that checks class name matches PSR-4 project structure.
Stars: ✭ 23 (-87.36%)
Mutual labels:  static-analysis
Lyra
No description or website provided.
Stars: ✭ 23 (-87.36%)
Mutual labels:  static-analysis
gospal
Go static program analyser
Stars: ✭ 56 (-69.23%)
Mutual labels:  static-analysis

WordPress extensions for PHPStan

Build Status Packagist Packagist stats PHPStan

Static analysis for the WordPress ecosystem.

Installation

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.

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

Configuration

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

parameters:
    level: 5
    paths:
        - plugin.php
        - inc/

Please read PHPStan Config Reference.

💡 Use Composer autoloader or a custom autoloader!

Usage

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
  • Provides dynamic return type extensions for many core functions
  • Defines some core constants
  • Handles special functions and classes e.g. is_wp_error()
  • Validates the optional docblock that precedes a call to apply_filters() and treats the type of its first @param as certain

Usage of an apply_filters() docblock

WordPress core -- and the wider WordPress ecosystem -- uses PHPDoc docblocks in a non-standard manner to document the parameters passed to apply_filters(). Example:

/**
 * Filters the page title when creating an HTML drop-down list of pages.
 *
 * @param string  $title Page title.
 * @param WP_Post $page  Page data object.
 */
$title = apply_filters( 'list_pages', $title, $page );

This extension understands these docblocks when they're present in your code and uses them to instruct PHPStan to treat the return type of the filter as certain, according to the first @param tag. In the example above this means PHPStan treats the type of $title as string.

To make the best use of this feature, ensure that the type of the first @param tag in each of these such docblocks is accurate and correct.

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, and calls to apply_filters()
  • 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

Dirty corner (FAQ)

WordPress uses conditional function and class definition for override purposes. Use sed command to exclude function stubs when they are previously defined.

sed -i -e 's#function is_gd_image#function __is_gd_image#' vendor/php-stubs/wordpress-stubs/wordpress-stubs.php
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].