|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Codor\Sniffs\Files; |
|
4
|
|
|
|
|
5
|
|
|
use PHP_CodeSniffer_Sniff; |
|
6
|
|
|
use PHP_CodeSniffer_File; |
|
7
|
|
|
|
|
8
|
|
|
class FunctionNameContainsAndOrSniff implements PHP_CodeSniffer_Sniff |
|
9
|
|
|
{ |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* The forbidden strings this sniff looks for. |
|
13
|
|
|
* @var array |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $keywords = ['And', '_and', 'Or', '_or']; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Returns the token types that this sniff is interested in. |
|
19
|
|
|
* @return array |
|
20
|
|
|
*/ |
|
21
|
|
|
public function register() |
|
22
|
|
|
{ |
|
23
|
|
|
return [T_FUNCTION]; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Processes the tokens that this sniff is interested in. |
|
28
|
|
|
* |
|
29
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file where the token was found. |
|
30
|
|
|
* @param integer $stackPtr The position in the stack where |
|
31
|
|
|
* the token was found. |
|
32
|
|
|
* @return void |
|
33
|
|
|
*/ |
|
34
|
|
|
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
35
|
|
|
{ |
|
36
|
|
|
$tokens = $phpcsFile->getTokens(); |
|
37
|
|
|
$functionNameToken = $tokens[$stackPtr + 2]; |
|
38
|
|
|
|
|
39
|
|
|
if (! $this->containsKeywords($functionNameToken)) { |
|
40
|
|
|
return; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$phpcsFile->addError($this->getErrorMessage(), $stackPtr); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
protected function containsKeywords($functionNameToken) |
|
47
|
|
|
{ |
|
48
|
|
|
$functionName = $functionNameToken['content']; |
|
49
|
|
|
foreach ($this->keywords as $keyword) { |
|
50
|
|
|
if ($this->contains($keyword, $functionName)) { |
|
51
|
|
|
return true; |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return false; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
protected function contains($needle, $haystack) |
|
59
|
|
|
{ |
|
60
|
|
|
return strpos($haystack, $needle) !== false; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
protected function getErrorMessage() |
|
64
|
|
|
{ |
|
65
|
|
|
return "Your function contains 'and' or 'or' which indicates it might be doing more than one thing."; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
|