PHPCSStandards / PHP_CodeSniffer

PHP_CodeSniffer tokenizes PHP files and detects violations of a defined set of coding standards.
BSD 3-Clause "New" or "Revised" License
805 stars 47 forks source link

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

Closed LastDragon-ru closed 1 month ago

LastDragon-ru commented 1 month ago

I think you should try to report it to PHPCS first. (c) @kukulich

Sorry, I'm not sure this is related to phpcs or slevomat, so I've posted it in both repos 🤷‍♂️

https://github.com/slevomat/coding-standard/issues/1680

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.

jrfnl commented 1 month ago

@LastDragon-ru Thank you for reporting this. Definitely a bug in the 3.10.0 release and definitely related to named parameters. Unfortunate that nobody caught this during the testing period.

Minimal code sample it can be reproduced with:

preg_split($r, $h, flags: PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);

Symptom: close parenthesis of the function call gets incorrectly tokenized as T_TYPE_CLOSE_PARENTHESIS. Also note: it's only the close parenthesis, the open parenthesis is not affected.

  2 | L3 | C  1 | CC 0 | ( 0) | T_STRING                   | [ 10]: preg_split
  3 | L3 | C 11 | CC 0 | ( 0) | T_OPEN_PARENTHESIS         | [  1]: (
  4 | L3 | C 12 | CC 0 | ( 1) | T_VARIABLE                 | [  2]: $r
  5 | L3 | C 14 | CC 0 | ( 1) | T_COMMA                    | [  1]: ,
  6 | L3 | C 15 | CC 0 | ( 1) | T_WHITESPACE               | [  1]: ⸱
  7 | L3 | C 16 | CC 0 | ( 1) | T_VARIABLE                 | [  2]: $h
  8 | L3 | C 18 | CC 0 | ( 1) | T_COMMA                    | [  1]: ,
  9 | L3 | C 19 | CC 0 | ( 1) | T_WHITESPACE               | [  1]: ⸱
 10 | L3 | C 20 | CC 0 | ( 1) | T_PARAM_NAME               | [  5]: flags
 11 | L3 | C 25 | CC 0 | ( 1) | T_COLON                    | [  1]: :
 12 | L3 | C 26 | CC 0 | ( 1) | T_WHITESPACE               | [  1]: ⸱
 13 | L3 | C 27 | CC 0 | ( 1) | T_STRING                   | [ 19]: PREG_SPLIT_NO_EMPTY
 14 | L3 | C 46 | CC 0 | ( 1) | T_WHITESPACE               | [  1]: ⸱
 15 | L3 | C 47 | CC 0 | ( 1) | T_TYPE_UNION               | [  1]: |
 16 | L3 | C 48 | CC 0 | ( 1) | T_WHITESPACE               | [  1]: ⸱
 17 | L3 | C 49 | CC 0 | ( 1) | T_STRING                   | [ 24]: PREG_SPLIT_DELIM_CAPTURE
 18 | L3 | C 73 | CC 0 | ( 0) | T_TYPE_CLOSE_PARENTHESIS   | [  1]: )
 19 | L3 | C 74 | CC 0 | ( 0) | T_SEMICOLON                | [  1]: ;

I'll have a look.

jrfnl commented 1 month ago

@LastDragon-ru @momala454 PR #507 should fix this. I would appreciate it if you could test the fix.

If you need it, you can download the PHAR files containing the patch here: https://github.com/PHPCSStandards/PHP_CodeSniffer/actions/runs/9173810442

momala454 commented 1 month ago

It fixes the problem, thanks

jrfnl commented 1 month ago

Thanks for testing @momala454!

LastDragon-ru commented 1 month ago

Yep, no error anymore

jrfnl commented 1 month ago

Thanks @LastDragon-ru for testing and confirming!