slevomat / coding-standard

Slevomat Coding Standard for PHP_CodeSniffer provides many useful sniffs
MIT License
1.37k stars 170 forks source link

`SlevomatCodingStandard.Namespaces.UnusedUses` cannot handle properties with (nullable) intersection types #1639

Open kapersoft opened 7 months ago

kapersoft commented 7 months ago

SlevomatCodingStandard.Namespaces.UnusedUses thinks the imports are unused when property contains (nullable) intersection types.

This code:

<?php

namespace App;

use Symfony\Component\Cache\Adapter\AdapterInterface;
use Symfony\Contracts\Cache\CacheInterface;

class Example
{
    public (AdapterInterface&CacheInterface)|null $cache = null;
}

Results in the following output:

FILE: example.php
-------------------------------------------------------------------------------------------------
FOUND 2 ERRORS AFFECTING 2 LINES
-------------------------------------------------------------------------------------------------
 5 | ERROR | [x] Type Symfony\Component\Cache\Adapter\AdapterInterface is not used in this file.
 6 | ERROR | [x] Type Symfony\Contracts\Cache\CacheInterface is not used in this file.
-------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
-------------------------------------------------------------------------------------------------

Time: 16ms; Memory: 8MB

Rule config:

<rule ref="SlevomatCodingStandard.Namespaces.UnusedUses">
    <properties>
        <property name="searchAnnotations" value="true"/>
    </properties>
</rule>

I am using squizlabs/php_codesniffer@3.7.2 and slevomat/coding-standard@8.14.1

Does any have any ideas how to fix this?

kukulich commented 7 months ago

I think PHPCS does not support https://wiki.php.net/rfc/dnf_types