timber icon indicating copy to clipboard operation
timber copied to clipboard

2.x Update PHPStan setup

Open gchtr opened this issue 3 years ago • 6 comments

Issue

There are some PHP libraries we can use to provide function stubs for PHPStan instead of adding the ourselves.

Solution

Use php-stubs/acf-pro-stubs and php-stubs/wp-cli-stubs.

For now, this isn’t working because the function signature for acf_get_field_type() in php-stubs/acf-pro-stubs.

 ------ -------------------------------------------------------
  Line   Integration/AcfIntegration.php
 ------ -------------------------------------------------------
  244    Result of function acf_get_field_type (void) is used.
  245    Result of function acf_get_field_type (void) is used.
  246    Result of function acf_get_field_type (void) is used.
  247    Result of function acf_get_field_type (void) is used.
  248    Result of function acf_get_field_type (void) is used.
  249    Result of function acf_get_field_type (void) is used.
  250    Result of function acf_get_field_type (void) is used.
  251    Result of function acf_get_field_type (void) is used.
  252    Result of function acf_get_field_type (void) is used.
 ------ -------------------------------------------------------

I guess it’s because ACF uses a return type of n/a in the DocBlock for acf_get_field_type(), which is translated to void. But actually it should return mixed, when we look at the underlying acf()->fields->get_field_type() function.

For an easy fix, we could switch to using acf()->fields->get_field_type() instead of acf_get_field_type(), but maybe this is an issue that can be fixed in the php-stubs/acf-pro-stubs library.

I hope that @szepeviktor can help us out here 😊.

Impact

Less code to manage.

Usage Changes

None.

gchtr avatar Aug 15 '22 19:08 gchtr

Use php-stubs/acf-pro-stubs and php-stubs/wp-cli-stubs.

I suspect these are fine packages :)

szepeviktor avatar Aug 15 '22 19:08 szepeviktor

@szepeviktor Well, they apparently were made by someone who knows his stuff 😉💪

gchtr avatar Aug 15 '22 19:08 gchtr

I guess it’s because ACF uses a return type of n/a in the DocBlock

I contacted Elliot and he was happy about docblock fixes but refused them. Currently invalid docblocks are dumbfixed with string replacement. https://github.com/php-stubs/acf-pro-stubs/blob/7e44b653bd72e5eebf725f3af6b5afaae3039dcf/generate.sh#L30-L43

Fixing those could be also be done with sed.

szepeviktor avatar Aug 15 '22 19:08 szepeviktor

I would gladly apply a patch on top the released stubs if you provide me one fixing functions in includes/fields.php. That would go like this: patch -p 1 < acf-pro-field-function-fixes.patch

szepeviktor avatar Aug 15 '22 20:08 szepeviktor

@szepeviktor Thanks for your insights!

I contacted Elliot and he was happy about docblock fixes but refused them.

I see 🙃.

Currently invalid docblocks are dumbfixed by string replacement. php-stubs/acf-pro-stubs@7e44b65/generate.sh#L30-L43

Yes, I saw that :). I’m not familiar enough with sed though.

I would gladly apply a patch on top the released stubs if you provide me one fixing functions in includes/fields.php. That would go like this: patch -p 1 < acf-pro-field-function-fixes.patch

So, you mean I could provide you with a .patch file that fixes that particular function signature and you could apply that when generating the stubs? I think I could prepare that.

gchtr avatar Aug 15 '22 20:08 gchtr

I think I could prepare that.

All right! To be clear: please start with the stubs.

szepeviktor avatar Aug 15 '22 20:08 szepeviktor

I decided to go the easy way and just use acf()->fields->get_field_type() instead of acf_get_field_type() to make it work with the php-stubs/acf-pro-stubs package.

gchtr avatar Oct 19 '22 20:10 gchtr