From ccf6f7c053f4746b1f72c1165abc537d11b38ede Mon Sep 17 00:00:00 2001 From: IanDelMar <42134098+IanDelMar@users.noreply.github.com> Date: Thu, 26 Oct 2023 00:32:01 +0200 Subject: [PATCH 1/6] Fix typo in parameter name (#117) --- functionMap.php | 4 ++-- wordpress-stubs.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/functionMap.php b/functionMap.php index 722c852b..ba34cd1b 100644 --- a/functionMap.php +++ b/functionMap.php @@ -7,7 +7,7 @@ } $cronArgsType = 'list'; $wpWidgetRssFormArgsType = 'array{number: int, error: bool, title?: string, url?: string, items?: int, show_summary?: int, show_author?: int, show_date?: int}'; -$wpWidgetRssFormInputType = 'array{title?: bool, url?: bool, items?: bool, show_summary?: bool, show_author?: bool, show_date?: bool}'; +$wpWidgetRssFormInputsType = 'array{title?: bool, url?: bool, items?: bool, show_summary?: bool, show_author?: bool, show_date?: bool}'; $filesystemDirlistReturnType = "false|array"; /** @@ -57,7 +57,7 @@ 'wp_slash' => ['T', '@phpstan-template' => 'T', 'value' => 'T'], 'wp_unschedule_event' => ['($wp_error is false ? bool : true|\WP_Error)', 'args'=>$cronArgsType], 'wp_unslash' => ['T', '@phpstan-template' => 'T', 'value' => 'T'], - 'wp_widget_rss_form' => ['void', 'args'=>$wpWidgetRssFormArgsType, 'input'=>$wpWidgetRssFormInputType], + 'wp_widget_rss_form' => ['void', 'args' => $wpWidgetRssFormArgsType, 'inputs' => $wpWidgetRssFormInputType], 'WP_REST_Request' => [null, '@phpstan-template' => 'T of array', '@phpstan-implements' => 'ArrayAccess, value-of>'], 'WP_REST_Request::offsetExists' => [null, 'offset' => '@param key-of'], 'WP_REST_Request::offsetGet' => ['T[TOffset]', '@phpstan-template' => 'TOffset of key-of', 'offset' => 'TOffset'], diff --git a/wordpress-stubs.php b/wordpress-stubs.php index d231c4f6..8d3eebac 100644 --- a/wordpress-stubs.php +++ b/wordpress-stubs.php @@ -138464,7 +138464,7 @@ function wp_widget_rss_output($rss, $args = array()) * @param array|string $args Values for input fields. * @param array $inputs Override default display options. * @phpstan-param array{number: int, error: bool, title?: string, url?: string, items?: int, show_summary?: int, show_author?: int, show_date?: int} $args - * @phpstan-param array{title?: bool, url?: bool, items?: bool, show_summary?: bool, show_author?: bool, show_date?: bool} $input + * @phpstan-param array{title?: bool, url?: bool, items?: bool, show_summary?: bool, show_author?: bool, show_date?: bool} $inputs * @phpstan-return void */ function wp_widget_rss_form($args, $inputs = \null) From fe8d328ed119d1ba48bf5d230fb170e121b44690 Mon Sep 17 00:00:00 2001 From: IanDelMar <42134098+IanDelMar@users.noreply.github.com> Date: Thu, 26 Oct 2023 00:35:23 +0200 Subject: [PATCH 2/6] Update parameter names (#115) --- functionMap.php | 8 ++++---- wordpress-stubs.php | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/functionMap.php b/functionMap.php index ba34cd1b..85af6da1 100644 --- a/functionMap.php +++ b/functionMap.php @@ -68,10 +68,10 @@ 'WP_Theme::offsetExists' => ['($offset is ThemeKey ? true : false)'], 'WP_Theme::offsetGet' => ['($offset is ThemeKey ? mixed : null)'], 'WP_Block_List' => [null, '@phpstan-implements' => 'ArrayAccess'], - 'WP_Block_List::offsetExists' => [null, 'index' => 'int'], - 'WP_Block_List::offsetGet' => ['WP_Block|null', 'index' => 'int'], - 'WP_Block_List::offsetSet' => ['void', 'index' => 'int|null'], - 'WP_Block_List::offsetUnset' => ['void', 'index' => 'int'], + 'WP_Block_List::offsetExists' => [null, 'offset' => 'int'], + 'WP_Block_List::offsetGet' => ['WP_Block|null', 'offset'=>'int'], + 'WP_Block_List::offsetSet' => ['void', 'offset' => 'int|null'], + 'WP_Block_List::offsetUnset' => ['void', 'offset' => 'int'], 'is_wp_error' => ['($thing is \WP_Error ? true : false)', '@phpstan-assert-if-true' => '\WP_Error $thing'], 'current_time' => ["(\$type is 'timestamp'|'U' ? int : string)"], 'mysql2date' => ["(\$format is 'G'|'U' ? int|false : string|false)"], diff --git a/wordpress-stubs.php b/wordpress-stubs.php index 8d3eebac..ea7d2430 100644 --- a/wordpress-stubs.php +++ b/wordpress-stubs.php @@ -31010,7 +31010,7 @@ public function __construct($blocks, $available_context = array(), $registry = \ * * @param string $offset Offset of block to check for. * @return bool Whether block exists. - * @phpstan-param int $index + * @phpstan-param int $offset */ #[\ReturnTypeWillChange] public function offsetExists($offset) @@ -31025,7 +31025,7 @@ public function offsetExists($offset) * * @param string $offset Offset of block value to retrieve. * @return mixed|null Block value if exists, or null. - * @phpstan-param int $index + * @phpstan-param int $offset * @phpstan-return WP_Block|null */ #[\ReturnTypeWillChange] @@ -31041,7 +31041,7 @@ public function offsetGet($offset) * * @param string $offset Offset of block value to set. * @param mixed $value Block value. - * @phpstan-param int|null $index + * @phpstan-param int|null $offset * @phpstan-return void */ #[\ReturnTypeWillChange] @@ -31056,7 +31056,7 @@ public function offsetSet($offset, $value) * @link https://www.php.net/manual/en/arrayaccess.offsetunset.php * * @param string $offset Offset of block value to unset. - * @phpstan-param int $index + * @phpstan-param int $offset * @phpstan-return void */ #[\ReturnTypeWillChange] From 1ca7882a7e16070080ec807c03ac30968d83d013 Mon Sep 17 00:00:00 2001 From: IanDelMar <42134098+IanDelMar@users.noreply.github.com> Date: Fri, 27 Oct 2023 01:50:56 +0200 Subject: [PATCH 3/6] Remove obsolete isset() (#120) --- visitor.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/visitor.php b/visitor.php index b07a08f1..baa3588a 100644 --- a/visitor.php +++ b/visitor.php @@ -969,7 +969,7 @@ private static function getTypesAtLevel(string $text, bool $optional, int $level if (is_numeric($nameTrimmed)) { $optionalArg = false; } elseif ($optional && ($level > 1)) { - $optionalArg = isset($parts[2]) && self::isOptional($parts[2]); + $optionalArg = self::isOptional($parts[2]); } if (strpos($name, '...$') !== false) { From 903d7e41eed8c08eaa758a5b28f284a814ae7b23 Mon Sep 17 00:00:00 2001 From: IanDelMar <42134098+IanDelMar@users.noreply.github.com> Date: Sat, 28 Oct 2023 20:50:41 +0200 Subject: [PATCH 4/6] Fix and improve handling of wp_die's 3rd parameter (#123) --- visitor.php | 29 ++++++++++++++--------------- wordpress-stubs.php | 1 + 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/visitor.php b/visitor.php index baa3588a..100260b4 100644 --- a/visitor.php +++ b/visitor.php @@ -11,15 +11,14 @@ use phpDocumentor\Reflection\Types\Never_; use phpDocumentor\Reflection\Types\Void_; use PhpParser\Comment\Doc; +use PhpParser\ConstExprEvaluationException; +use PhpParser\ConstExprEvaluator; use PhpParser\Node; use PhpParser\NodeFinder; use PhpParser\Node\Identifier; use PhpParser\Node\Name; -use PhpParser\Node\Expr\Array_; -use PhpParser\Node\Expr\ArrayItem; use PhpParser\Node\Expr\Exit_; use PhpParser\Node\Expr\FuncCall; -use PhpParser\Node\Scalar\String_; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Expression; @@ -1071,23 +1070,23 @@ static function (Node $node): bool { return 'never'; } // If wp_die is called with 3rd parameter, we need additional checks. - $argValue = $args[2]->value; - if (!($argValue instanceof Array_)) { + try { + $arg = (new ConstExprEvaluator())->evaluateSilently($args[2]->value); + } catch (ConstExprEvaluationException $e) { + // If we don't know the value of the 3rd parameter, we can't be sure. continue; } - foreach ($argValue->items as $item) { - if (!($item instanceof ArrayItem && $item->key instanceof String_ && $item->key->value === 'exit')) { - continue; - } - if ( - ($item->value instanceof Node\Expr\ConstFetch && strtolower($item->value->name->toString()) === 'true') - || ($item->value instanceof Node\Scalar\LNumber && $item->value->value === 1) - || ($item->value instanceof Node\Scalar\String_ && $item->value->value !== '' && $item->value->value !== '0') - ) { + + if (is_int($arg)) { + return 'never'; + } + if (is_array($arg)) { + if (!isset($arg['exit']) || (bool)$arg['exit'] === true) { return 'never'; } - return ''; } + + continue; } return ''; } diff --git a/wordpress-stubs.php b/wordpress-stubs.php index ea7d2430..eb843e1a 100644 --- a/wordpress-stubs.php +++ b/wordpress-stubs.php @@ -120938,6 +120938,7 @@ function ms_load_current_site_and_network($domain, $path, $subdomain = \false) * * @param string $domain The requested domain for the error to reference. * @param string $path The requested path for the error to reference. + * @phpstan-return never */ function ms_not_installed($domain, $path) { From 0a8a4efd97e7db5d2ba3289ae737f222091ea944 Mon Sep 17 00:00:00 2001 From: IanDelMar <42134098+IanDelMar@users.noreply.github.com> Date: Mon, 30 Oct 2023 09:32:40 +0100 Subject: [PATCH 5/6] Apply coding standards (#119) --- composer.json | 7 +- functionMap.php | 22 +++--- phpcs.xml.dist | 10 +++ visitor.php | 177 +++++++++++++++++++----------------------------- 4 files changed, 98 insertions(+), 118 deletions(-) create mode 100644 phpcs.xml.dist diff --git a/composer.json b/composer.json index a4e5ca77..531ac8d0 100644 --- a/composer.json +++ b/composer.json @@ -10,11 +10,13 @@ "homepage": "https://github.com/php-stubs/wordpress-stubs", "require-dev": { "php": "^7.4 || ~8.0.0", + "dealerdirect/phpcodesniffer-composer-installer": "^1.0", "nikic/php-parser": "^4.13", "php-stubs/generator": "^0.8.3", "phpdocumentor/reflection-docblock": "^5.3", "phpstan/phpstan": "^1.10.12", - "phpunit/phpunit": "^9.5" + "phpunit/phpunit": "^9.5", + "szepeviktor/phpcs-psr-12-neutron-hybrid-ruleset": "^0.8" }, "suggest": { "paragonie/sodium_compat": "Pure PHP implementation of libsodium", @@ -41,9 +43,12 @@ "post-update-cmd": "@composer --working-dir=source/ update --no-interaction", "cleanup": "git status --short --ignored | sed -n -e 's#^!! ##p' | xargs -r rm -vrf", "test": [ + "@test:cs", "@test:phpstan", "@test:phpunit" ], + "test:cs": "phpcs", + "test:cs:fix": "phpcbf", "test:phpstan": "phpstan analyze", "test:phpunit": "phpunit" }, diff --git a/functionMap.php b/functionMap.php index 85af6da1..ed098594 100644 --- a/functionMap.php +++ b/functionMap.php @@ -31,9 +31,9 @@ 'stripslashes_deep' => ['T', '@phpstan-template' => 'T', 'value' => 'T'], 'urldecode_deep' => ['T', '@phpstan-template' => 'T', 'value' => 'T'], 'urlencode_deep' => ['T', '@phpstan-template' => 'T', 'value' => 'T'], - 'wp_clear_scheduled_hook' => ['(0|positive-int|($wp_error is false ? false : \WP_Error))', 'args'=>$cronArgsType], - 'wp_get_schedule' => [null, 'args'=>$cronArgsType], - 'wp_get_scheduled_event' => [null, 'args'=>$cronArgsType], + 'wp_clear_scheduled_hook' => ['(0|positive-int|($wp_error is false ? false : \WP_Error))', 'args' => $cronArgsType], + 'wp_get_schedule' => [null, 'args' => $cronArgsType], + 'wp_get_scheduled_event' => [null, 'args' => $cronArgsType], 'WP_Http::get' => [$httpReturnType], 'WP_Http::head' => [$httpReturnType], 'WP_Http::post' => [$httpReturnType], @@ -41,35 +41,35 @@ 'WP_List_Table::display_tablenav' => ['void', 'which' => '"top"|"bottom"'], 'WP_List_Table::pagination' => ['void', 'which' => '"top"|"bottom"'], 'WP_List_Table::set_pagination_args' => ['void', 'args' => 'array{total_items?: int, total_pages?: int, per_page?: int}'], - 'wp_next_scheduled' => [null, 'args'=>$cronArgsType], + 'wp_next_scheduled' => [null, 'args' => $cronArgsType], 'WP_Query::have_posts' => [null, '@phpstan-impure' => ''], 'wp_remote_get' => [$httpReturnType], 'wp_remote_head' => [$httpReturnType], 'wp_remote_post' => [$httpReturnType], 'wp_remote_request' => [$httpReturnType], - 'wp_reschedule_event' => ['($wp_error is false ? bool : true|\WP_Error)', 'args'=>$cronArgsType], + 'wp_reschedule_event' => ['($wp_error is false ? bool : true|\WP_Error)', 'args' => $cronArgsType], 'wp_safe_remote_get' => [$httpReturnType], 'wp_safe_remote_head' => [$httpReturnType], 'wp_safe_remote_post' => [$httpReturnType], 'wp_safe_remote_request' => [$httpReturnType], - 'wp_schedule_event' => ['($wp_error is false ? bool : true|\WP_Error)', 'args'=>$cronArgsType], - 'wp_schedule_single_event' => ['($wp_error is false ? bool : true|\WP_Error)', 'args'=>$cronArgsType], + 'wp_schedule_event' => ['($wp_error is false ? bool : true|\WP_Error)', 'args' => $cronArgsType], + 'wp_schedule_single_event' => ['($wp_error is false ? bool : true|\WP_Error)', 'args' => $cronArgsType], 'wp_slash' => ['T', '@phpstan-template' => 'T', 'value' => 'T'], - 'wp_unschedule_event' => ['($wp_error is false ? bool : true|\WP_Error)', 'args'=>$cronArgsType], + 'wp_unschedule_event' => ['($wp_error is false ? bool : true|\WP_Error)', 'args' => $cronArgsType], 'wp_unslash' => ['T', '@phpstan-template' => 'T', 'value' => 'T'], - 'wp_widget_rss_form' => ['void', 'args' => $wpWidgetRssFormArgsType, 'inputs' => $wpWidgetRssFormInputType], + 'wp_widget_rss_form' => ['void', 'args' => $wpWidgetRssFormArgsType, 'inputs' => $wpWidgetRssFormInputsType], 'WP_REST_Request' => [null, '@phpstan-template' => 'T of array', '@phpstan-implements' => 'ArrayAccess, value-of>'], 'WP_REST_Request::offsetExists' => [null, 'offset' => '@param key-of'], 'WP_REST_Request::offsetGet' => ['T[TOffset]', '@phpstan-template' => 'TOffset of key-of', 'offset' => 'TOffset'], 'WP_REST_Request::offsetSet' => ['void', '@phpstan-template' => 'TOffset of key-of', 'offset' => 'TOffset', 'value' => 'T[TOffset]'], 'WP_REST_Request::offsetUnset' => ['void', '@phpstan-template' => 'TOffset of key-of', 'offset' => 'TOffset'], - 'WP_Theme' => [null, '@phpstan-type'=>"ThemeKey 'Name'|'Version'|'Status'|'Title'|'Author'|'Author Name'|'Author URI'|'Description'|'Template'|'Stylesheet'|'Template Files'|'Stylesheet Files'|'Template Dir'|'Stylesheet Dir'|'Screenshot'|'Tags'|'Theme Root'|'Theme Root URI'|'Parent Theme'"], + 'WP_Theme' => [null, '@phpstan-type' => "ThemeKey 'Name'|'Version'|'Status'|'Title'|'Author'|'Author Name'|'Author URI'|'Description'|'Template'|'Stylesheet'|'Template Files'|'Stylesheet Files'|'Template Dir'|'Stylesheet Dir'|'Screenshot'|'Tags'|'Theme Root'|'Theme Root URI'|'Parent Theme'"], 'WP_Theme::get' => ["(\$header is 'Name'|'ThemeURI'|'Description'|'Author'|'AuthorURI'|'Version'|'Template'|'Status'|'Tags'|'TextDomain'|'DomainPath'|'RequiresWP'|'RequiresPHP'|'UpdateURI' ? (\$header is 'Tags' ? string[] : string) : false)"], 'WP_Theme::offsetExists' => ['($offset is ThemeKey ? true : false)'], 'WP_Theme::offsetGet' => ['($offset is ThemeKey ? mixed : null)'], 'WP_Block_List' => [null, '@phpstan-implements' => 'ArrayAccess'], 'WP_Block_List::offsetExists' => [null, 'offset' => 'int'], - 'WP_Block_List::offsetGet' => ['WP_Block|null', 'offset'=>'int'], + 'WP_Block_List::offsetGet' => ['WP_Block|null', 'offset' => 'int'], 'WP_Block_List::offsetSet' => ['void', 'offset' => 'int|null'], 'WP_Block_List::offsetUnset' => ['void', 'offset' => 'int'], 'is_wp_error' => ['($thing is \WP_Error ? true : false)', '@phpstan-assert-if-true' => '\WP_Error $thing'], diff --git a/phpcs.xml.dist b/phpcs.xml.dist new file mode 100644 index 00000000..4985dd4d --- /dev/null +++ b/phpcs.xml.dist @@ -0,0 +1,10 @@ + + + visitor.php + + + + + + + diff --git a/visitor.php b/visitor.php index 100260b4..3d7e3c7d 100644 --- a/visitor.php +++ b/visitor.php @@ -1,6 +1,6 @@ */ public $children = []; public function isArrayShape(): bool @@ -68,28 +65,20 @@ public function hasChildren(): bool final class WordPressTag extends WithChildren { - /** - * @var string - */ + /** @var string */ public $tag; - /** - * @var string - */ + /** @var string */ public $type; - /** - * @var ?string - */ + /** @var ?string */ public $name = null; - /** - * @var ?string - */ + /** @var ?string */ public $description = null; /** - * @return string[] + * @return list */ public function format(): array { @@ -100,7 +89,7 @@ public function format(): array $this->tag, $this->type, ($this->name !== null) ? (' $' . $this->name) : '' - ) + ), ]; } @@ -146,7 +135,6 @@ public function format(): array $this->type, $name ); - return $strings; } } @@ -158,19 +146,9 @@ public function format(): array $description = ' ' . $this->description; } - if ($this->isArrayShape()) { - $strings[] = sprintf( - '}%s%s', - $name, - $description - ); - } else { - $strings[] = sprintf( - '}>%s%s', - $name, - $description - ); - } + $strings[] = $this->isArrayShape() + ? sprintf('}%s%s', $name, $description) + : sprintf('}>%s%s', $name, $description); return $strings; } @@ -178,24 +156,16 @@ public function format(): array final class WordPressArg extends WithChildren { - /** - * @var string - */ + /** @var string */ public $type; - /** - * @var bool - */ + /** @var bool */ public $optional = false; - /** - * @var ?string - */ + /** @var ?string */ public $name = null; - /** - * @return string[] - */ + /** @return list */ public function format(int $level = 1): array { $strings = []; @@ -260,25 +230,16 @@ public function format(int $level = 1): array } return new class extends NodeVisitor { - - /** - * @var \phpDocumentor\Reflection\DocBlockFactory - */ + /** @var \phpDocumentor\Reflection\DocBlockFactory */ private $docBlockFactory; - /** - * @var ?array> - */ + /** @var ?array> */ private $functionMap = null; - /** - * @var array> - */ + /** @var array> */ private $additionalTags = []; - /** - * @var array> - */ + /** @var array> */ private $additionalTagStrings = []; /** @var \PhpParser\NodeFinder */ @@ -324,12 +285,12 @@ public function enterNode(Node $node) $additions = $this->generateAdditionalTagsFromDoc($docComment); if (count($additions) > 0) { - $this->additionalTags[ $symbolName ] = $additions; + $this->additionalTags[$symbolName] = $additions; } $additions = $this->getAdditionalTagsFromMap($symbolName); if (count($additions) > 0) { - $this->additionalTagStrings[ $symbolName ] = $additions; + $this->additionalTagStrings[$symbolName] = $additions; } if ($voidOrNever !== '') { @@ -367,7 +328,7 @@ private static function getNodeName(Node $node): string } /** - * @return Node[] + * @return list<\PhpParser\Node> */ public function getStubStmts(): array { @@ -376,7 +337,6 @@ public function getStubStmts(): array foreach ($stmts as $stmt) { $this->postProcessNode($stmt); } - return $stmts; } @@ -410,7 +370,7 @@ private function postProcessNode(Node $node): void $node->setDocComment($newDocComment); } - if (! isset($this->additionalTagStrings[ $name ])) { + if (! isset($this->additionalTagStrings[$name])) { return; } @@ -428,7 +388,7 @@ private function postProcessNode(Node $node): void } /** - * @return array + * @return array */ private function generateAdditionalTagsFromDoc(Doc $docComment): array { @@ -436,22 +396,22 @@ private function generateAdditionalTagsFromDoc(Doc $docComment): array try { $docblock = $this->docBlockFactory->create($docCommentText); - } catch ( \RuntimeException $e ) { + } catch (\RuntimeException $e) { return []; - } catch ( \InvalidArgumentException $e ) { + } catch (\InvalidArgumentException $e) { return []; } - /** @var \phpDocumentor\Reflection\DocBlock\Tags\Param[] */ + /** @var list<\phpDocumentor\Reflection\DocBlock\Tags\Param> $params*/ $params = $docblock->getTagsByName('param'); - /** @var \phpDocumentor\Reflection\DocBlock\Tags\Return_[] */ + /** @var list<\phpDocumentor\Reflection\DocBlock\Tags\Return_> $returns */ $returns = $docblock->getTagsByName('return'); - /** @var \phpDocumentor\Reflection\DocBlock\Tags\Var_[] */ + /** @var list<\phpDocumentor\Reflection\DocBlock\Tags\Var_> $vars */ $vars = $docblock->getTagsByName('var'); - /** @var WordPressTag[] $additions */ + /** @var list<\WordPressTag> $additions */ $additions = []; foreach ($params as $param) { @@ -495,34 +455,32 @@ private function generateAdditionalTagsFromDoc(Doc $docComment): array private function addTags(string $name, Doc $docComment): ?Doc { - if (isset($this->additionalTags[ $name ])) { - $additions = $this->additionalTags[ $name ]; - } else { - $additions = []; - } - + $additions = $this->additionalTags[$name] ?? []; $docCommentText = $docComment->getText(); try { $docblock = $this->docBlockFactory->create($docCommentText); - } catch ( \RuntimeException $e ) { + } catch (\RuntimeException $e) { return null; - } catch ( \InvalidArgumentException $e ) { + } catch (\InvalidArgumentException $e) { return null; } $additions = $this->discoverInheritedArgs($docblock, $additions); - /** @var string[] $additionStrings */ - $additionStrings = array_map( function(WordPressTag $tag): string { - $lines = $tag->format(); + /** @var list $additionStrings */ + $additionStrings = array_map( + static function (WordPressTag $tag): string { + $lines = $tag->format(); - if (count($lines) === 0) { - return ''; - } + if (count($lines) === 0) { + return ''; + } - return " * " . implode("\n * ", $lines); - }, $additions); + return ' * ' . implode("\n * ", $lines); + }, + $additions + ); $additionStrings = array_filter($additionStrings); @@ -540,17 +498,20 @@ private function addTags(string $name, Doc $docComment): ?Doc } /** - * @param array $additions - * @return array + * @param array $additions + * @return array */ private function discoverInheritedArgs(DocBlock $docblock, array $additions): array { - /** @var Param[] $params */ + /** @var list<\phpDocumentor\Reflection\DocBlock\Tags\Param> $params */ $params = $docblock->getTagsByName('param'); - $phpStanParams = array_filter($additions, function(WordPressTag $addition): bool { - return $addition->tag === '@phpstan-param'; - }); + $phpStanParams = array_filter( + $additions, + static function (WordPressTag $addition): bool { + return $addition->tag === '@phpstan-param'; + } + ); foreach ($params as $param) { $inherited = $this->getInheritedTagsForParam($param); @@ -577,7 +538,7 @@ private function discoverInheritedArgs(DocBlock $docblock, array $additions): ar } /** - * @return array + * @return array */ private function getInheritedTagsForParam(Param $param): array { @@ -630,7 +591,7 @@ private function getInheritedTagsForParam(Param $param): array } /** - * @param array $tags + * @param array $tags */ private static function getMatchingInheritedTag(Param $param, array $tags, string $symbolName): ?WordPressTag { @@ -641,9 +602,12 @@ private static function getMatchingInheritedTag(Param $param, array $tags, strin 'options', 'query', ]; - $matchingTags = array_filter($tags, static function(WordPressTag $tag) use ($matchNames): bool { - return in_array($tag->name, $matchNames, true); - }); + $matchingTags = array_filter( + $tags, + static function (WordPressTag $tag) use ($matchNames): bool { + return in_array($tag->name, $matchNames, true); + } + ); foreach ($matchingTags as $tag) { $addTag = clone $tag; @@ -706,11 +670,11 @@ private function getAdditionalTagsFromMap(string $symbolName): array private function addStringTags(string $name, Doc $docComment): ?Doc { - if ( !isset($this->additionalTagStrings[ $name ])) { + if (!isset($this->additionalTagStrings[$name])) { return null; } - $additions = $this->additionalTagStrings[ $name ]; + $additions = $this->additionalTagStrings[$name]; $docCommentText = $docComment->getText(); $newDocComment = sprintf( @@ -848,7 +812,7 @@ private static function getTypeNameFromDescription(Description $tagVariableDescr return self::getTypeNameFromDescriptionString($tagVariableDescription->__toString()); } - private static function getTypeNameFromDescriptionString(string $tagDescription = null): ?string + private static function getTypeNameFromDescriptionString(?string $tagDescription = null): ?string { if ($tagDescription === null) { return null; @@ -912,17 +876,18 @@ private static function getTypeNameFromString(string $tagVariable): ?string ]; foreach ($supportedTypes as $supportedType) { - if (strpos($tagVariableType, "{$supportedType}|") !== false) { - // Move the type that uses the hash notation to the end of union types so the shape works. - $tagVariableType = str_replace("{$supportedType}|", '', $tagVariableType) . "|{$supportedType}"; + if (strpos($tagVariableType, "{$supportedType}|") === false) { + continue; } + // Move the type that uses the hash notation to the end of union types so the shape works. + $tagVariableType = str_replace("{$supportedType}|", '', $tagVariableType) . "|{$supportedType}"; } return $tagVariableType; } /** - * @return WordPressArg[] + * @return list<\WordPressArg> */ private static function getElementsFromDescription(Description $tagDescription, bool $optional): array { @@ -938,7 +903,7 @@ private static function getElementsFromDescription(Description $tagDescription, } /** - * @return WordPressArg[] + * @return list<\WordPressArg> */ private static function getTypesAtLevel(string $text, bool $optional, int $level): array { @@ -1072,7 +1037,7 @@ static function (Node $node): bool { // If wp_die is called with 3rd parameter, we need additional checks. try { $arg = (new ConstExprEvaluator())->evaluateSilently($args[2]->value); - } catch (ConstExprEvaluationException $e) { + } catch (\PhpParser\ConstExprEvaluationException $e) { // If we don't know the value of the 3rd parameter, we can't be sure. continue; } From bf01dad880b7ed1452f5d7f30cf9155bfce92609 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20Sz=C3=A9pe?= Date: Wed, 1 Nov 2023 18:56:43 +0100 Subject: [PATCH 6/6] Check functionMap.php (#126) * Check functionMap.php * Update phpcs.xml.dist --- phpcs.xml.dist | 2 ++ phpstan.neon.dist | 1 + 2 files changed, 3 insertions(+) diff --git a/phpcs.xml.dist b/phpcs.xml.dist index 4985dd4d..fb3af315 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -1,5 +1,7 @@ + finder.php + functionMap.php visitor.php diff --git a/phpstan.neon.dist b/phpstan.neon.dist index f9aea2ba..3b1fa804 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -2,6 +2,7 @@ parameters: level: 8 paths: - finder.php + - functionMap.php - visitor.php - tests/ excludePaths: