phpmd icon indicating copy to clipboard operation
phpmd copied to clipboard

False positive for UnusedPrivateMethod when method is used as callable

Open sreichel opened this issue 1 year ago • 3 comments

  • PHPMD version: latest
  • PHP Version: 7.4
  • Installation type: composer
  • Operating System / Distribution & Version: Linux

Example

Having

usort($history, [__CLASS__, '_sortHistoryByTimestamp']);

and

private static function _sortHistoryByTimestamp($a, $b)
{
    // ...
}

sreichel avatar Nov 28 '24 20:11 sreichel

Duplicates #253 but the old one get closed so let's keep this one open.

I wonder if we will support all the syntax (i.e. __CLASS__, self::class, static::class, get_class() etc.).

I see that the next version of your project will bump PHP version to 8.2.

Since PHP 8.1, it's possible to write:

usort($history, self::_sortHistoryByTimestamp(...));

And this should be supported by PHPMD if I'm not mistaken.

kylekatarnls avatar Nov 29 '24 09:11 kylekatarnls

I see that the next version of your project will bump PHP version to 8.2.

We will support 7.4 for the next time. ;)

I wonder if we will support all the syntax (i.e. CLASS, self::class, static::class, get_class() etc.).

Dont know if is_callable() works.

(May take a look at phpstan ... it does not report the method as unused)

sreichel avatar Nov 29 '24 09:11 sreichel

Yes yes, we know PHPMD has a bug, or rather that we never implemented any support for those syntax, we have the same issue with [$this, 'foo'] and is_callable is indeed a runtime way to know if something is callable, but that's not the end of it because: we need a static way and most importantly we need to know what is expecting a callable.

I.E.

usort([__CLASS__, 'a'], [__CLASS__, 'b']);
array_map([__CLASS__, 'c'], [__CLASS__, 'd']);

In this example: b() and c() are used, a() and d() are unused.

kylekatarnls avatar Nov 29 '24 10:11 kylekatarnls