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

Squiz.Classes.SelfMemberReference: false negative if file contains namespace operator #553

Closed
4 tasks done
jrfnl opened this issue Jul 15, 2024 · 0 comments · Fixed by #587
Closed
4 tasks done

Squiz.Classes.SelfMemberReference: false negative if file contains namespace operator #553

jrfnl opened this issue Jul 15, 2024 · 0 comments · Fixed by #587

Comments

@jrfnl
Copy link
Member

jrfnl commented Jul 15, 2024

Describe the bug

The namespace keyword can both be used for namespace declarations as well as as an operator - "magic keyword" - to resolve to the current namespace.
See: https://www.php.net/manual/en/language.namespaces.nsconstants.php#example-298

This last case is not correctly taken into account when determining the current namespace, which leads to false negatives.

Code sample

<?php

namespace TestMe;

//namespace\functionCall();

class SelfMemberReference
{
    public function falseNegative()
    {
        $testResults[] = \TestMe\SelfMemberReference::test();
    }
}

To reproduce

Steps to reproduce the behavior:

  1. Create a file called test.php with the code sample above...
  2. Run phpcs -ps ./test.php --standard=squiz --sniffs=squiz.classes.selfmemberreference
  3. See error message displayed
------------------------------------------------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
------------------------------------------------------------------------------------------------------------------
 11 | ERROR | [x] Must use "self::" for local static member reference (Squiz.Classes.SelfMemberReference.NotUsed)
------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
------------------------------------------------------------------------------------------------------------------
  1. Now uncomment line 5 containing the namespace keyword used as an operator.
  2. Run phpcs -ps ./test.php --standard=squiz --sniffs=squiz.classes.selfmemberreference again.
  3. See no error displayed <= this is the false negative.

Expected behavior

That namespace keywords used as an operator are ignored when determining the applicable namespace name.

Versions (please complete the following information)

Operating System not relevant (Windows 10)
PHP version not relevant (8.3.8)
PHP_CodeSniffer version 3.10.1 / master as of today
Standard Squiz
Install type not relevant (git clone)

Please confirm

  • I have searched the issue list and am not opening a duplicate issue.
  • I have read the Contribution Guidelines and this is not a support question.
  • 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant