@@ -7,7 +7,7 @@ Please support my work to avoid abandoning this package.
77
88Thank you!
99
10- # WordPress extensions for PHPStan
10+ # WordPress Extensions for PHPStan
1111
1212[ ![ Packagist stats] ( https://img.shields.io/packagist/dt/szepeviktor/phpstan-wordpress.svg )] ( https://packagist.org/packages/szepeviktor/phpstan-wordpress/stats )
1313[ ![ Packagist] ( https://img.shields.io/packagist/v/szepeviktor/phpstan-wordpress.svg?color=239922&style=popout )] ( https://packagist.org/packages/szepeviktor/phpstan-wordpress )
@@ -20,21 +20,34 @@ Static analysis for the WordPress ecosystem.
2020- [ PHPStan] ( https://phpstan.org/ )
2121- [ WordPress] ( https://wordpress.org/ )
2222
23+ ## Features
24+
25+ - Enables PHPStan to analyze WordPress plugins and themes.
26+ - Loads the [ ` php-stubs/wordpress-stubs ` ] ( https://github.com/php-stubs/wordpress-stubs ) package.
27+ - Provides dynamic return type extensions for functions that are not covered in
28+ [ ` php-stubs/wordpress-stubs ` ] ( https://github.com/php-stubs/wordpress-stubs/blob/master/functionMap.php )
29+ - Defines some WordPress core constants.
30+ - Validates optional docblocks before ` apply_filters() ` and ` do_action() ` calls,
31+ treating the type of the first ` @param ` as definitive.
32+
33+ ## Requirements
34+
35+ - PHPStan 2.0 or higher
36+ - PHP 7.4 or higher (tested up to PHP 8.3)
37+
2338## Installation
2439
25- Add this package to your project.
40+ To use this extension, require it in [ Composer ] ( https://getcomposer.org/ ) :
2641
2742``` bash
2843composer require --dev szepeviktor/phpstan-wordpress
2944```
3045
31- Make PHPStan find it automatically using ` phpstan/extension-installer ` .
46+ If you also install [ phpstan/extension-installer] ( https://github.com/phpstan/extension-installer ) then you're all set!
3247
33- ``` bash
34- composer require --dev phpstan/extension-installer
35- ```
48+ ### Manual Installation
3649
37- Or manually include it in your ` phpstan.neon ` .
50+ If you don't want to use ` phpstan/extension-installer ` , include ` extension .neon` in your project's PHPStan config:
3851
3952``` neon
4053includes:
@@ -43,7 +56,8 @@ includes:
4356
4457## Configuration
4558
46- Needs no extra configuration. :smiley : Simply configure PHPStan - for example - this way.
59+ No additional setup is needed. :smiley :
60+ Just configure PHPStan - for example - as shown below:
4761
4862``` neon
4963parameters:
@@ -53,37 +67,32 @@ parameters:
5367 - inc/
5468```
5569
56- Please read [ PHPStan Config Reference] ( https://phpstan.org/config-reference ) .
70+ For more details, visit the [ PHPStan Config Reference] ( https://phpstan.org/config-reference ) .
5771
58- :bulb : Use Composer autoloader or a
59- [ custom autoloader] ( https://github.com/szepeviktor/debian-server-tools/blob/master/webserver/wp-install/wordpress-autoloader.php ) !
72+ :bulb : Use the Composer autoloader or a [ custom autoloader] ( https://github.com/szepeviktor/debian-server-tools/blob/master/webserver/wp-install/wordpress-autoloader.php ) !
6073
6174## Usage
6275
63- Just start the analysis: ` vendor/bin/phpstan analyze `
64- then fix an error and ` GOTO 10 ` !
76+ Run the analysis with:
6577
66- You find further information in the ` examples ` directory
67- e.g. [ ` examples/phpstan.neon.dist ` ] ( /examples/phpstan.neon.dist )
78+ ``` bash
79+ vendor/bin/phpstan analyze
80+ ```
6881
69- ## Usage in WooCommerce webshops
82+ then fix an error and ` GOTO 10 ` !
7083
71- Please see [ WooCommerce Stubs] ( https://github.com/php-stubs/woocommerce-stubs )
84+ You find further information in the ` examples ` directory,
85+ e.g. [ ` examples/phpstan.neon.dist ` ] ( /examples/phpstan.neon.dist )
7286
73- ## What this extension does
87+ ### Usage in WooCommerce Webshops
7488
75- - Makes it possible to run PHPStan on WordPress plugins and themes
76- - Loads [ ` php-stubs/wordpress-stubs ` ] ( https://github.com/php-stubs/wordpress-stubs ) package
77- - Provides dynamic return type extensions for functions
78- that are not covered in [ ` php-stubs/wordpress-stubs ` ] ( https://github.com/php-stubs/wordpress-stubs/blob/master/functionMap.php )
79- - Defines some core constants
80- - Validates the optional docblock that precedes a call to ` apply_filters() ` and treats the type of its first ` @param ` as certain
89+ Refer to [ WooCommerce Stubs] ( https://github.com/php-stubs/woocommerce-stubs ) for specific guidance.
8190
82- ## Usage of an ` apply_filters() ` docblock
91+ ### Usage of an ` apply_filters() ` Docblock
8392
84- WordPress core - and the wider WordPress ecosystem - uses PHPDoc docblocks
85- in a non-standard manner to document the parameters passed to ` apply_filters() ` .
86- Example :
93+ The WordPress ecosystem often uses PHPDoc docblocks in a non-standard way to
94+ document parameters passed to ` apply_filters() ` .
95+ Here’s an example :
8796
8897``` php
8998/**
@@ -95,32 +104,30 @@ Example:
95104$title = apply_filters( 'list_pages', $title, $page );
96105```
97106
98- This extension understands these docblocks when they're present in your code
99- and uses them to instruct PHPStan to treat the return type of the filter as certain ,
100- according to the first ` @param ` tag. In the example above this means PHPStan treats the type of ` $title ` as ` string ` .
107+ This extension reads these docblocks and instructs PHPStan to treat the filter’s
108+ return type as certain, based on the first ` @param ` tag. In this example ,
109+ PHPStan interprets ` $title ` as ` string ` .
101110
102- To make the best use of this feature,
103- ensure that the type of the first ` @param ` tag in each of these such docblocks is accurate and correct.
111+ For best results, ensure the first ` @param ` tag in these docblocks is accurate.
104112
105- ## Make your code testable
113+ ## Make Your Code Testable
106114
107- - Write clean OOP code: 1 class per file, no other code in class files outside ` class Name { ... } `
108- - Structure your code: uniform class names (WPCS or PSR-4), keep classes in a separate directory ` inc/ `
109- - Add proper PHPDoc blocks to classes, properties, methods, functions, and calls to ` apply_filters() `
115+ - Write clean OOP code: 1 class per file, and no additional code outside ` class Name { ... } ` .
116+ - Use consistent class naming (WPCS or PSR-4) and store classes in a dedicated ` inc/ ` directory.
117+ - Add precise PHPDoc blocks to classes, properties, methods, functions, and
118+ ` apply_filters() ` calls.
110119- Choose your [ main plugin file parts] ( https://github.com/szepeviktor/starter-plugin ) .
111- - Avoid using core constants, use core functions
112- - Avoid bad parts of PHP
113- - functions: ` eval ` , ` extract ` , ` compact ` , ` list `
114- - [ type juggling] ( https://www.php.net/manual/en/language.types.type-juggling.php ) : ` $a = '15'; if ($a) ... `
120+ - Avoid using core constants, use core functions.
121+ - Avoid bad PHP practices, such as:
122+ - functions: ` eval ` , ` extract ` , ` compact ` , ` list `
123+ - [ type juggling] ( https://www.php.net/manual/en/language.types.type-juggling.php ) : ` $a = '15'; if ($a) ... `
115124- If you need robust code try avoiding all kinds of type juggling (e.g. ` if ` needs a boolean),
116125 see [ Variable handling functions] ( https://www.php.net/manual/en/ref.var.php )
117- - If you are not bound by PHP 5 consider following
118- [ Neutron Standard] ( https://github.com/Automattic/phpcs-neutron-standard )
119- - Do not enable ` exit_error ` in ` WP_CLI::launch ` or ` WP_CLI::runcommand ` to keep your code testable
126+ - Avoid enabling ` exit_error ` in ` WP_CLI::launch ` or ` WP_CLI::runcommand ` for improved testability.
120127
121- ## Dirty corner (FAQ)
128+ ## Dirty Corner (FAQ)
122129
123- WordPress uses conditional function and class definition for override purposes .
130+ WordPress uses conditional function and class definition to allow overrides .
124131Use ` sed ` command to exclude function stubs when they are previously defined.
125132
126133``` bash
0 commit comments