@@ -7,7 +7,7 @@ Please support my work to avoid abandoning this package.
7
7
8
8
Thank you!
9
9
10
- # WordPress extensions for PHPStan
10
+ # WordPress Extensions for PHPStan
11
11
12
12
[ ![ Packagist stats] ( https://img.shields.io/packagist/dt/szepeviktor/phpstan-wordpress.svg )] ( https://packagist.org/packages/szepeviktor/phpstan-wordpress/stats )
13
13
[ ![ 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.
20
20
- [ PHPStan] ( https://phpstan.org/ )
21
21
- [ WordPress] ( https://wordpress.org/ )
22
22
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
+
23
38
## Installation
24
39
25
- Add this package to your project.
40
+ To use this extension, require it in [ Composer ] ( https://getcomposer.org/ ) :
26
41
27
42
``` bash
28
43
composer require --dev szepeviktor/phpstan-wordpress
29
44
```
30
45
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!
32
47
33
- ``` bash
34
- composer require --dev phpstan/extension-installer
35
- ```
48
+ ### Manual Installation
36
49
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:
38
51
39
52
``` neon
40
53
includes:
@@ -43,7 +56,8 @@ includes:
43
56
44
57
## Configuration
45
58
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:
47
61
48
62
``` neon
49
63
parameters:
@@ -53,37 +67,32 @@ parameters:
53
67
- inc/
54
68
```
55
69
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 ) .
57
71
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 ) !
60
73
61
74
## Usage
62
75
63
- Just start the analysis: ` vendor/bin/phpstan analyze `
64
- then fix an error and ` GOTO 10 ` !
76
+ Run the analysis with:
65
77
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
+ ```
68
81
69
- ## Usage in WooCommerce webshops
82
+ then fix an error and ` GOTO 10 ` !
70
83
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 )
72
86
73
- ## What this extension does
87
+ ### Usage in WooCommerce Webshops
74
88
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.
81
90
82
- ## Usage of an ` apply_filters() ` docblock
91
+ ### Usage of an ` apply_filters() ` Docblock
83
92
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 :
87
96
88
97
``` php
89
98
/**
@@ -95,32 +104,30 @@ Example:
95
104
$title = apply_filters( 'list_pages', $title, $page );
96
105
```
97
106
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 ` .
101
110
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.
104
112
105
- ## Make your code testable
113
+ ## Make Your Code Testable
106
114
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.
110
119
- 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) ... `
115
124
- If you need robust code try avoiding all kinds of type juggling (e.g. ` if ` needs a boolean),
116
125
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.
120
127
121
- ## Dirty corner (FAQ)
128
+ ## Dirty Corner (FAQ)
122
129
123
- WordPress uses conditional function and class definition for override purposes .
130
+ WordPress uses conditional function and class definition to allow overrides .
124
131
Use ` sed ` command to exclude function stubs when they are previously defined.
125
132
126
133
``` bash
0 commit comments