Skip to content

[FR] Sniff to flag useless use of the namespace operator for resolution #424

@jrfnl

Description

@jrfnl

Is your feature request related to a problem?

When code is in a namespace and one wants to use a class/function/constant from the same namespace, one can use the unqualified name of the class/function/constant and as long as you have not imported something with that name, PHP will resolve things correctly.

However, it can be good practice, to make it very clear that one is using a namespaced function/constant by using the namespace\ prefix, especially for functions with names mirroring well-known global function names.

namespace Foo;

function bar() {}

// These both call `Foo\bar()`.
bar();
namespace\bar();

However, when the code is not namespaced, it never makes sense to use the namespace\ prefix as the code is already in the global namespace and the namespace\ prefix would just resolve to the global namespace.

function bar() {}

// These both call the global function `bar()`.
bar();
namespace\bar();

Describe the solution you'd like

This ticket is a feature request/suggestion to have a new sniff which forbids the use of the namespace\ prefix/namespace operator in code which is not namespaced.

See above for some code samples.

Additional context (optional)

See: https://www.php.net/manual/en/language.namespaces.nsconstants.php

  • I intend to create a pull request to implement this feature.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions