From 2a81a4d1845fc5875817efdf590a343df927f735 Mon Sep 17 00:00:00 2001 From: Marian <42134098+IanDelMar@users.noreply.github.com> Date: Tue, 10 Sep 2024 13:21:14 +0200 Subject: [PATCH 1/5] Add __get and __call for WP_Query --- functionMap.php | 1 + tests/TypeInferenceTest.php | 1 + tests/data/wp_query.php | 14 ++++++++++++++ wordpress-stubs.php | 3 +++ 4 files changed, 19 insertions(+) create mode 100644 tests/data/wp_query.php diff --git a/functionMap.php b/functionMap.php index 17f0291..f109857 100644 --- a/functionMap.php +++ b/functionMap.php @@ -192,4 +192,5 @@ 'size_format' => ["(\$bytes is not numeric ? false : (\$bytes is negative-int|'0' ? false : string))"], 'WP_Translations::translate' => ['($singular is null ? null : string)'], 'WP_Translations::translate_plural' => ['($singular is null ? null : ($plural is null ? T : string))', '@phpstan-template T' => 'of string|null', 'singular' => 'T', 'count' => 'int'], + 'WP_Query' => [null, '@phpstan-property-read bool $query_vars_changed' => '', '@phpstan-property-read bool|string $query_vars_hash' => '', '@phpstan-method void init_query_flags()' => ''], ]; diff --git a/tests/TypeInferenceTest.php b/tests/TypeInferenceTest.php index b2edf2f..3d827e5 100644 --- a/tests/TypeInferenceTest.php +++ b/tests/TypeInferenceTest.php @@ -55,6 +55,7 @@ public function dataFileAsserts(): iterable yield from $this->gatherAssertTypes(__DIR__ . '/data/wp_list_categories.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/wp_list_pages.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/wp_parse_list.php'); + yield from $this->gatherAssertTypes(__DIR__ . '/data/wp_query.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/wp_rest_request.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/wp_tag_cloud.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/wp_theme.php'); diff --git a/tests/data/wp_query.php b/tests/data/wp_query.php new file mode 100644 index 0000000..bdfed51 --- /dev/null +++ b/tests/data/wp_query.php @@ -0,0 +1,14 @@ +query_vars_changed); +assertType('string|bool', $wpQuery->$query_vars_hash); +assertType('void', $wpQuery->init_query_flags()); diff --git a/wordpress-stubs.php b/wordpress-stubs.php index ef7e231..ec7eaee 100644 --- a/wordpress-stubs.php +++ b/wordpress-stubs.php @@ -46297,6 +46297,9 @@ public function to_array() * * @since 1.5.0 * @since 4.5.0 Removed the `$comments_popup` property. + * @phpstan-property-read bool $query_vars_changed + * @phpstan-property-read bool|string $query_vars_hash + * @phpstan-method void init_query_flags() */ #[\AllowDynamicProperties] class WP_Query From d663d2027be2190826b28a3b2f5ce7e92558bb65 Mon Sep 17 00:00:00 2001 From: Marian <42134098+IanDelMar@users.noreply.github.com> Date: Tue, 10 Sep 2024 13:23:19 +0200 Subject: [PATCH 2/5] Fix wp_query test --- tests/data/wp_query.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/data/wp_query.php b/tests/data/wp_query.php index bdfed51..837aab5 100644 --- a/tests/data/wp_query.php +++ b/tests/data/wp_query.php @@ -10,5 +10,5 @@ $wpQuery = new WP_Query(); assertType('bool', $wpQuery->query_vars_changed); -assertType('string|bool', $wpQuery->$query_vars_hash); -assertType('void', $wpQuery->init_query_flags()); +assertType('bool|string', $wpQuery->query_vars_hash); +assertType('null', $wpQuery->init_query_flags()); From 52e2e068b55fe28e4814dc7cf27dddad5a2722a8 Mon Sep 17 00:00:00 2001 From: Marian <42134098+IanDelMar@users.noreply.github.com> Date: Tue, 10 Sep 2024 13:27:20 +0200 Subject: [PATCH 3/5] Add note about void->null conversion --- tests/data/wp_query.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/data/wp_query.php b/tests/data/wp_query.php index 837aab5..766a6c3 100644 --- a/tests/data/wp_query.php +++ b/tests/data/wp_query.php @@ -1,5 +1,13 @@ Date: Tue, 10 Sep 2024 13:29:53 +0200 Subject: [PATCH 4/5] Fix cs --- functionMap.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/functionMap.php b/functionMap.php index f109857..1baf52b 100644 --- a/functionMap.php +++ b/functionMap.php @@ -192,5 +192,5 @@ 'size_format' => ["(\$bytes is not numeric ? false : (\$bytes is negative-int|'0' ? false : string))"], 'WP_Translations::translate' => ['($singular is null ? null : string)'], 'WP_Translations::translate_plural' => ['($singular is null ? null : ($plural is null ? T : string))', '@phpstan-template T' => 'of string|null', 'singular' => 'T', 'count' => 'int'], - 'WP_Query' => [null, '@phpstan-property-read bool $query_vars_changed' => '', '@phpstan-property-read bool|string $query_vars_hash' => '', '@phpstan-method void init_query_flags()' => ''], + 'WP_Query' => [null, '@phpstan-property-read bool $query_vars_changed' => '', '@phpstan-property-read bool|string $query_vars_hash' => '', '@phpstan-method void init_query_flags()' => ''], ]; From 844abff6b246891f1e26da0a4beacdcb8a6d9ccb Mon Sep 17 00:00:00 2001 From: Marian <42134098+IanDelMar@users.noreply.github.com> Date: Tue, 10 Sep 2024 13:30:43 +0200 Subject: [PATCH 5/5] Fix cs --- tests/data/wp_query.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/data/wp_query.php b/tests/data/wp_query.php index 766a6c3..6c557fd 100644 --- a/tests/data/wp_query.php +++ b/tests/data/wp_query.php @@ -12,9 +12,10 @@ namespace PhpStubs\WordPress\Core\Tests; -use function PHPStan\Testing\assertType; use WP_Query; +use function PHPStan\Testing\assertType; + $wpQuery = new WP_Query(); assertType('bool', $wpQuery->query_vars_changed);