Expectations of PhpStorm's default WordPress Code Inspection Lint OR Unused Symbols / Unused Declaration in Wordpress Theme add_filter

I've imported a wordpress site as a PhpStorm project using Jetbrains' recommended step.

The site's primary child theme's `functions.php` contains a variety of overridden commercial plugin functions using `add_filter()`.

PhpStorm's default inspection is counting functions defined for the `add_filter()` hook as unused symbols, despite the fact that they are used by the `add_filter` hook itself. For example:

add_filter('ywcm_before_cart_priority', 'yith_cart_message_change_priority');
function yith_cart_message_change_priority(): int
{
return 11;
}

`yith_cart_message_change_priority()` s caught by the code inspector as Unused Symbols / Unused Declaration.

Does this indicate I have not set up Wordpress in PhpStorm correctly? There are many functions defined and implemented using hooks like this.

I realized I can command-click the function name and it does find the add_filter hook as the only "usage":

If this is expected behavior, or a bug?

Is there some tweak to the linting definition I can use that causes PhpStorm to recognize this common pattern in Wordpress theme customization?

For now, I am using the following at the top of the file to handle the function warnings, which I want to avoid so I don't miss actual unused parameters or functions.

/** @noinspection PhpUnused */
/** @noinspection PhpUnusedParameterInspection */

Note, I also must handle unused parameters, which unfortunately are not "unused" because it seems WordPress will potentially provide different responses if these stub values are not specified. Again, I am curious if PhpStorm is supposed to understand these implementations and not warn on them.

0
6 comments

Hi there,

https://youtrack.jetbrains.com/issue/WI-34709

P.S. You can manually add those functions as "entry points" in that particular inspection settings.

0

@andriy thank you for sharing that link and hint on entry points. I’m not familiar with that specification so I will look at that.

The other issue addresses the unused argument concern, so I’ll vote for that and link here.

Previous to this project, I had no idea how common and large the install / developer base is for Wordpress.

Any touches to make PhpStorm a stronger IDE for WP dev is appreciated.

0

Andriy Bazanov

I had a look at adding those functions as entrypoints to those functions and I see I can just click the Add As Entrypoint recommended action. But I don't see what configuration that changes. When I went to undo it, whatever happened seemed to do so outside of undo history.

I saw this SO question that sort of gets at this feature, can you point me at some documentation or explanation on how this is intended for handling this hook function issue in wordpress or even more generally than that?

0

The entry point in general: the IDE do not see where this public function/method is used in the actual code (and hence marks it as unused) .. but it is used from outside (typical example: Controller Action / API endpoint).

See screenshot below on where to find the entry points entries (when "unused method" is reported).

Few tickets to give you some background info on "unused methods / entry points":

 

I'm not really doing WP coding (only basic stuff) so this lack on knowledge of current IDE behaviour on this matter and ability to quickly test it before replying on a real code is what responsible for this confusion. The word "unused" must have triggered the wrong association/issue in my head. Sorry for the possible confusion here.

0

Andriy Bazanov

Thank you for pointing me in this direction, I looked at this area of inspections and at the links you provided.

In the Code patterns area, it looks like the matching function must be inside a class. It is common in wordpress customization to write functions in `functions.php` of a child theme.

So, these do not seem to be available for entry or selection using the code patterns dialog.

In the links you gave, I see that there appears to be a way to do this using the Suppressed annotations option.  I tried adding `@wp-hook` like below, but when I added the flag to a function annotation, it still shows up as an unused declaration (also below)

What would be ideal would be able to write a regex for the first string argument of this function:

add_filter('woocommerce_review_order_after_cart_contents', 'woo_gg_review_order_after_cart_contents', 12);

Which would be recognized then as a function. It doesn't have to be that clever, I could annotate with a tag, but I'm unclear on how to use this.

This person seems to have it figured out but there is not an example.

Any chance we could get a developer advocate or product manager to explain how to use this feature in a blog entry?

 

0

This person seems to have it figured out but there is not an example.

According to the screenshots you attached below, you've successfully figured it out.
You just add the annotation you have in a PHPDoc comment that marks this function/method as used.

On the other hand, I can't reproduce the original issue - since there is a reference between the invocation and the definition (meaning, you can Ctrl/Cmd+click one to get to another), there is supposed to be no inspection hit.

Please check the screencast: https://drive.google.com/file/d/1OVLTdPIKrPID4fhLsfRGbhPMd3zIiU29
As soon as I enabled WordPress integration, the inspection hit went away.

0

Please sign in to leave a comment.