Skip to content

Commit e611a83

Browse files
authored
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
1 parent 4f85d8d commit e611a83

File tree

5 files changed

+55
-40
lines changed

5 files changed

+55
-40
lines changed

source/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"ext-mbstring": "*",
88
"ext-openssl": "*",
99
"ext-sodium": "*",
10-
"johnpbloch/wordpress": "6.5.2"
10+
"johnpbloch/wordpress": "6.5.3"
1111
},
1212
"minimum-stability": "stable",
1313
"config": {

src/Visitor.php

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ class Visitor extends NodeVisitor
3636
private \phpDocumentor\Reflection\DocBlockFactory $docBlockFactory;
3737

3838
/** @var ?array<string,array<int|string,string>> */
39-
private $functionMap = null;
39+
private ?array $functionMap = null;
4040

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

4444
/** @var array<string, list<string>> */
45-
private $additionalTagStrings = [];
45+
private array $additionalTagStrings = [];
4646

4747
private \PhpParser\NodeFinder $nodeFinder;
4848

@@ -77,7 +77,7 @@ public function enterNode(Node $node)
7777
$parent = $this->stack[count($this->stack) - 2];
7878
\assert($parent instanceof \PhpParser\Node\Stmt\ClassLike);
7979

80-
if (isset($parent->name)) {
80+
if ($parent->name !== null) {
8181
$symbolName = sprintf(
8282
'%1$s::%2$s',
8383
$parent->name->name,
@@ -146,7 +146,7 @@ public function getStubStmts(): array
146146

147147
private function postProcessNode(Node $node): void
148148
{
149-
if (isset($node->stmts) && is_array($node->stmts)) {
149+
if (property_exists($node, 'stmts') && is_array($node->stmts)) {
150150
foreach ($node->stmts as $stmt) {
151151
$this->postProcessNode($stmt);
152152
}
@@ -202,9 +202,7 @@ private function generateAdditionalTagsFromDoc(Doc $docComment): array
202202

203203
try {
204204
$docblock = $this->docBlockFactory->create($docCommentText);
205-
} catch (\RuntimeException $e) {
206-
return [];
207-
} catch (\InvalidArgumentException $e) {
205+
} catch (\RuntimeException | \InvalidArgumentException $e) {
208206
return [];
209207
}
210208

@@ -260,9 +258,7 @@ private function addTags(string $name, Doc $docComment): ?Doc
260258

261259
try {
262260
$docblock = $this->docBlockFactory->create($docCommentText);
263-
} catch (\RuntimeException $e) {
264-
return null;
265-
} catch (\InvalidArgumentException $e) {
261+
} catch (\RuntimeException | \InvalidArgumentException $e) {
266262
return null;
267263
}
268264

@@ -430,7 +426,7 @@ static function (WordPressTag $tag) use ($matchNames): bool {
430426
*/
431427
private function getAdditionalTagsFromMap(string $symbolName): array
432428
{
433-
if (! isset($this->functionMap)) {
429+
if ($this->functionMap === null) {
434430
$this->functionMap = require sprintf('%s/functionMap.php', dirname(__DIR__));
435431
}
436432

@@ -495,7 +491,7 @@ private function getAdditionFromParam(Param $tag): ?WordPressTag
495491
$tagVariableType = $tag->getType();
496492

497493
// Skip if information we need is missing.
498-
if (! $tagDescription instanceof Description || ! $tagVariableName || ! $tagVariableType instanceof Type) {
494+
if (! $tagDescription instanceof Description || $tagVariableName === null || $tagVariableName === '' || ! $tagVariableType instanceof Type) {
499495
return null;
500496
}
501497

@@ -636,7 +632,7 @@ private static function getTypeNameFromDescriptionString(?string $tagDescription
636632
*/
637633
$matched = preg_match("#(?>returns|either|one of|accepts|values are|:) ('.+'),? (?>or|and) '([^']+)'#i", $fullDescription, $matches);
638634

639-
if (! $matched) {
635+
if ($matched !== 1) {
640636
return null;
641637
}
642638

@@ -805,7 +801,7 @@ private function voidOrNever(Node $node): string
805801
$this->nodeFinder->findFirst(
806802
$returnStmts,
807803
static function (Node $node): bool {
808-
return isset($node->expr);
804+
return property_exists($node, 'expr') && $node->expr !== null;
809805
}
810806
) instanceof Node
811807
) {
@@ -823,10 +819,11 @@ static function (Node $node): bool {
823819
}
824820
// If a first level statement is exit/die, it's return type never.
825821
if ($stmt->expr instanceof Exit_) {
826-
if ($stmt->expr->expr instanceof String_) {
827-
if (strpos($stmt->expr->expr->value, 'must be overridden') !== false) {
828-
return '';
829-
}
822+
if (! $stmt->expr->expr instanceof String_) {
823+
return 'never';
824+
}
825+
if (strpos($stmt->expr->expr->value, 'must be overridden') !== false) {
826+
return '';
830827
}
831828
return 'never';
832829
}
@@ -859,13 +856,14 @@ static function (Node $node): bool {
859856
if (is_int($arg)) {
860857
return 'never';
861858
}
862-
if (is_array($arg)) {
863-
if (! isset($arg['exit']) || (bool)$arg['exit'] === true) {
864-
return 'never';
865-
}
859+
860+
if (! is_array($arg)) {
861+
continue;
866862
}
867863

868-
continue;
864+
if (! array_key_exists('exit', $arg) || (bool)$arg['exit']) {
865+
return 'never';
866+
}
869867
}
870868
return '';
871869
}

src/WithChildren.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
abstract class WithChildren
88
{
99
/** @var list<\PhpStubs\WordPress\Core\WordPressArg> */
10-
public $children = [];
10+
public array $children = [];
1111

1212
public function isArrayShape(): bool
1313
{

src/WordPressArg.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,11 @@
66

77
final class WordPressArg extends WithChildren
88
{
9-
/** @var string */
10-
public $type;
9+
public string $type;
1110

12-
/** @var bool */
13-
public $optional = false;
11+
public bool $optional = false;
1412

15-
/** @var ?string */
16-
public $name = null;
13+
public ?string $name = null;
1714

1815
/** @return list<string> */
1916
public function format(int $level = 1): array

wordpress-stubs.php

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53489,11 +53489,14 @@ protected function get_block_classes($style_nodes)
5348953489
*
5349053490
* @since 6.1.0
5349153491
* @since 6.3.0 Reduced specificity for layout margin rules.
53492+
* @since 6.5.1 Only output rules referencing content and wide sizes when values exist.
53493+
* @since 6.5.3 Add types parameter to check if only base layout styles are needed.
5349253494
*
5349353495
* @param array $block_metadata Metadata about the block to get styles for.
53496+
* @param array $types Optional. Types of styles to output. If empty, all styles will be output.
5349453497
* @return string Layout styles for the block.
5349553498
*/
53496-
protected function get_layout_styles($block_metadata)
53499+
protected function get_layout_styles($block_metadata, $types = array())
5349753500
{
5349853501
}
5349953502
/**
@@ -76400,7 +76403,7 @@ public function delete_item($request)
7640076403
* @since 5.8.0
7640176404
*
7640276405
* @param WP_REST_Request $request Request object.
76403-
* @return stdClass Changes to pass to wp_update_post.
76406+
* @return stdClass|WP_Error Changes to pass to wp_update_post.
7640476407
*/
7640576408
protected function prepare_item_for_database($request)
7640676409
{
@@ -94986,6 +94989,24 @@ function _wp_build_title_and_description_for_single_post_type_block_template($po
9498694989
function _wp_build_title_and_description_for_taxonomy_block_template($taxonomy, $slug, \WP_Block_Template $template)
9498794990
{
9498894991
}
94992+
/**
94993+
* Builds a block template object from a post object.
94994+
*
94995+
* This is a helper function that creates a block template object from a given post object.
94996+
* It is self-sufficient in that it only uses information passed as arguments; it does not
94997+
* query the database for additional information.
94998+
*
94999+
* @since 6.5.3
95000+
* @access private
95001+
*
95002+
* @param WP_Post $post Template post.
95003+
* @param array $terms Additional terms to inform the template object.
95004+
* @param array $meta Additional meta fields to inform the template object.
95005+
* @return WP_Block_Template|WP_Error Template or error object.
95006+
*/
95007+
function _build_block_template_object_from_post_object($post, $terms = array(), $meta = array())
95008+
{
95009+
}
9498995010
/**
9499095011
* Builds a unified template object based a post Object.
9499195012
*
@@ -95140,12 +95161,12 @@ function get_template_hierarchy($slug, $is_custom = \false, $template_prefix = '
9514095161
* @since 6.5.0
9514195162
* @access private
9514295163
*
95143-
* @param stdClass $post An object representing a template or template part
95144-
* prepared for inserting or updating the database.
95145-
* @param WP_REST_Request $request Request object.
95146-
* @return stdClass The updated object representing a template or template part.
95164+
* @param stdClass $changes An object representing a template or template part
95165+
* prepared for inserting or updating the database.
95166+
* @param WP_REST_Request $deprecated Deprecated. Not used.
95167+
* @return stdClass|WP_Error The updated object representing a template or template part.
9514795168
*/
95148-
function inject_ignored_hooked_blocks_metadata_attributes($post, $request)
95169+
function inject_ignored_hooked_blocks_metadata_attributes($changes, $deprecated = \null)
9514995170
{
9515095171
}
9515195172
/**
@@ -113883,7 +113904,6 @@ function wp_register_script($handle, $src, $deps = array(), $ver = \false, $args
113883113904
*
113884113905
* @see WP_Scripts::localize()
113885113906
* @link https://core.trac.wordpress.org/ticket/11520
113886-
* @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
113887113907
*
113888113908
* @since 2.2.0
113889113909
*

0 commit comments

Comments
 (0)