Skip to content

Commit

Permalink
Generate stubs for WordPress 6.5.3 (#173)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
IanDelMar committed May 8, 2024
1 parent 4f85d8d commit e611a83
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 40 deletions.
2 changes: 1 addition & 1 deletion source/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"ext-mbstring": "*",
"ext-openssl": "*",
"ext-sodium": "*",
"johnpbloch/wordpress": "6.5.2"
"johnpbloch/wordpress": "6.5.3"
},
"minimum-stability": "stable",
"config": {
Expand Down
46 changes: 22 additions & 24 deletions src/Visitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ class Visitor extends NodeVisitor
private \phpDocumentor\Reflection\DocBlockFactory $docBlockFactory;

/** @var ?array<string,array<int|string,string>> */
private $functionMap = null;
private ?array $functionMap = null;

/** @var array<string, list<\PhpStubs\WordPress\Core\WordPressTag>> */
private $additionalTags = [];
private array $additionalTags = [];

/** @var array<string, list<string>> */
private $additionalTagStrings = [];
private array $additionalTagStrings = [];

private \PhpParser\NodeFinder $nodeFinder;

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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 [];
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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__));
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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
) {
Expand All @@ -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';
}
Expand Down Expand Up @@ -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 '';
}
Expand Down
2 changes: 1 addition & 1 deletion src/WithChildren.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
abstract class WithChildren
{
/** @var list<\PhpStubs\WordPress\Core\WordPressArg> */
public $children = [];
public array $children = [];

public function isArrayShape(): bool
{
Expand Down
9 changes: 3 additions & 6 deletions src/WordPressArg.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> */
public function format(int $level = 1): array
Expand Down
36 changes: 28 additions & 8 deletions wordpress-stubs.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())
{
}
/**
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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)
{
}
/**
Expand Down Expand Up @@ -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
*
Expand Down

0 comments on commit e611a83

Please sign in to comment.