-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathElseKeywordSniff.php
More file actions
47 lines (41 loc) · 1.16 KB
/
Copy pathElseKeywordSniff.php
File metadata and controls
47 lines (41 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
/**
* Sniff to detect usage of else keywords in if statements
*/
namespace HWPStandard\Sniffs\ControlStructures;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
class ElseKeywordSniff implements Sniff
{
/**
* Returns the token types that this sniff is interested in.
*
* @return array
*/
public function register()
{
return [T_ELSE, T_ELSEIF];
}
/**
* Processes this sniff when one of its tokens is encountered.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in the stack.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$token = $tokens[$stackPtr];
if ($token['code'] === T_ELSE) {
$warning = 'Usage of "else" detected; consider refactoring to avoid else branches';
$phpcsFile->addWarning($warning, $stackPtr, 'ElseDetected');
return;
}
if ($token['code'] === T_ELSEIF) {
$warning = 'Usage of "elseif" detected; consider refactoring to avoid else branches';
$phpcsFile->addWarning($warning, $stackPtr, 'ElseIfDetected');
}
}
}