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

False positive on Generic.PHP.ForbiddenFunctions.FoundWithAlternative #439

Open
3 tasks done
p4veI opened this issue Apr 10, 2024 · 1 comment
Open
3 tasks done

Comments

@p4veI
Copy link

p4veI commented Apr 10, 2024

Describe the bug

This sniff incorrectly reports an error while using in my case \Psl\Str\join function and reports forbidden use of the native PHP function

Code sample

src/a.php

<?php

use function Psl\Str\join;

class A {
  public function get(): string
  {
    $a = ['bar', 'baz', 'foo'];
    
    return join($a, ',');
  }
}

phpcs.xml

<?xml version="1.0"?>
<ruleset
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd"
>
    <arg name="basepath" value="."/>
    <arg name="cache" value=".phpcs-cache"/>

    <rule ref="Generic.PHP.ForbiddenFunctions">
        <properties>
            <property name="forbiddenFunctions" type="array">
                <element key="join" value="implode"/>
            </property>
        </properties>
    </rule>

    <file>src</file>
</ruleset>

Expected behavior

No errors

Versions (please complete the following information)

PHP version 8.3
PHP_CodeSniffer version 3.9 & master
Standard Generic

Please confirm:

  • I have searched the issue list and am not opening a duplicate issue.
  • I confirm that this bug is a bug in PHP_CodeSniffer and not in one of the external standards.
  • I have verified the issue still exists in the master branch of PHP_CodeSniffer.
@DannyvdSluijs
Copy link
Contributor

I can confirm this is reproducible with the current master branch (commit dba1cc1)

The output when running on the file using the config is:

FILE: reproductions/439.php
-----------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
-----------------------------------------------------------------------------
 10 | ERROR | The use of function join() is forbidden; use implode() instead
-----------------------------------------------------------------------------

Time: 47ms; Memory: 4MB

Debugging the violating token shows the information as shown below. Which doesn't seem to contain any information about the function namespace. Perhaps that is where the issue lies as adjusting the code the use the full qualified function name resolves the false positive. Should we perhaps look at the uses/imports and see if there is one that matches the function name. Also one last remark is that when using \join() (the native PHP versie with the root namespace of slash) it is correctly detected.

array(8) {
  'code' =>
  int(262)
  'type' =>
  string(8) "T_STRING"
  'content' =>
  string(4) "join"
  'line' =>
  int(10)
  'column' =>
  int(16)
  'length' =>
  int(4)
  'level' =>
  int(2)
  'conditions' =>
  array(2) {
    [14] =>
    int(333)
    [23] =>
    int(310)
  }
}

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

No branches or pull requests

3 participants