php-stubs / wordpress-stubs

Up-to-date WordPress function and class declaration stubs for static analysis by PHPStan
https://packagist.org/packages/php-stubs/wordpress-stubs
MIT License
135 stars 18 forks source link

`block_template_part` parameter limited to `'header'|'footer'` when `string` should be accepted. #179

Closed lipemat closed 9 hours ago

lipemat commented 1 month ago

The block_template_part function can accept any string although the documentation suggests only 'header' or 'footer'.

To recreate:

  1. Create a custom template part using the site editor.
  2. Call block_template_part with the slug of the custom template.
  3. Notice the function works.

My hunch is the documentation is referring to how WP core uses the parameter which is only 'header' and 'footer'.

Within the stubs the following is generated, but so far I have been unable to explain why. @phpstan-param 'header'|'footer' $part

I created a patch which adjusts the map for this function but ends up doubling up the parameter like so.

* @phpstan-param 'header'|'footer' $part
* @phpstan-param string $part

block-template-part.patch

Works but not ideal.

szepeviktor commented 1 month ago
diff --git a/functionMap.php b/functionMap.php
index 7065d1f..2619d5e 100644
--- a/functionMap.php
+++ b/functionMap.php
@@ -136,4 +136,5 @@ return [
     'WP_Widget::form' => [null, 'instance' => 'T'],
     'WP_Widget::update' => [null, 'new_instance' => 'T', 'old_instance' => 'T'],
     'WP_Widget::widget' => [null, 'instance' => 'T', 'args' => 'array{name:string,id:string,description:string,class:string,before_widget:string,after_widget:string,before_title:string,after_title:string,before_sidebar:string,after_sidebar:string,show_in_rest:boolean,widget_id:string,widget_name:string}'],
+    'block_template_part' => ['void', 'part' => 'string']
 ];
diff --git a/wordpress-stubs.php b/wordpress-stubs.php
index dfdea96..3a4a5d3 100644
--- a/wordpress-stubs.php
+++ b/wordpress-stubs.php
@@ -95084,6 +95084,7 @@ namespace {
      *
      * @param string $part The block template part to print. Either 'header' or 'footer'.
      * @phpstan-param 'header'|'footer' $part
+     * @phpstan-param string $part
      * @phpstan-return void
      */
     function block_template_part($part)
szepeviktor commented 1 month ago

@johnbillion Do you know where @phpstan-param 'header'|'footer' $part comes from? Couldn't find it in this repository.

szepeviktor commented 1 month ago

@lipemat Thank you! What is your specific $part value?

lipemat commented 1 month ago

Varies per project but here are some I was testing with.

  1. 'test-part'
  2. 'gallery'
  3. 'latest-articles'
szepeviktor commented 1 month ago

Do you know where @phpstan-param 'header'|'footer' $part comes from?

https://github.com/php-stubs/wordpress-stubs/blob/a5558fc5edcfc87750bca966d4696c41b6ec5b10/src/Visitor.php#L635

IanDelMar commented 1 month ago

Without losing all the 'either ... or ...' constructs, we cannot fix this in the visitor except by skipping predefined functions. So there are basically two options: 1) exclude specific functions, or 2) use the function map to override tags automatically added by identifying expressions that indicate a restricted set of possible values.

szepeviktor commented 1 month ago

Please do not complicate this more. It can be resolved in local stubs.

swissspidy commented 1 month ago

Let's just fix this in WordPress itself :-)

https://core.trac.wordpress.org/changeset/58268

swissspidy commented 9 hours ago

With 6.6 being released, this issue can be closed once the stubs are updated in this repo.