Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

phpmd ignore SuppressWarnings(PHPMD.CountInLoopExpression) inside class #676

Closed
fernandoval opened this issue Oct 8, 2019 · 3 comments
Closed

Comments

@fernandoval
Copy link

  • PHPMD version: 2.7.0
  • PHP Version: 7.2.19
  • Installation type: composer
  • Operating System / Distribution & Version: Ubuntu 18.04.3

Current Behavior

Receiving "Avoid using count() function in while loops" warning message.

Expected Behavior

No error messages.

Steps To Reproduce:

If comment @SuppressWarnings(PHPMD.CountInLoopExpression) is in class comment block no warning message is printed.

But if the suppression instruction is in function comment block the warning message is printed.

@kylekatarnls
Copy link
Member

Hi @fernandoval, please dump the code so we can reproduce it.

Did you check the PHPDoc comment has the valid syntax with 2 stars: /** ... */?

@fernandoval
Copy link
Author

fernandoval commented Oct 8, 2019

Sample of code where comment works:

/**
 * @SuppressWarnings(PHPMD.CountInLoopExpression)
 */
class ArrayUtils
{
    public function dottedUnset(array &$array, string $key)
    {
        $keys = explode('.', $key);

        while (count($keys) > 1) {
            $key = array_shift($keys);

            if (!isset($array[$key]) || !is_array($array[$key])) {
                return;
            }

            $array = &$array[$key];
        }

        unset($array[array_shift($keys)]);
    }
}

Sample where comment does not works:

class ArrayUtils
{
    /**
     * Removes the element from an array using dot notation.
     *
     * @SuppressWarnings(PHPMD.CountInLoopExpression)
     *
     * @param array  $array
     * @param string $key
     *
     * @return void
     */
    public function dottedUnset(array &$array, string $key)
    {
        $keys = explode('.', $key);

        while (count($keys) > 1) {
            $key = array_shift($keys);

            if (!isset($array[$key]) || !is_array($array[$key])) {
                return;
            }

            $array = &$array[$key];
        }

        unset($array[array_shift($keys)]);
    }
}

@kylekatarnls
Copy link
Member

Bug reproduced. Fix #677 proposed.

kylekatarnls added a commit to kylekatarnls/phpmd that referenced this issue Oct 8, 2019
@tvbeek tvbeek closed this as completed in 815712b Oct 8, 2019
tvbeek added a commit that referenced this issue Oct 8, 2019
…-in-loop-method-suppress

Fix #676 Handle @SuppressWarnings on each method for CountInLoop
@ravage84 ravage84 added this to the 2.x (unspecific) milestone May 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants