Snapey's avatar
Level 122

Unused method is actually used

I have this code;

    private function customerSpecifiedHandling($event)
    {
        $method = setting('system_events.callback','');
        if(empty($method)) return;

        if(method_exists($this, $method)) {
            $this->$method($event);
        }

    }

    private function moveDriverDisplayName($event)
    {
        //
	}

I configure the method name in the tenant's database. If they don't have this setting then the method is not called. Its a way to compartmentalise bespoke functionality in a standard method for a customer.

The problem is, static analysis of `moveDriverDisplayName' thinks the code is not used anywhere, and dims the method title, which messes with my head a little.

Is there a way via docblock I can indicate that the method is in fact used?

Whilst we are on the subject, is there a good reference to possible docblock @ parameters?

1 like
8 replies
LaryAI's avatar
Level 58

To address the issue of static analysis tools marking your method as unused, you can use a docblock annotation to indicate that the method is used dynamically. This can help inform the static analysis tool that the method is indeed used, even though it might not be directly referenced in the code.

One common annotation for this purpose is @used-by. However, not all static analysis tools support this annotation. Another approach is to use @method or @see annotations to provide additional context.

Here's how you can modify your code with appropriate docblock annotations:

/**
 * Handles customer-specified events.
 *
 * @param mixed $event The event to handle.
 * @return void
 */
private function customerSpecifiedHandling($event)
{
    $method = setting('system_events.callback', '');
    if (empty($method)) return;

    if (method_exists($this, $method)) {
        $this->$method($event);
    }
}

/**
 * Moves the driver display name.
 *
 * @param mixed $event The event to handle.
 * @return void
 *
 * @used-by customerSpecifiedHandling
 */
private function moveDriverDisplayName($event)
{
    //
}

In this example, the @used-by annotation is added to the moveDriverDisplayName method to indicate that it is used by the customerSpecifiedHandling method. This can help some static analysis tools understand the relationship between the methods.

If your static analysis tool does not support @used-by, you might need to check its documentation for the appropriate annotation or configuration to handle dynamically called methods.

gych's avatar

On this PHPDoc manual page, you can find the available docblock @ parameters but I'm not sure if its possible to use docblock to indicate that this method is actually used. This is the doc link for @method https://manual.phpdoc.org/HTMLSmartyConverter/HandS/phpDocumentor/tutorial_tags.method.pkg.html

Which tool are you using for static analysis?

There are some alternative solutions that could help address this issue, such as mapping the possible functions explicitly, but I'm not sure if these are considered best practice.

1 like
Snapey's avatar
Level 122

I'm using intelliphense in VS code

it says Symbol 'moveDriverDisplayName' is declared but not used

The block suggested by @tisuchi does not work unfortunately, nor the suggestion from @laryai

1 like
gych's avatar
gych
Best Answer
Level 29

@Snapey For VS code intelliphense you can use @disregard for this, this will tell the analysis to ignore the error if the error code is specified or all errors if the code is not specified.

/** @disregard [OPTIONAL CODE] [OPTIONAL DESCRIPTION] */
2 likes
vincent15000's avatar

@snapey

static analysis of `moveDriverDisplayName' thinks the code is not used anywhere

What do you mean when you are talking about static analysis ?

Is it just a problem with VSCode ?

Snapey's avatar
Level 122

@vincent15000 static analysis in this case, is the Intelliphense plugin checking that my code is consistent and without errors. It does this by evaluating the code (static) to see if variables just appeared out of nowhere or you tried to call a function that it cannot find in your code, etc,etc.etc

1 like

Please or to participate in this conversation.