Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
LastDragon-ru opened this issue May 21, 2024 · 6 comments · Fixed by #507
Closed

Comments

@LastDragon-ru
Copy link

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 🤷‍♂️

slevomat/coding-standard#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
Copy link
Member

jrfnl commented May 21, 2024

@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
Copy link
Member

jrfnl commented May 21, 2024

@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
Copy link

It fixes the problem, thanks

@jrfnl
Copy link
Member

jrfnl commented May 21, 2024

Thanks for testing @momala454!

@LastDragon-ru
Copy link
Author

Yep, no error anymore

@jrfnl
Copy link
Member

jrfnl commented May 22, 2024

Thanks @LastDragon-ru for testing and confirming!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants