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

PHPCS doesn't recongnize namespace\function() syntax. #3209

Closed
azjezz opened this issue Feb 7, 2021 · 3 comments
Closed

PHPCS doesn't recongnize namespace\function() syntax. #3209

azjezz opened this issue Feb 7, 2021 · 3 comments

Comments

@azjezz
Copy link

azjezz commented Feb 7, 2021

Describe the bug
A clear and concise description of what the bug is.

Code sample

<?php

namespace Foo;

$e = namespace\count([1, 2]);

Custom ruleset

<?xml version="1.0"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="PSL"
         xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd">
    <file>src</file>
    <file>tests</file>

    <arg name="basepath" value="."/>
    <arg name="colors"/>
    <arg name="parallel" value="75"/>

    <rule ref="PSR1">
        <type>error</type>
    </rule>

    <rule ref="PSR2">
        <type>error</type>
    </rule>

    <rule ref="PSR12">
        <type>error</type>
    </rule>

    <!-- Don't hide tokenizer exceptions -->
    <rule ref="Internal.Tokenizer.Exception">
        <type>error</type>
    </rule>

    <!-- Ban some functions -->
    <rule ref="Generic.PHP.ForbiddenFunctions">
        <properties>
            <property name="forbiddenFunctions" type="array">
                <element key="sizeof" value="count"/>
                <element key="delete" value="unset"/>
            </property>
        </properties>
    </rule>
</ruleset>

To reproduce
Steps to reproduce the behavior:

  1. Create a file called test.php with the code sample above...
  2. Run phpcs test.php ...
  3. See error message displayed
-------------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
-------------------------------------------------------------------------------
 40 | ERROR | [x] There must be one blank line after the namespace declaration
-------------------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
-------------------------------------------------------------------------------

Expected behavior
No issues.

Versions (please complete the following information):

  • OS: irrelevant
  • PHP: 7.4, 8.0
  • PHPCS: 3.5.8

Additional context

using namespace\count to call a function is different than using count() and \count().

  1. namespace\count(): use count() from the current namespace, don't try to fallback to global count
  2. count(): use count() from current namespace, if not found, fallback to global namespace
  3. \count(): use count() from global namespace

see: https://3v4l.org/enMgi

@jrfnl
Copy link
Contributor

jrfnl commented Feb 7, 2021

Would you mind testing with master ? I believe this has already been fixed via #3184.

@azjezz
Copy link
Author

azjezz commented Feb 7, 2021

It seems to work! thank you for you work 🎉

@azjezz azjezz closed this as completed Feb 7, 2021
@jrfnl
Copy link
Contributor

jrfnl commented Feb 7, 2021

Thanks for confirming!

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

No branches or pull requests

2 participants