AlphabeticExtendsImplementsSniff
in package
implements
Sniff
Verifies that the interface names used in a class/enum "implements" statement or an interface "extends" statement, are listed in alphabetic order.
Tags
Table of Contents
Interfaces
Constants
- METRIC_NAME_ALPHA = 'Interface names in implements/extends ordered alphabetically (%s)'
- Name of the "Alphabetically ordered" metric.
- METRIC_NAME_COUNT = 'Number of interfaces being implemented/extended'
- Name of the "interface count" metric.
Properties
- $orderby : string
- The sort order to use for the statement.
Methods
- process() : void
- Processes this test, when one of its tokens is encountered.
- register() : array<string|int, int|string>
- Returns an array of tokens this test wants to listen for.
- sortByFull() : array<string|int, string>
- Sort an array of potentially mixed qualified and unqualified names by the full name.
- sortByName() : array<string|int, string>
- Sort an array of potentially mixed qualified and unqualified names by the interface name.
- sortNames() : array<string|int, string>
- Sort an array of names.
Constants
METRIC_NAME_ALPHA
Name of the "Alphabetically ordered" metric.
public
string
METRIC_NAME_ALPHA
= 'Interface names in implements/extends ordered alphabetically (%s)'
Tags
METRIC_NAME_COUNT
Name of the "interface count" metric.
public
string
METRIC_NAME_COUNT
= 'Number of interfaces being implemented/extended'
Tags
Properties
$orderby
The sort order to use for the statement.
public
string
$orderby
= 'name'
If all names used are unqualified, the sort order won't make a difference. However, if one or more of the names are partially or fully qualified, the chosen sort order will determine how the sorting between unqualified, partially and fully qualified names is handled.
The sniff supports two sort order options:
- 'name' : sort by the interface name only (default);
- 'full' : sort by the full name as used in the statement (without leading backslash).
In both cases, the sorting will be done using natural sort, case-insensitive.
Example:
class Foo implements \Vendor\DiffIterator, My\Count, DateTimeInterface }
If sorted using the "name" sort-order, the sniff looks just at the interface name, i.e.
DiffIterator, Count and DateTimeInterface, which for this example would mean
the correct order would be My\Count, DateTimeInterface, \Vendor\DiffIterator.
If sorted using the "full" sort-order, the sniff will look at the full name as used
in the implements statement, without leading backslashes.
For the example above, this would mean that the correct order would be:
DateTimeInterface, My\Count, \Vendor\DiffIterator.
Tags
Methods
process()
Processes this test, when one of its tokens is encountered.
public
process(File $phpcsFile, int $stackPtr) : void
Parameters
- $phpcsFile : File
-
The file being scanned.
- $stackPtr : int
-
The position of the current token in the stack passed in $tokens.
Tags
register()
Returns an array of tokens this test wants to listen for.
public
register() : array<string|int, int|string>
Tags
Return values
array<string|int, int|string>sortByFull()
Sort an array of potentially mixed qualified and unqualified names by the full name.
protected
sortByFull(array<string|int, string> $names) : array<string|int, string>
Parameters
- $names : array<string|int, string>
-
Interface names, potentially mixed qualified and unqualified.
Tags
Return values
array<string|int, string>sortByName()
Sort an array of potentially mixed qualified and unqualified names by the interface name.
protected
sortByName(array<string|int, string> $names) : array<string|int, string>
Parameters
- $names : array<string|int, string>
-
Interface names, potentially mixed qualified and unqualified.
Tags
Return values
array<string|int, string>sortNames()
Sort an array of names.
private
sortNames(array<string|int, string> $names, callable $prepareNames) : array<string|int, string>
Parameters
- $names : array<string|int, string>
-
Interface names, potentially mixed qualified and unqualified.
- $prepareNames : callable
-
Function to call to prepare the names before sorting.