PHP_CodeSniffer_Standards_AbstractScopeSniff
in package
implements
PHP_CodeSniffer_Sniff
An AbstractScopeTest allows for tests that extend from this class to listen for tokens within a particular scope.
Below is a test that listens to methods that exist only within classes:
class ClassScopeTest extends PHP_CodeSniffer_Standards_AbstractScopeSniff
{
public function __construct()
{
parent::__construct(array(T_CLASS), array(T_FUNCTION));
}
protected function processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, $)
{
$className = $phpcsFile->getDeclarationName($currScope);
echo 'encountered a method within class '.$className;
}
}
Tags
Interfaces, Classes and Traits
- PHP_CodeSniffer_Sniff
- Represents a PHP_CodeSniffer sniff for sniffing coding standards.
Table of Contents
- $_listenOutside : bool
- True if this test should fire on tokens outside of the scope.
- $_scopeTokens : string
- The type of scope opener tokens that this test wishes to listen to.
- $_tokens : array<string|int, mixed>
- The token types that this test wishes to listen to within the scope.
- __construct() : mixed
- Constructs a new AbstractScopeTest.
- process() : void
- Processes the tokens that this test is listening for.
- register() : array<string|int, int>
- The method that is called to register the tokens this test wishes to listen to.
- processTokenOutsideScope() : void
- Processes a token that is found outside the scope that this test is listening to.
- processTokenWithinScope() : void
- Processes a token that is found within the scope that this test is listening to.
Properties
$_listenOutside
True if this test should fire on tokens outside of the scope.
private
bool
$_listenOutside
= alse
$_scopeTokens
The type of scope opener tokens that this test wishes to listen to.
private
string
$_scopeTokens
= array()
$_tokens
The token types that this test wishes to listen to within the scope.
private
array<string|int, mixed>
$_tokens
= array()
Methods
__construct()
Constructs a new AbstractScopeTest.
public
__construct(array<string|int, mixed> $scopeTokens, array<string|int, mixed> $tokens[, bool $listenOutside = false ]) : mixed
Parameters
- $scopeTokens : array<string|int, mixed>
-
The type of scope the test wishes to listen to.
- $tokens : array<string|int, mixed>
-
The tokens that the test wishes to listen to within the scope.
- $listenOutside : bool = false
-
If true this test will also alert the extending class when a token is found outside the scope, by calling the processTokenOutsideScope method.
Tags
Return values
mixed —process()
Processes the tokens that this test is listening for.
public
final process(PHP_CodeSniffer_File $phpcsFile, int $stackPtr) : void
Parameters
- $phpcsFile : PHP_CodeSniffer_File
-
The file where this token was found.
- $stackPtr : int
-
The position in the stack where this token was found.
Tags
Return values
void —register()
The method that is called to register the tokens this test wishes to listen to.
public
final register() : array<string|int, int>
DO NOT OVERRIDE THIS METHOD. Use the constructor of this class to register for the desired tokens and scope.
Tags
Return values
array<string|int, int> —processTokenOutsideScope()
Processes a token that is found outside the scope that this test is listening to.
protected
processTokenOutsideScope(PHP_CodeSniffer_File $phpcsFile, int $stackPtr) : void
Parameters
- $phpcsFile : PHP_CodeSniffer_File
-
The file where this token was found.
- $stackPtr : int
-
The position in the stack where this token was found.
Return values
void —processTokenWithinScope()
Processes a token that is found within the scope that this test is listening to.
protected
abstract processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, int $stackPtr, int $currScope) : void
Parameters
- $phpcsFile : PHP_CodeSniffer_File
-
The file where this token was found.
- $stackPtr : int
-
The position in the stack where this token was found.
- $currScope : int
-
The position in the tokens array that opened the scope that this test is listening for.