Sniff checks whether use
declarations at the top of a file are alphabetically sorted. Follows natural sorting and takes edge cases with special symbols into consideration. The following code snippet is an example of correctly sorted uses:
use LogableTrait;
use LogAware;
use LogFactory;
use LoggerInterface;
use LogLevel;
use LogStandard;
Sniff provides the following settings:
psr12Compatible
(defaults totrue
): sets the required order toclasses
,functions
andconstants
.false
sets the required order toclasses
,constants
andfunctions
.caseSensitive
: compare namespaces case sensitively, which makes this order correct:
use LogAware;
use LogFactory;
use LogLevel;
use LogStandard;
use LogableTrait;
use LoggerInterface;
Group use declarations are ugly, make diffs ugly and this sniff prohibits them.
This sniff reduces confusion in the following code snippet:
try {
$this->foo();
} catch (Exception $e) {
// Is this the general exception all exceptions must extend from? Or Exception from the current namespace?
}
All references to types named Exception
or ending with Exception
must be referenced via a fully qualified name:
try {
$this->foo();
} catch (\FooCurrentNamespace\Exception $e) {
} catch (\Exception $e) {
}
Sniff provides the following settings:
- Exceptions with different names can be configured in
specialExceptionNames
property. - If your codebase uses classes that look like exceptions (because they have
Exception
orError
suffixes) but aren't, you can add them toignoredNames
property and the sniff won't enforce them to be fully qualified. Classes withError
suffix have to be added to ignored only if they are in the root namespace (likeLibXMLError
).
All references to global constants must be referenced via a fully qualified name.
Sniff provides the following settings:
include
: list of global constants that must be referenced via FQN. If not set all constants are considered.exclude
: list of global constants that are allowed not to be referenced via FQN.
All references to global functions must be referenced via a fully qualified name.
Sniff provides the following settings:
include
: list of global functions that must be referenced via FQN. If not set all functions are considered.includeSpecialFunctions
: include complete list of PHP internal functions that could be optimized when referenced via FQN.exclude
: list of global functions that are allowed not to be referenced via FQN.
Enforces fully qualified names of classes and interfaces in phpDocs - in annotations. This results in unambiguous phpDocs.
Sniff provides the following settings:
ignoredAnnotationNames
: case-sensitive list of annotation names that the sniff should ignore. Useful for custom annotation names like@apiParam
Prohibits multiple uses separated by commas:
use Foo, Bar;
Enforces one space after namespace
, disallows content between namespace name and semicolon and disallows use of bracketed syntax.
Enforces configurable number of lines before and after namespace
.
Sniff provides the following settings:
linesCountBeforeNamespace
: allows to configure the number of lines beforenamespace
.linesCountAfterNamespace
: allows to configure the number of lines afternamespace
.
Requires only one namespace in a file.
Sniff provides the following settings:
searchAnnotations
(defaults tofalse
): enables searching for mentions in annotations.namespacesRequiredToUse
: if not set, all namespaces are required to be used. When set, only mentioned namespaces are required to be used. Useful in tandem with UseOnlyWhitelistedNamespaces sniff.allowFullyQualifiedExceptions
,specialExceptionNames
&ignoredNames
: allows fully qualified exceptions. Useful in tandem with FullyQualifiedExceptions sniff.allowFullyQualifiedNameForCollidingClasses
: allow fully qualified name for a class with a colliding use statement.allowFullyQualifiedNameForCollidingFunctions
: allow fully qualified name for a function with a colliding use statement.allowFullyQualifiedNameForCollidingConstants
: allow fully qualified name for a constant with a colliding use statement.allowFullyQualifiedGlobalClasses
: allows using fully qualified classes from global space (i.e.\DateTimeImmutable
).allowFullyQualifiedGlobalFunctions
: allows using fully qualified functions from global space (i.e.\phpversion()
).allowFullyQualifiedGlobalConstants
: allows using fully qualified constants from global space (i.e.\PHP_VERSION
).allowFallbackGlobalFunctions
: allows using global functions via fallback name withoutuse
(i.e.phpversion()
).allowFallbackGlobalConstants
: allows using global constants via fallback name withoutuse
(i.e.PHP_VERSION
).allowPartialUses
: allows using and referencing whole namespaces.
Disallows leading backslash in use statement:
use \Foo\Bar;
Sniff prohibits uses from the same namespace:
namespace Foo;
use Foo\Bar;
Enforces configurable number of lines before first use
, after last use
and between two different types of use
(eg. between use function
and use const
). Also enforces zero number of lines between same types of use
.
Sniff provides the following settings:
linesCountBeforeFirstUse
: allows to configure the number of lines before firstuse
.linesCountBetweenUseTypes
: allows to configure the number of lines between two different types ofuse
.linesCountAfterLastUse
: allows to configure the number of lines after lastuse
.
Sniff disallows uses of other than configured namespaces.
Sniff provides the following settings:
namespacesRequiredToUse
: namespaces in this array are the only ones allowed to be used. E.g. root project namespace.allowUseFromRootNamespace
: also allow using top-level namespace:
use DateTimeImmutable;
Looks for use
alias that is same as unqualified name.
Looks for unused imports from other namespaces.
Sniff provides the following settings:
searchAnnotations
(defaults tofalse
): enables searching for class names in annotations.ignoredAnnotationNames
: case-sensitive list of annotation names that the sniff should ignore (only the name is ignored, annotation content is still searched). Useful for name collisions like@testCase
annotation andTestCase
class.ignoredAnnotations
: case-sensitive list of annotation names that the sniff ignore completely (both name and content are ignored). Useful for name collisions like@group Cache
annotation andCache
class.