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

Generic/UpperCaseConstantName: false positives for namespaced function called define() #733

Open
4 tasks done
jrfnl opened this issue Nov 26, 2024 · 0 comments
Open
4 tasks done

Comments

@jrfnl
Copy link
Member

jrfnl commented Nov 26, 2024

Describe the bug

Inspired by #665

When a codebase contains a namespaced function called define, the sniff may throw false positives.

Code sample

File 1:

namespace Foo;

function define($name, $value) {
    // Do something.
}

// These are calls to the namespaced function, not the global one.
define('name', 'value');
namespace\define('name', 'value');

// This is a call to another namespaced function `Foo\Sub\define()`, not the global one.
Sub\define('name', 'value');

// This is a call to yet another namespaced function `My\Other\NS\define()`, not the global one.
\My\Other\NS\define('name', 'value');

File 2:

namespace Foo;

use function Bar\define;

// This is a call to the imported namespaced function, not the global one.
define('name', 'value');

To reproduce

Steps to reproduce the behavior:

  1. Create a file called test.php with the code sample above...
  2. Run phpcs test.php --standard=generic --sniffs=generic.namingconventions.uppercaseconstantname
  3. See error message displayed
# File 1:
---------------------------------------------------------------------------------------------------------------------------------------------------
FOUND 4 ERRORS AFFECTING 4 LINES
---------------------------------------------------------------------------------------------------------------------------------------------------
 10 | ERROR | Constants must be uppercase; expected 'NAME' but found 'name' (Generic.NamingConventions.UpperCaseConstantName.ConstantNotUpperCase)
 11 | ERROR | Constants must be uppercase; expected 'NAME' but found 'name' (Generic.NamingConventions.UpperCaseConstantName.ConstantNotUpperCase)
 14 | ERROR | Constants must be uppercase; expected 'NAME' but found 'name' (Generic.NamingConventions.UpperCaseConstantName.ConstantNotUpperCase)
 17 | ERROR | Constants must be uppercase; expected 'NAME' but found 'name' (Generic.NamingConventions.UpperCaseConstantName.ConstantNotUpperCase)
---------------------------------------------------------------------------------------------------------------------------------------------------

# File 2:
--------------------------------------------------------------------------------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
--------------------------------------------------------------------------------------------------------------------------------------------------
 8 | ERROR | Constants must be uppercase; expected 'NAME' but found 'name' (Generic.NamingConventions.UpperCaseConstantName.ConstantNotUpperCase)
--------------------------------------------------------------------------------------------------------------------------------------------------

Expected behavior

No errors.

Versions (please complete the following information)

Operating System not relevant
PHP version not relevant
PHP_CodeSniffer version master
Standard Generic
Install type not relevant

Additional context

Add any other context about the problem here.

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
Development

No branches or pull requests

1 participant