phpmd / phpmd/phpmd

UnusedPrivateField false positive with Trait

Open
#602 0 comments 0 reactions 1 assignee View on GitHub

@kylekatarnls is already working on this.

Since May 21, 2020.

Dominant language
PHP
Stars
2.5k
Forks
361
Avg merge
1d 19h
Merged PRs (30d)
11

Description

Hi!
I have discovered that when I use a private variable only in a trait it will give a false positive unused private field warning:

Simplified example:

<?php
trait TestTrait {
   public function testIgnore(string $fieldname): bool
   {
      if (!in_array($fieldname, self::$ignoreFields, true)) {
         return true;
      }
      return false;
   }
}


final class Tester
{
    use TestTrait;
    private static $ignoreFields = ['one', 'two'];

}

This would give the warning: "Avoid unused private field such as '$ignoreFields'"

Using version 2.6.0
Regards
Alex

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.