From 956d2c22f3b697614113d4aa1a180fd0695e0a3b Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Wed, 23 Mar 2022 21:28:40 +0000 Subject: [PATCH 01/16] Add the ability to provide additional parameter information for stubs. --- visitor.php | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/visitor.php b/visitor.php index 54456c9..1ec6f8b 100644 --- a/visitor.php +++ b/visitor.php @@ -19,6 +19,16 @@ */ private $docBlockFactory; + /** + * @var array> + */ + private $additionalParams; + + /** + * @var string + */ + private $currentSymbolName; + public function __construct() { $this->docBlockFactory = \phpDocumentor\Reflection\DocBlockFactory::createInstance(); @@ -38,12 +48,25 @@ public function enterNode(Node $node) return null; } + $this->currentSymbolName = $node->name->name; $newDocComment = $this->addArrayHashNotation($docComment); if ($newDocComment !== null) { $node->setDocComment($newDocComment); } + $docComment = $node->getDocComment(); + + if (!($docComment instanceof Doc)) { + return null; + } + + $newDocComment = $this->addAdditionalParams($docComment); + + if ($newDocComment !== null) { + $node->setDocComment($newDocComment); + } + return null; } @@ -100,6 +123,43 @@ private function addArrayHashNotation(Doc $docComment): ?Doc return new Doc($newDocComment, $docComment->getLine(), $docComment->getFilePos()); } + private function addAdditionalParams(Doc $docComment): ?Doc + { + if (! isset($this->additionalParams)) { + $this->additionalParams = require __DIR__ . '/additionalParams.php'; + } + + if (! isset($this->additionalParams[$this->currentSymbolName])) { + return null; + } + + $params = $this->additionalParams[$this->currentSymbolName]; + $returnType = array_shift($params); + $additions = []; + + foreach ($params as $param => $paramType) { + $additions[] = sprintf( + '@phpstan-param %s $%s', + $paramType, + $param + ); + } + + $additions[] = sprintf( + '@phpstan-return %s', + $returnType + ); + + $docCommentText = $docComment->getText(); + $newDocComment = sprintf( + "%s\n * %s\n */", + substr($docCommentText, 0, -4), + implode("\n * ", $additions) + ); + + return new Doc($newDocComment, $docComment->getLine(), $docComment->getFilePos()); + } + private function getAdditionFromParam(Param $tag): ?string { $tagDescription = $tag->getDescription(); From 873417aa7b9ce236d2e0b30ecbe65f7fa9d81f74 Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Wed, 23 Mar 2022 21:35:27 +0000 Subject: [PATCH 02/16] Add more specific return types for the HTTP API functions. --- additionalParams.php | 10 ++++++++++ wordpress-stubs.php | 12 ++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 additionalParams.php diff --git a/additionalParams.php b/additionalParams.php new file mode 100644 index 0000000..a4b9f96 --- /dev/null +++ b/additionalParams.php @@ -0,0 +1,10 @@ + [$httpReturnType, 'url'=>'string', 'args'=>'array'], + 'wp_remote_head' => [$httpReturnType, 'url'=>'string', 'args'=>'array'], + 'wp_remote_post' => [$httpReturnType, 'url'=>'string', 'args'=>'array'], + 'wp_remote_request' => [$httpReturnType, 'url'=>'string', 'args'=>'array'], +]; diff --git a/wordpress-stubs.php b/wordpress-stubs.php index 9587907..a2a06e9 100644 --- a/wordpress-stubs.php +++ b/wordpress-stubs.php @@ -104097,6 +104097,9 @@ function wp_safe_remote_head($url, $args = array()) * cookies: WP_HTTP_Cookie[], * http_response: WP_HTTP_Requests_Response|null, * } + * @phpstan-param string $url + * @phpstan-param array $args + * @phpstan-return array{headers:array,body:string,response:array{code:int,message:string},cookies:WP_HTTP_Cookie[],filename:string|null}|WP_Error */ function wp_remote_request($url, $args = array()) { @@ -104112,6 +104115,9 @@ function wp_remote_request($url, $args = array()) * @param string $url URL to retrieve. * @param array $args Optional. Request arguments. Default empty array. * @return array|WP_Error The response or WP_Error on failure. + * @phpstan-param string $url + * @phpstan-param array $args + * @phpstan-return array{headers:array,body:string,response:array{code:int,message:string},cookies:WP_HTTP_Cookie[],filename:string|null}|WP_Error */ function wp_remote_get($url, $args = array()) { @@ -104127,6 +104133,9 @@ function wp_remote_get($url, $args = array()) * @param string $url URL to retrieve. * @param array $args Optional. Request arguments. Default empty array. * @return array|WP_Error The response or WP_Error on failure. + * @phpstan-param string $url + * @phpstan-param array $args + * @phpstan-return array{headers:array,body:string,response:array{code:int,message:string},cookies:WP_HTTP_Cookie[],filename:string|null}|WP_Error */ function wp_remote_post($url, $args = array()) { @@ -104142,6 +104151,9 @@ function wp_remote_post($url, $args = array()) * @param string $url URL to retrieve. * @param array $args Optional. Request arguments. Default empty array. * @return array|WP_Error The response or WP_Error on failure. + * @phpstan-param string $url + * @phpstan-param array $args + * @phpstan-return array{headers:array,body:string,response:array{code:int,message:string},cookies:WP_HTTP_Cookie[],filename:string|null}|WP_Error */ function wp_remote_head($url, $args = array()) { From fc6e3831dff4286059cba57f218ca0184d3b9152 Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Wed, 23 Mar 2022 22:00:40 +0000 Subject: [PATCH 03/16] More specificity. --- additionalParams.php | 2 +- wordpress-stubs.php | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/additionalParams.php b/additionalParams.php index a4b9f96..267b52e 100644 --- a/additionalParams.php +++ b/additionalParams.php @@ -1,6 +1,6 @@ ,filename:string|null,http_response:WP_HTTP_Requests_Response}|WP_Error'; return [ 'wp_remote_get' => [$httpReturnType, 'url'=>'string', 'args'=>'array'], diff --git a/wordpress-stubs.php b/wordpress-stubs.php index a2a06e9..9b708cb 100644 --- a/wordpress-stubs.php +++ b/wordpress-stubs.php @@ -104099,7 +104099,7 @@ function wp_safe_remote_head($url, $args = array()) * } * @phpstan-param string $url * @phpstan-param array $args - * @phpstan-return array{headers:array,body:string,response:array{code:int,message:string},cookies:WP_HTTP_Cookie[],filename:string|null}|WP_Error + * @phpstan-return array{headers:Requests_Utility_CaseInsensitiveDictionary,body:string,response:array{code:int,message:string},cookies:array,filename:string|null,http_response:WP_HTTP_Requests_Response}|WP_Error */ function wp_remote_request($url, $args = array()) { @@ -104117,7 +104117,7 @@ function wp_remote_request($url, $args = array()) * @return array|WP_Error The response or WP_Error on failure. * @phpstan-param string $url * @phpstan-param array $args - * @phpstan-return array{headers:array,body:string,response:array{code:int,message:string},cookies:WP_HTTP_Cookie[],filename:string|null}|WP_Error + * @phpstan-return array{headers:Requests_Utility_CaseInsensitiveDictionary,body:string,response:array{code:int,message:string},cookies:array,filename:string|null,http_response:WP_HTTP_Requests_Response}|WP_Error */ function wp_remote_get($url, $args = array()) { @@ -104135,7 +104135,7 @@ function wp_remote_get($url, $args = array()) * @return array|WP_Error The response or WP_Error on failure. * @phpstan-param string $url * @phpstan-param array $args - * @phpstan-return array{headers:array,body:string,response:array{code:int,message:string},cookies:WP_HTTP_Cookie[],filename:string|null}|WP_Error + * @phpstan-return array{headers:Requests_Utility_CaseInsensitiveDictionary,body:string,response:array{code:int,message:string},cookies:array,filename:string|null,http_response:WP_HTTP_Requests_Response}|WP_Error */ function wp_remote_post($url, $args = array()) { @@ -104153,7 +104153,7 @@ function wp_remote_post($url, $args = array()) * @return array|WP_Error The response or WP_Error on failure. * @phpstan-param string $url * @phpstan-param array $args - * @phpstan-return array{headers:array,body:string,response:array{code:int,message:string},cookies:WP_HTTP_Cookie[],filename:string|null}|WP_Error + * @phpstan-return array{headers:Requests_Utility_CaseInsensitiveDictionary,body:string,response:array{code:int,message:string},cookies:array,filename:string|null,http_response:WP_HTTP_Requests_Response}|WP_Error */ function wp_remote_head($url, $args = array()) { From 4bfb6fb3dd8862fc6df7a908d41e22e020e7bf28 Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Wed, 23 Mar 2022 22:02:04 +0000 Subject: [PATCH 04/16] No need to override these parameters. --- additionalParams.php | 8 ++++---- wordpress-stubs.php | 8 -------- 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/additionalParams.php b/additionalParams.php index 267b52e..20feb88 100644 --- a/additionalParams.php +++ b/additionalParams.php @@ -3,8 +3,8 @@ $httpReturnType = 'array{headers:Requests_Utility_CaseInsensitiveDictionary,body:string,response:array{code:int,message:string},cookies:array,filename:string|null,http_response:WP_HTTP_Requests_Response}|WP_Error'; return [ - 'wp_remote_get' => [$httpReturnType, 'url'=>'string', 'args'=>'array'], - 'wp_remote_head' => [$httpReturnType, 'url'=>'string', 'args'=>'array'], - 'wp_remote_post' => [$httpReturnType, 'url'=>'string', 'args'=>'array'], - 'wp_remote_request' => [$httpReturnType, 'url'=>'string', 'args'=>'array'], + 'wp_remote_get' => [$httpReturnType], + 'wp_remote_head' => [$httpReturnType], + 'wp_remote_post' => [$httpReturnType], + 'wp_remote_request' => [$httpReturnType], ]; diff --git a/wordpress-stubs.php b/wordpress-stubs.php index 9b708cb..5107c7c 100644 --- a/wordpress-stubs.php +++ b/wordpress-stubs.php @@ -104097,8 +104097,6 @@ function wp_safe_remote_head($url, $args = array()) * cookies: WP_HTTP_Cookie[], * http_response: WP_HTTP_Requests_Response|null, * } - * @phpstan-param string $url - * @phpstan-param array $args * @phpstan-return array{headers:Requests_Utility_CaseInsensitiveDictionary,body:string,response:array{code:int,message:string},cookies:array,filename:string|null,http_response:WP_HTTP_Requests_Response}|WP_Error */ function wp_remote_request($url, $args = array()) @@ -104115,8 +104113,6 @@ function wp_remote_request($url, $args = array()) * @param string $url URL to retrieve. * @param array $args Optional. Request arguments. Default empty array. * @return array|WP_Error The response or WP_Error on failure. - * @phpstan-param string $url - * @phpstan-param array $args * @phpstan-return array{headers:Requests_Utility_CaseInsensitiveDictionary,body:string,response:array{code:int,message:string},cookies:array,filename:string|null,http_response:WP_HTTP_Requests_Response}|WP_Error */ function wp_remote_get($url, $args = array()) @@ -104133,8 +104129,6 @@ function wp_remote_get($url, $args = array()) * @param string $url URL to retrieve. * @param array $args Optional. Request arguments. Default empty array. * @return array|WP_Error The response or WP_Error on failure. - * @phpstan-param string $url - * @phpstan-param array $args * @phpstan-return array{headers:Requests_Utility_CaseInsensitiveDictionary,body:string,response:array{code:int,message:string},cookies:array,filename:string|null,http_response:WP_HTTP_Requests_Response}|WP_Error */ function wp_remote_post($url, $args = array()) @@ -104151,8 +104145,6 @@ function wp_remote_post($url, $args = array()) * @param string $url URL to retrieve. * @param array $args Optional. Request arguments. Default empty array. * @return array|WP_Error The response or WP_Error on failure. - * @phpstan-param string $url - * @phpstan-param array $args * @phpstan-return array{headers:Requests_Utility_CaseInsensitiveDictionary,body:string,response:array{code:int,message:string},cookies:array,filename:string|null,http_response:WP_HTTP_Requests_Response}|WP_Error */ function wp_remote_head($url, $args = array()) From 72ea26000e4b4d553776bae75d4bab3e8113b8b6 Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Wed, 23 Mar 2022 22:55:45 +0000 Subject: [PATCH 05/16] Add the safe functions too. --- additionalParams.php | 4 ++++ wordpress-stubs.php | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/additionalParams.php b/additionalParams.php index 20feb88..35044f0 100644 --- a/additionalParams.php +++ b/additionalParams.php @@ -7,4 +7,8 @@ 'wp_remote_head' => [$httpReturnType], 'wp_remote_post' => [$httpReturnType], 'wp_remote_request' => [$httpReturnType], + 'wp_safe_remote_get' => [$httpReturnType], + 'wp_safe_remote_head' => [$httpReturnType], + 'wp_safe_remote_post' => [$httpReturnType], + 'wp_safe_remote_request' => [$httpReturnType], ]; diff --git a/wordpress-stubs.php b/wordpress-stubs.php index 5107c7c..d717f9d 100644 --- a/wordpress-stubs.php +++ b/wordpress-stubs.php @@ -104003,6 +104003,7 @@ function _wp_http_get_object() * @param string $url URL to retrieve. * @param array $args Optional. Request arguments. Default empty array. * @return array|WP_Error The response or WP_Error on failure. + * @phpstan-return array{headers:Requests_Utility_CaseInsensitiveDictionary,body:string,response:array{code:int,message:string},cookies:array,filename:string|null,http_response:WP_HTTP_Requests_Response}|WP_Error */ function wp_safe_remote_request($url, $args = array()) { @@ -104021,6 +104022,7 @@ function wp_safe_remote_request($url, $args = array()) * @param string $url URL to retrieve. * @param array $args Optional. Request arguments. Default empty array. * @return array|WP_Error The response or WP_Error on failure. + * @phpstan-return array{headers:Requests_Utility_CaseInsensitiveDictionary,body:string,response:array{code:int,message:string},cookies:array,filename:string|null,http_response:WP_HTTP_Requests_Response}|WP_Error */ function wp_safe_remote_get($url, $args = array()) { @@ -104039,6 +104041,7 @@ function wp_safe_remote_get($url, $args = array()) * @param string $url URL to retrieve. * @param array $args Optional. Request arguments. Default empty array. * @return array|WP_Error The response or WP_Error on failure. + * @phpstan-return array{headers:Requests_Utility_CaseInsensitiveDictionary,body:string,response:array{code:int,message:string},cookies:array,filename:string|null,http_response:WP_HTTP_Requests_Response}|WP_Error */ function wp_safe_remote_post($url, $args = array()) { @@ -104057,6 +104060,7 @@ function wp_safe_remote_post($url, $args = array()) * @param string $url URL to retrieve. * @param array $args Optional. Request arguments. Default empty array. * @return array|WP_Error The response or WP_Error on failure. + * @phpstan-return array{headers:Requests_Utility_CaseInsensitiveDictionary,body:string,response:array{code:int,message:string},cookies:array,filename:string|null,http_response:WP_HTTP_Requests_Response}|WP_Error */ function wp_safe_remote_head($url, $args = array()) { From 5c6b2343fdd0debfa09a9cfa1aaf92fc0463118c Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Wed, 23 Mar 2022 22:56:35 +0000 Subject: [PATCH 06/16] Add full support for class methods. --- visitor.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/visitor.php b/visitor.php index 1ec6f8b..e5c5c3f 100644 --- a/visitor.php +++ b/visitor.php @@ -49,6 +49,18 @@ public function enterNode(Node $node) } $this->currentSymbolName = $node->name->name; + + if ($node instanceof ClassMethod) { + /** @var \PhpParser\Node\Stmt\Class_ $parent */ + $parent = $this->stack[count($this->stack) - 2]; + + $this->currentSymbolName = sprintf( + '%1$s::%2$s', + $parent->name->name, + $node->name->name + ); + } + $newDocComment = $this->addArrayHashNotation($docComment); if ($newDocComment !== null) { From ed1b85e81bdd895d4adaa5f2b886a7ee0772be10 Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Wed, 23 Mar 2022 22:56:47 +0000 Subject: [PATCH 07/16] Add the WP_Http methods. --- additionalParams.php | 4 ++++ wordpress-stubs.php | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/additionalParams.php b/additionalParams.php index 35044f0..e8f573f 100644 --- a/additionalParams.php +++ b/additionalParams.php @@ -11,4 +11,8 @@ 'wp_safe_remote_head' => [$httpReturnType], 'wp_safe_remote_post' => [$httpReturnType], 'wp_safe_remote_request' => [$httpReturnType], + 'WP_Http::get' => [$httpReturnType], + 'WP_Http::head' => [$httpReturnType], + 'WP_Http::post' => [$httpReturnType], + 'WP_Http::request' => [$httpReturnType], ]; diff --git a/wordpress-stubs.php b/wordpress-stubs.php index d717f9d..eeb93e2 100644 --- a/wordpress-stubs.php +++ b/wordpress-stubs.php @@ -40697,6 +40697,7 @@ class WP_Http * filename?: string, * limit_response_size?: int, * } $args + * @phpstan-return array{headers:Requests_Utility_CaseInsensitiveDictionary,body:string,response:array{code:int,message:string},cookies:array,filename:string|null,http_response:WP_HTTP_Requests_Response}|WP_Error */ public function request($url, $args = array()) { @@ -40785,6 +40786,7 @@ private function _dispatch_request($url, $args) * @param string|array $args Optional. Override the defaults. * @return array|WP_Error Array containing 'headers', 'body', 'response', 'cookies', 'filename'. * A WP_Error instance upon error. + * @phpstan-return array{headers:Requests_Utility_CaseInsensitiveDictionary,body:string,response:array{code:int,message:string},cookies:array,filename:string|null,http_response:WP_HTTP_Requests_Response}|WP_Error */ public function post($url, $args = array()) { @@ -40800,6 +40802,7 @@ public function post($url, $args = array()) * @param string|array $args Optional. Override the defaults. * @return array|WP_Error Array containing 'headers', 'body', 'response', 'cookies', 'filename'. * A WP_Error instance upon error. + * @phpstan-return array{headers:Requests_Utility_CaseInsensitiveDictionary,body:string,response:array{code:int,message:string},cookies:array,filename:string|null,http_response:WP_HTTP_Requests_Response}|WP_Error */ public function get($url, $args = array()) { @@ -40815,6 +40818,7 @@ public function get($url, $args = array()) * @param string|array $args Optional. Override the defaults. * @return array|WP_Error Array containing 'headers', 'body', 'response', 'cookies', 'filename'. * A WP_Error instance upon error. + * @phpstan-return array{headers:Requests_Utility_CaseInsensitiveDictionary,body:string,response:array{code:int,message:string},cookies:array,filename:string|null,http_response:WP_HTTP_Requests_Response}|WP_Error */ public function head($url, $args = array()) { From 26a44aff1d91609713ecf676527ec2777b9afce5 Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Wed, 23 Mar 2022 23:15:06 +0000 Subject: [PATCH 08/16] Add some list table methods. --- additionalParams.php | 2 ++ wordpress-stubs.php | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/additionalParams.php b/additionalParams.php index e8f573f..53c71ce 100644 --- a/additionalParams.php +++ b/additionalParams.php @@ -15,4 +15,6 @@ 'WP_Http::head' => [$httpReturnType], 'WP_Http::post' => [$httpReturnType], 'WP_Http::request' => [$httpReturnType], + 'WP_List_Table::display_tablenav' => ['void', 'which'=>'"top"|"bottom"'], + 'WP_List_Table::pagination' => ['void', 'which'=>'"top"|"bottom"'], ]; diff --git a/wordpress-stubs.php b/wordpress-stubs.php index eeb93e2..8079724 100644 --- a/wordpress-stubs.php +++ b/wordpress-stubs.php @@ -4378,6 +4378,8 @@ protected function get_items_per_page($option, $default = 20) * @since 3.1.0 * * @param string $which + * @phpstan-param "top"|"bottom" $which + * @phpstan-return void */ protected function pagination($which) { @@ -4494,6 +4496,8 @@ protected function get_table_classes() * * @since 3.1.0 * @param string $which + * @phpstan-param "top"|"bottom" $which + * @phpstan-return void */ protected function display_tablenav($which) { From 10e4b1f2e557353b4884e57d5f967fa138336d4f Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Wed, 23 Mar 2022 23:17:08 +0000 Subject: [PATCH 09/16] Docs. --- additionalParams.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/additionalParams.php b/additionalParams.php index 53c71ce..8c266b3 100644 --- a/additionalParams.php +++ b/additionalParams.php @@ -2,6 +2,13 @@ $httpReturnType = 'array{headers:Requests_Utility_CaseInsensitiveDictionary,body:string,response:array{code:int,message:string},cookies:array,filename:string|null,http_response:WP_HTTP_Requests_Response}|WP_Error'; +/** + * This array is in the same format as the stubs array in PHPStan: + * + * '' => [', ''=>''] + * + * @link https://github.com/phpstan/phpstan-src/blob/1.5.x/resources/functionMap.php + */ return [ 'wp_remote_get' => [$httpReturnType], 'wp_remote_head' => [$httpReturnType], From 77ac9f6a44d0c64af040ad38bd433ed4c46b1f6e Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Wed, 23 Mar 2022 23:17:30 +0000 Subject: [PATCH 10/16] Sorting. --- additionalParams.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/additionalParams.php b/additionalParams.php index 8c266b3..c2b6953 100644 --- a/additionalParams.php +++ b/additionalParams.php @@ -10,6 +10,12 @@ * @link https://github.com/phpstan/phpstan-src/blob/1.5.x/resources/functionMap.php */ return [ + 'WP_Http::get' => [$httpReturnType], + 'WP_Http::head' => [$httpReturnType], + 'WP_Http::post' => [$httpReturnType], + 'WP_Http::request' => [$httpReturnType], + 'WP_List_Table::display_tablenav' => ['void', 'which'=>'"top"|"bottom"'], + 'WP_List_Table::pagination' => ['void', 'which'=>'"top"|"bottom"'], 'wp_remote_get' => [$httpReturnType], 'wp_remote_head' => [$httpReturnType], 'wp_remote_post' => [$httpReturnType], @@ -18,10 +24,4 @@ 'wp_safe_remote_head' => [$httpReturnType], 'wp_safe_remote_post' => [$httpReturnType], 'wp_safe_remote_request' => [$httpReturnType], - 'WP_Http::get' => [$httpReturnType], - 'WP_Http::head' => [$httpReturnType], - 'WP_Http::post' => [$httpReturnType], - 'WP_Http::request' => [$httpReturnType], - 'WP_List_Table::display_tablenav' => ['void', 'which'=>'"top"|"bottom"'], - 'WP_List_Table::pagination' => ['void', 'which'=>'"top"|"bottom"'], ]; From 113e7fcaa1be867195d2cdac9333552fff00c1d9 Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Wed, 23 Mar 2022 23:26:02 +0000 Subject: [PATCH 11/16] Another list table method. --- additionalParams.php | 1 + wordpress-stubs.php | 2 ++ 2 files changed, 3 insertions(+) diff --git a/additionalParams.php b/additionalParams.php index c2b6953..e2ab1ef 100644 --- a/additionalParams.php +++ b/additionalParams.php @@ -14,6 +14,7 @@ 'WP_Http::head' => [$httpReturnType], 'WP_Http::post' => [$httpReturnType], 'WP_Http::request' => [$httpReturnType], + 'WP_List_Table::bulk_actions' => ['void', 'which'=>'"top"|"bottom"'], 'WP_List_Table::display_tablenav' => ['void', 'which'=>'"top"|"bottom"'], 'WP_List_Table::pagination' => ['void', 'which'=>'"top"|"bottom"'], 'wp_remote_get' => [$httpReturnType], diff --git a/wordpress-stubs.php b/wordpress-stubs.php index 8079724..f024aac 100644 --- a/wordpress-stubs.php +++ b/wordpress-stubs.php @@ -4290,6 +4290,8 @@ protected function get_bulk_actions() * * @param string $which The location of the bulk actions: 'top' or 'bottom'. * This is designated as optional for backward compatibility. + * @phpstan-param "top"|"bottom" $which + * @phpstan-return void */ protected function bulk_actions($which = '') { From 4136098031b4d25749582219663e69d889c91a65 Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Wed, 23 Mar 2022 23:28:46 +0000 Subject: [PATCH 12/16] Parameters for meta box functions. --- additionalParams.php | 2 ++ wordpress-stubs.php | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/additionalParams.php b/additionalParams.php index e2ab1ef..37cf144 100644 --- a/additionalParams.php +++ b/additionalParams.php @@ -10,6 +10,8 @@ * @link https://github.com/phpstan/phpstan-src/blob/1.5.x/resources/functionMap.php */ return [ + 'add_meta_box' => ['void', 'context'=>'"normal"|"side"|"advanced"', 'priority'=>'"high"|"core"|"default"|"low"'], + 'remove_meta_box' => ['void', 'context'=>'"normal"|"side"|"advanced"'], 'WP_Http::get' => [$httpReturnType], 'WP_Http::head' => [$httpReturnType], 'WP_Http::post' => [$httpReturnType], diff --git a/wordpress-stubs.php b/wordpress-stubs.php index f024aac..0bc41a0 100644 --- a/wordpress-stubs.php +++ b/wordpress-stubs.php @@ -81901,6 +81901,9 @@ function wp_import_upload_form($action) * @param array $callback_args Optional. Data that should be set as the $args property * of the box array (which is the second parameter passed * to your callback). Default null. + * @phpstan-param "normal"|"side"|"advanced" $context + * @phpstan-param "high"|"core"|"default"|"low" $priority + * @phpstan-return void */ function add_meta_box($id, $title, $callback, $screen = \null, $context = 'advanced', $priority = 'default', $callback_args = \null) { @@ -81980,6 +81983,8 @@ function do_meta_boxes($screen, $context, $object) * include 'normal', 'side', and 'advanced'. Comments screen contexts * include 'normal' and 'side'. Menus meta boxes (accordion sections) * all use the 'side' context. + * @phpstan-param "normal"|"side"|"advanced" $context + * @phpstan-return void */ function remove_meta_box($id, $screen, $context) { From 282c4f6e3fbc687388865e252b4d5a4e19ecd35e Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Thu, 24 Mar 2022 12:10:07 +0000 Subject: [PATCH 13/16] Terminology improvements. --- additionalParams.php => functionMap.php | 2 +- visitor.php | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) rename additionalParams.php => functionMap.php (94%) diff --git a/additionalParams.php b/functionMap.php similarity index 94% rename from additionalParams.php rename to functionMap.php index 37cf144..cb31dbf 100644 --- a/additionalParams.php +++ b/functionMap.php @@ -3,7 +3,7 @@ $httpReturnType = 'array{headers:Requests_Utility_CaseInsensitiveDictionary,body:string,response:array{code:int,message:string},cookies:array,filename:string|null,http_response:WP_HTTP_Requests_Response}|WP_Error'; /** - * This array is in the same format as the stubs array in PHPStan: + * This array is in the same format as the function map array in PHPStan: * * '' => [', ''=>''] * diff --git a/visitor.php b/visitor.php index e5c5c3f..3e08970 100644 --- a/visitor.php +++ b/visitor.php @@ -22,7 +22,7 @@ /** * @var array> */ - private $additionalParams; + private $functionMap; /** * @var string @@ -137,23 +137,23 @@ private function addArrayHashNotation(Doc $docComment): ?Doc private function addAdditionalParams(Doc $docComment): ?Doc { - if (! isset($this->additionalParams)) { - $this->additionalParams = require __DIR__ . '/additionalParams.php'; + if (! isset($this->functionMap)) { + $this->functionMap = require __DIR__ . '/functionMap.php'; } - if (! isset($this->additionalParams[$this->currentSymbolName])) { + if (! isset($this->functionMap[$this->currentSymbolName])) { return null; } - $params = $this->additionalParams[$this->currentSymbolName]; - $returnType = array_shift($params); + $parameters = $this->functionMap[$this->currentSymbolName]; + $returnType = array_shift($parameters); $additions = []; - foreach ($params as $param => $paramType) { + foreach ($parameters as $paramName => $paramType) { $additions[] = sprintf( '@phpstan-param %s $%s', $paramType, - $param + $paramName ); } From e528c3a85002dd11a2e6f1382da33f55291f304b Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Thu, 24 Mar 2022 12:24:59 +0000 Subject: [PATCH 14/16] Use dev-master for the stubs generator for now. --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index e71b605..a690434 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,7 @@ "require-dev": { "php": "~7.3 || ~8.0", "nikic/php-parser": "< 4.12.0", - "php-stubs/generator": "^0.8.0", + "php-stubs/generator": "dev-master", "phpdocumentor/reflection-docblock": "^5.3", "phpstan/phpstan": "^1.2" }, From c149f815a2afd1ea2f2142ed1f38ed5ded6ec226 Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Thu, 24 Mar 2022 12:25:15 +0000 Subject: [PATCH 15/16] Use FQNs and add some breathing space. --- functionMap.php | 2 +- wordpress-stubs.php | 24 ++++++++++++------------ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/functionMap.php b/functionMap.php index cb31dbf..b5df5d6 100644 --- a/functionMap.php +++ b/functionMap.php @@ -1,6 +1,6 @@ ,filename:string|null,http_response:WP_HTTP_Requests_Response}|WP_Error'; +$httpReturnType = 'array{headers: \Requests_Utility_CaseInsensitiveDictionary, body: string, response: array{code: int,message: string}, cookies: array, filename: string|null, http_response: \WP_HTTP_Requests_Response}|\WP_Error'; /** * This array is in the same format as the function map array in PHPStan: diff --git a/wordpress-stubs.php b/wordpress-stubs.php index 0bc41a0..37efea0 100644 --- a/wordpress-stubs.php +++ b/wordpress-stubs.php @@ -40703,7 +40703,7 @@ class WP_Http * filename?: string, * limit_response_size?: int, * } $args - * @phpstan-return array{headers:Requests_Utility_CaseInsensitiveDictionary,body:string,response:array{code:int,message:string},cookies:array,filename:string|null,http_response:WP_HTTP_Requests_Response}|WP_Error + * @phpstan-return array{headers: \Requests_Utility_CaseInsensitiveDictionary, body: string, response: array{code: int,message: string}, cookies: array, filename: string|null, http_response: \WP_HTTP_Requests_Response}|\WP_Error */ public function request($url, $args = array()) { @@ -40792,7 +40792,7 @@ private function _dispatch_request($url, $args) * @param string|array $args Optional. Override the defaults. * @return array|WP_Error Array containing 'headers', 'body', 'response', 'cookies', 'filename'. * A WP_Error instance upon error. - * @phpstan-return array{headers:Requests_Utility_CaseInsensitiveDictionary,body:string,response:array{code:int,message:string},cookies:array,filename:string|null,http_response:WP_HTTP_Requests_Response}|WP_Error + * @phpstan-return array{headers: \Requests_Utility_CaseInsensitiveDictionary, body: string, response: array{code: int,message: string}, cookies: array, filename: string|null, http_response: \WP_HTTP_Requests_Response}|\WP_Error */ public function post($url, $args = array()) { @@ -40808,7 +40808,7 @@ public function post($url, $args = array()) * @param string|array $args Optional. Override the defaults. * @return array|WP_Error Array containing 'headers', 'body', 'response', 'cookies', 'filename'. * A WP_Error instance upon error. - * @phpstan-return array{headers:Requests_Utility_CaseInsensitiveDictionary,body:string,response:array{code:int,message:string},cookies:array,filename:string|null,http_response:WP_HTTP_Requests_Response}|WP_Error + * @phpstan-return array{headers: \Requests_Utility_CaseInsensitiveDictionary, body: string, response: array{code: int,message: string}, cookies: array, filename: string|null, http_response: \WP_HTTP_Requests_Response}|\WP_Error */ public function get($url, $args = array()) { @@ -40824,7 +40824,7 @@ public function get($url, $args = array()) * @param string|array $args Optional. Override the defaults. * @return array|WP_Error Array containing 'headers', 'body', 'response', 'cookies', 'filename'. * A WP_Error instance upon error. - * @phpstan-return array{headers:Requests_Utility_CaseInsensitiveDictionary,body:string,response:array{code:int,message:string},cookies:array,filename:string|null,http_response:WP_HTTP_Requests_Response}|WP_Error + * @phpstan-return array{headers: \Requests_Utility_CaseInsensitiveDictionary, body: string, response: array{code: int,message: string}, cookies: array, filename: string|null, http_response: \WP_HTTP_Requests_Response}|\WP_Error */ public function head($url, $args = array()) { @@ -104018,7 +104018,7 @@ function _wp_http_get_object() * @param string $url URL to retrieve. * @param array $args Optional. Request arguments. Default empty array. * @return array|WP_Error The response or WP_Error on failure. - * @phpstan-return array{headers:Requests_Utility_CaseInsensitiveDictionary,body:string,response:array{code:int,message:string},cookies:array,filename:string|null,http_response:WP_HTTP_Requests_Response}|WP_Error + * @phpstan-return array{headers: \Requests_Utility_CaseInsensitiveDictionary, body: string, response: array{code: int,message: string}, cookies: array, filename: string|null, http_response: \WP_HTTP_Requests_Response}|\WP_Error */ function wp_safe_remote_request($url, $args = array()) { @@ -104037,7 +104037,7 @@ function wp_safe_remote_request($url, $args = array()) * @param string $url URL to retrieve. * @param array $args Optional. Request arguments. Default empty array. * @return array|WP_Error The response or WP_Error on failure. - * @phpstan-return array{headers:Requests_Utility_CaseInsensitiveDictionary,body:string,response:array{code:int,message:string},cookies:array,filename:string|null,http_response:WP_HTTP_Requests_Response}|WP_Error + * @phpstan-return array{headers: \Requests_Utility_CaseInsensitiveDictionary, body: string, response: array{code: int,message: string}, cookies: array, filename: string|null, http_response: \WP_HTTP_Requests_Response}|\WP_Error */ function wp_safe_remote_get($url, $args = array()) { @@ -104056,7 +104056,7 @@ function wp_safe_remote_get($url, $args = array()) * @param string $url URL to retrieve. * @param array $args Optional. Request arguments. Default empty array. * @return array|WP_Error The response or WP_Error on failure. - * @phpstan-return array{headers:Requests_Utility_CaseInsensitiveDictionary,body:string,response:array{code:int,message:string},cookies:array,filename:string|null,http_response:WP_HTTP_Requests_Response}|WP_Error + * @phpstan-return array{headers: \Requests_Utility_CaseInsensitiveDictionary, body: string, response: array{code: int,message: string}, cookies: array, filename: string|null, http_response: \WP_HTTP_Requests_Response}|\WP_Error */ function wp_safe_remote_post($url, $args = array()) { @@ -104075,7 +104075,7 @@ function wp_safe_remote_post($url, $args = array()) * @param string $url URL to retrieve. * @param array $args Optional. Request arguments. Default empty array. * @return array|WP_Error The response or WP_Error on failure. - * @phpstan-return array{headers:Requests_Utility_CaseInsensitiveDictionary,body:string,response:array{code:int,message:string},cookies:array,filename:string|null,http_response:WP_HTTP_Requests_Response}|WP_Error + * @phpstan-return array{headers: \Requests_Utility_CaseInsensitiveDictionary, body: string, response: array{code: int,message: string}, cookies: array, filename: string|null, http_response: \WP_HTTP_Requests_Response}|\WP_Error */ function wp_safe_remote_head($url, $args = array()) { @@ -104116,7 +104116,7 @@ function wp_safe_remote_head($url, $args = array()) * cookies: WP_HTTP_Cookie[], * http_response: WP_HTTP_Requests_Response|null, * } - * @phpstan-return array{headers:Requests_Utility_CaseInsensitiveDictionary,body:string,response:array{code:int,message:string},cookies:array,filename:string|null,http_response:WP_HTTP_Requests_Response}|WP_Error + * @phpstan-return array{headers: \Requests_Utility_CaseInsensitiveDictionary, body: string, response: array{code: int,message: string}, cookies: array, filename: string|null, http_response: \WP_HTTP_Requests_Response}|\WP_Error */ function wp_remote_request($url, $args = array()) { @@ -104132,7 +104132,7 @@ function wp_remote_request($url, $args = array()) * @param string $url URL to retrieve. * @param array $args Optional. Request arguments. Default empty array. * @return array|WP_Error The response or WP_Error on failure. - * @phpstan-return array{headers:Requests_Utility_CaseInsensitiveDictionary,body:string,response:array{code:int,message:string},cookies:array,filename:string|null,http_response:WP_HTTP_Requests_Response}|WP_Error + * @phpstan-return array{headers: \Requests_Utility_CaseInsensitiveDictionary, body: string, response: array{code: int,message: string}, cookies: array, filename: string|null, http_response: \WP_HTTP_Requests_Response}|\WP_Error */ function wp_remote_get($url, $args = array()) { @@ -104148,7 +104148,7 @@ function wp_remote_get($url, $args = array()) * @param string $url URL to retrieve. * @param array $args Optional. Request arguments. Default empty array. * @return array|WP_Error The response or WP_Error on failure. - * @phpstan-return array{headers:Requests_Utility_CaseInsensitiveDictionary,body:string,response:array{code:int,message:string},cookies:array,filename:string|null,http_response:WP_HTTP_Requests_Response}|WP_Error + * @phpstan-return array{headers: \Requests_Utility_CaseInsensitiveDictionary, body: string, response: array{code: int,message: string}, cookies: array, filename: string|null, http_response: \WP_HTTP_Requests_Response}|\WP_Error */ function wp_remote_post($url, $args = array()) { @@ -104164,7 +104164,7 @@ function wp_remote_post($url, $args = array()) * @param string $url URL to retrieve. * @param array $args Optional. Request arguments. Default empty array. * @return array|WP_Error The response or WP_Error on failure. - * @phpstan-return array{headers:Requests_Utility_CaseInsensitiveDictionary,body:string,response:array{code:int,message:string},cookies:array,filename:string|null,http_response:WP_HTTP_Requests_Response}|WP_Error + * @phpstan-return array{headers: \Requests_Utility_CaseInsensitiveDictionary, body: string, response: array{code: int,message: string}, cookies: array, filename: string|null, http_response: \WP_HTTP_Requests_Response}|\WP_Error */ function wp_remote_head($url, $args = array()) { From 24ced7d229d99a98dfcce071424bb6fec71e9caf Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Thu, 24 Mar 2022 12:33:37 +0000 Subject: [PATCH 16/16] Coding standards. --- visitor.php | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/visitor.php b/visitor.php index 3e08970..97ef96a 100644 --- a/visitor.php +++ b/visitor.php @@ -20,9 +20,9 @@ private $docBlockFactory; /** - * @var array> + * @var ?array> */ - private $functionMap; + private $functionMap = null; /** * @var string @@ -54,11 +54,13 @@ public function enterNode(Node $node) /** @var \PhpParser\Node\Stmt\Class_ $parent */ $parent = $this->stack[count($this->stack) - 2]; - $this->currentSymbolName = sprintf( - '%1$s::%2$s', - $parent->name->name, - $node->name->name - ); + if (isset($parent->name)) { + $this->currentSymbolName = sprintf( + '%1$s::%2$s', + $parent->name->name, + $node->name->name + ); + } } $newDocComment = $this->addArrayHashNotation($docComment);