slevomat / coding-standard

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

Named arg + Constant = "Type `Constant` is not used in this file" (phpcs v3.10.0) #1680

Closed LastDragon-ru closed 1 month ago

LastDragon-ru commented 1 month ago

Describe the bug

Since phpcs v3.10.0 the code generate false positive errors about unused constants. Seems it is caused by named arg (no error without it).

-------------------------------------------------------------------------
FOUND 2 ERRORS AFFECTING 2 LINES
-------------------------------------------------------------------------
 7 | ERROR | [x] Type PREG_SPLIT_DELIM_CAPTURE is not used in this file.
 8 | ERROR | [x] Type PREG_SPLIT_NO_EMPTY is not used in this file.
-------------------------------------------------------------------------
PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
-------------------------------------------------------------------------

Code sample

<?php declare(strict_types = 1);

namespace Bug;

use function preg_split;

use const PREG_SPLIT_DELIM_CAPTURE;
use const PREG_SPLIT_NO_EMPTY;

preg_split('/((.)\g{-1}*)/um', 'abc', flags: PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);

Custom ruleset

<?xml version="1.0"?>
<ruleset name="LaraAspStandard"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="./vendor-bin/phpcs/vendor/squizlabs/php_codesniffer/phpcs.xsd">
    <config name="installed_paths" value="../../slevomat/coding-standard"/>
    <rule ref="SlevomatCodingStandard.Namespaces.UnusedUses">
        <properties>
            <property name="searchAnnotations" value="true"/>
            <property name="ignoredAnnotationNames" type="array">
                <element value="@dataProvider"/>
            </property>
        </properties>
    </rule>
</ruleset>

Expected behavior

No errors.

kukulich commented 1 month ago

I think you should try to report it to PHPCS first.

LastDragon-ru commented 1 month ago

Seems releated to phpcs :)