From e611a83292d02055a25f83291a98fadd0c21e092 Mon Sep 17 00:00:00 2001 From: IanDelMar <42134098+IanDelMar@users.noreply.github.com> Date: Wed, 8 May 2024 04:12:31 +0200 Subject: [PATCH] Generate stubs for WordPress 6.5.3 (#173) * use type declarations * generate stubs for WordPress 6.5.3 * use type declarations * remove dead continue * reduce code nesting * catch multiple exception types at once * fix cs * use strict comparison * reduce nesting * replace isset --- source/composer.json | 2 +- src/Visitor.php | 46 +++++++++++++++++++++----------------------- src/WithChildren.php | 2 +- src/WordPressArg.php | 9 +++------ wordpress-stubs.php | 36 ++++++++++++++++++++++++++-------- 5 files changed, 55 insertions(+), 40 deletions(-) diff --git a/source/composer.json b/source/composer.json index 6caa138..e334512 100644 --- a/source/composer.json +++ b/source/composer.json @@ -7,7 +7,7 @@ "ext-mbstring": "*", "ext-openssl": "*", "ext-sodium": "*", - "johnpbloch/wordpress": "6.5.2" + "johnpbloch/wordpress": "6.5.3" }, "minimum-stability": "stable", "config": { diff --git a/src/Visitor.php b/src/Visitor.php index 0122dec..de507cf 100644 --- a/src/Visitor.php +++ b/src/Visitor.php @@ -36,13 +36,13 @@ class Visitor extends NodeVisitor private \phpDocumentor\Reflection\DocBlockFactory $docBlockFactory; /** @var ?array> */ - private $functionMap = null; + private ?array $functionMap = null; /** @var array> */ - private $additionalTags = []; + private array $additionalTags = []; /** @var array> */ - private $additionalTagStrings = []; + private array $additionalTagStrings = []; private \PhpParser\NodeFinder $nodeFinder; @@ -77,7 +77,7 @@ public function enterNode(Node $node) $parent = $this->stack[count($this->stack) - 2]; \assert($parent instanceof \PhpParser\Node\Stmt\ClassLike); - if (isset($parent->name)) { + if ($parent->name !== null) { $symbolName = sprintf( '%1$s::%2$s', $parent->name->name, @@ -146,7 +146,7 @@ public function getStubStmts(): array private function postProcessNode(Node $node): void { - if (isset($node->stmts) && is_array($node->stmts)) { + if (property_exists($node, 'stmts') && is_array($node->stmts)) { foreach ($node->stmts as $stmt) { $this->postProcessNode($stmt); } @@ -202,9 +202,7 @@ private function generateAdditionalTagsFromDoc(Doc $docComment): array try { $docblock = $this->docBlockFactory->create($docCommentText); - } catch (\RuntimeException $e) { - return []; - } catch (\InvalidArgumentException $e) { + } catch (\RuntimeException | \InvalidArgumentException $e) { return []; } @@ -260,9 +258,7 @@ private function addTags(string $name, Doc $docComment): ?Doc try { $docblock = $this->docBlockFactory->create($docCommentText); - } catch (\RuntimeException $e) { - return null; - } catch (\InvalidArgumentException $e) { + } catch (\RuntimeException | \InvalidArgumentException $e) { return null; } @@ -430,7 +426,7 @@ static function (WordPressTag $tag) use ($matchNames): bool { */ private function getAdditionalTagsFromMap(string $symbolName): array { - if (! isset($this->functionMap)) { + if ($this->functionMap === null) { $this->functionMap = require sprintf('%s/functionMap.php', dirname(__DIR__)); } @@ -495,7 +491,7 @@ private function getAdditionFromParam(Param $tag): ?WordPressTag $tagVariableType = $tag->getType(); // Skip if information we need is missing. - if (! $tagDescription instanceof Description || ! $tagVariableName || ! $tagVariableType instanceof Type) { + if (! $tagDescription instanceof Description || $tagVariableName === null || $tagVariableName === '' || ! $tagVariableType instanceof Type) { return null; } @@ -636,7 +632,7 @@ private static function getTypeNameFromDescriptionString(?string $tagDescription */ $matched = preg_match("#(?>returns|either|one of|accepts|values are|:) ('.+'),? (?>or|and) '([^']+)'#i", $fullDescription, $matches); - if (! $matched) { + if ($matched !== 1) { return null; } @@ -805,7 +801,7 @@ private function voidOrNever(Node $node): string $this->nodeFinder->findFirst( $returnStmts, static function (Node $node): bool { - return isset($node->expr); + return property_exists($node, 'expr') && $node->expr !== null; } ) instanceof Node ) { @@ -823,10 +819,11 @@ static function (Node $node): bool { } // If a first level statement is exit/die, it's return type never. if ($stmt->expr instanceof Exit_) { - if ($stmt->expr->expr instanceof String_) { - if (strpos($stmt->expr->expr->value, 'must be overridden') !== false) { - return ''; - } + if (! $stmt->expr->expr instanceof String_) { + return 'never'; + } + if (strpos($stmt->expr->expr->value, 'must be overridden') !== false) { + return ''; } return 'never'; } @@ -859,13 +856,14 @@ static function (Node $node): bool { if (is_int($arg)) { return 'never'; } - if (is_array($arg)) { - if (! isset($arg['exit']) || (bool)$arg['exit'] === true) { - return 'never'; - } + + if (! is_array($arg)) { + continue; } - continue; + if (! array_key_exists('exit', $arg) || (bool)$arg['exit']) { + return 'never'; + } } return ''; } diff --git a/src/WithChildren.php b/src/WithChildren.php index 7ffa39f..043458c 100644 --- a/src/WithChildren.php +++ b/src/WithChildren.php @@ -7,7 +7,7 @@ abstract class WithChildren { /** @var list<\PhpStubs\WordPress\Core\WordPressArg> */ - public $children = []; + public array $children = []; public function isArrayShape(): bool { diff --git a/src/WordPressArg.php b/src/WordPressArg.php index 3f0dac9..c370a0e 100644 --- a/src/WordPressArg.php +++ b/src/WordPressArg.php @@ -6,14 +6,11 @@ final class WordPressArg extends WithChildren { - /** @var string */ - public $type; + public string $type; - /** @var bool */ - public $optional = false; + public bool $optional = false; - /** @var ?string */ - public $name = null; + public ?string $name = null; /** @return list */ public function format(int $level = 1): array diff --git a/wordpress-stubs.php b/wordpress-stubs.php index 49b09f5..d117b6c 100644 --- a/wordpress-stubs.php +++ b/wordpress-stubs.php @@ -53489,11 +53489,14 @@ protected function get_block_classes($style_nodes) * * @since 6.1.0 * @since 6.3.0 Reduced specificity for layout margin rules. + * @since 6.5.1 Only output rules referencing content and wide sizes when values exist. + * @since 6.5.3 Add types parameter to check if only base layout styles are needed. * * @param array $block_metadata Metadata about the block to get styles for. + * @param array $types Optional. Types of styles to output. If empty, all styles will be output. * @return string Layout styles for the block. */ - protected function get_layout_styles($block_metadata) + protected function get_layout_styles($block_metadata, $types = array()) { } /** @@ -76400,7 +76403,7 @@ public function delete_item($request) * @since 5.8.0 * * @param WP_REST_Request $request Request object. - * @return stdClass Changes to pass to wp_update_post. + * @return stdClass|WP_Error Changes to pass to wp_update_post. */ protected function prepare_item_for_database($request) { @@ -94986,6 +94989,24 @@ function _wp_build_title_and_description_for_single_post_type_block_template($po function _wp_build_title_and_description_for_taxonomy_block_template($taxonomy, $slug, \WP_Block_Template $template) { } + /** + * Builds a block template object from a post object. + * + * This is a helper function that creates a block template object from a given post object. + * It is self-sufficient in that it only uses information passed as arguments; it does not + * query the database for additional information. + * + * @since 6.5.3 + * @access private + * + * @param WP_Post $post Template post. + * @param array $terms Additional terms to inform the template object. + * @param array $meta Additional meta fields to inform the template object. + * @return WP_Block_Template|WP_Error Template or error object. + */ + function _build_block_template_object_from_post_object($post, $terms = array(), $meta = array()) + { + } /** * Builds a unified template object based a post Object. * @@ -95140,12 +95161,12 @@ function get_template_hierarchy($slug, $is_custom = \false, $template_prefix = ' * @since 6.5.0 * @access private * - * @param stdClass $post An object representing a template or template part - * prepared for inserting or updating the database. - * @param WP_REST_Request $request Request object. - * @return stdClass The updated object representing a template or template part. + * @param stdClass $changes An object representing a template or template part + * prepared for inserting or updating the database. + * @param WP_REST_Request $deprecated Deprecated. Not used. + * @return stdClass|WP_Error The updated object representing a template or template part. */ - function inject_ignored_hooked_blocks_metadata_attributes($post, $request) + function inject_ignored_hooked_blocks_metadata_attributes($changes, $deprecated = \null) { } /** @@ -113883,7 +113904,6 @@ function wp_register_script($handle, $src, $deps = array(), $ver = \false, $args * * @see WP_Scripts::localize() * @link https://core.trac.wordpress.org/ticket/11520 - * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts. * * @since 2.2.0 *