StoutLogic / acf-builder

An Advanced Custom Field Configuration Builder
GNU General Public License v2.0
794 stars 62 forks source link

PHPStan error with addFields method, using composing fields #137

Closed perruche closed 3 years ago

perruche commented 3 years ago

Hello,

First thanks a lot for this wonderful lib, it's a pleasure to work with it.

I am getting a PHPStan Error which should not be, while using composing fields.

Call to an undefined method StoutLogic\AcfBuilder\FieldBuilder::addFields()

addFields is the only method that is throwing this error

A code snippet of the implementation :

<?php

class ACFManager {
 /**
  * Intro fields.
  *
  * @var \StoutLogic\AcfBuilder\FieldsBuilder
  **/
  protected $intro;
  public function __construct() {
    $this->intro = new FieldsBuilder( 'wysiwyg' );
    $this->intro->addWysiwyg( 'intro' );

    add_action( 'acf/init', array( $this, 'register_acf_post_group' ) );
 }

 /**
  * Register ACF Field group for Posts
  *
  * @return void
  */
  public function register_acf_post_group() {
    $post_group = new FieldsBuilder( 'post_group' );
    $post_group->addFields( $this->intro )
    ->setLocation( 'post_type', '==', 'post' );

    acf_add_local_field_group( $post_group->build() );
  }
}
stevep commented 3 years ago

@perruche This is interesting. The error message references, singular class name, FieldBuilder::addFields rather than plural FieldBuilders::addFields though your posted code snippet only has the plural version. Unfortunately for this instance, both class names do exist in the code base, but only the plural version has the addFields method. If there is more to the snippet, I'd be happy to look at it if you post it.

perruche commented 3 years ago

I triple check my local code to make sure i only have plural FieldBuilders which is the case. There is more code in my local code with more fields and field groups, but no differences from the snippet above 😞 Not sure why PHPStan is reading the correct class

Thanks for the help tho

titouanmathis commented 3 years ago

I dug a bit into this issue, and it seems that adding the methods throwing a warning to the doc block of the FieldBuilder (singular) class does the trick for PHPStan:

diff --git a/src/FieldBuilder.php b/src/FieldBuilder.php
index 2460728..22fe301 100644
--- a/src/FieldBuilder.php
+++ b/src/FieldBuilder.php
@@ -5,6 +5,7 @@ namespace StoutLogic\AcfBuilder;
 /**
  * Builds configurations for an ACF Field
  * @method FieldBuilder addField(string $name, string $type, array $args = [])
+ * @method FieldBuilder addFields(FieldsBuilder|array $fields)
  * @method FieldBuilder addChoiceField(string $name, string $type, array $args = [])
  * @method FieldBuilder addText(string $name, array $args = [])
  * @method FieldBuilder addTextarea(string $name, array $args = [])
@@ -34,6 +35,7 @@ namespace StoutLogic\AcfBuilder;
  * @method FieldBuilder addTab(string $label, array $args = [])
  * @method FieldBuilder addRange(string $name, array $args = [])
  * @method FieldBuilder addMessage(string $label, string $message, array $args = [])
+ * @method FieldBuilder addRelationship(string $name, array $args = [])
  * @method GroupBuilder addGroup(string $name, array $args = [])
  * @method GroupBuilder endGroup()
  * @method RepeaterBuilder addRepeater(string $name, array $args = [])

As FieldBuilder can delegate a method call to its parent context by extending ParentDelegationBuilder, I think that the addFields method can be safely added to the doc block. Though I am not very familiar with all the internals of the builders, so I might be wrong.