phpmd icon indicating copy to clipboard operation
phpmd copied to clipboard

SuppressWarnings annotation for a function doesn't work

Open Janiczek opened this issue 9 years ago • 4 comments
trafficstars

PHPMD sees this:

function ab() {
    return 1;
}

and returns:

<violation beginline="2" endline="4" rule="ShortMethodName" ruleset="Naming Rules" package="+global" externalInfoUrl="http://phpmd.org/rules/naming.html#shortmethodname" function="ab" priority="3">
  Avoid using short method names like ::ab(). The configured minimum method name length is 3.
</violation>

Using a @SuppressWarnings annotation like this has no effect:

/**
 * @SuppressWarnings(PHPMD.ShortMethodName)
 */
function ab() {
    return 1;
}

I don't run PHPMD with --strict. I have verified that the annotation has effect on classes and methods for me.

How should I use the annotation for functions?

Janiczek avatar Jan 08 '16 14:01 Janiczek

This also happens when trying to suppress a short variable on a parameter. I mention it here because it seems related.

Example:

class Model
{
    /**
     * @var integer The id of the model.
     * @SuppressWarnings(PHPMD.ShortVariable)
     */
    public $id;
}

michaelherold avatar Apr 11 '16 14:04 michaelherold

Nobody? This is still open?

glamorous avatar Feb 23 '18 13:02 glamorous

I think it's a bug

/**
 * @SuppressWarnings(PHPMD.UnusedLocalVariable)
 * --> It work
 */
if (! function_exists('example')) {
  /**
   * @SuppressWarnings(PHPMD.UnusedLocalVariable)
   * --> doesn't work
   */
    function example(array $array)
    {
        /**
         * @SuppressWarnings(PHPMD.UnusedLocalVariable)
         * --> doesn't work
         */
        foreach ($array as $key => $value) {
            echo $key;
        }
    }
}

ozelotdev avatar Nov 17 '18 11:11 ozelotdev

We have 3 different bugs here:

Original bug of this stream:

/**
 * @SuppressWarnings(PHPMD.ShortMethodName)
 */
function ab() {
    return 1;
}

Fixed.

class Model
{
    /**
     * @var integer The id of the model.
     * @SuppressWarnings(PHPMD.ShortVariable)
     */
    public $id;
}

The rule ShortVariable doesn't have the @SuppressWarnings proper support. And LongVariable has the same bug: #493. Those 2 rules are similar, so let track this issue in #493.

And the previous comment https://github.com/phpmd/phpmd/issues/337#issuecomment-439609957 It was discussed here: #441

I confirm it's still happening, so we should rather re-open #441

kylekatarnls avatar Oct 19 '19 16:10 kylekatarnls