-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathReturnValueCheckSniff.php
165 lines (150 loc) · 4.23 KB
/
ReturnValueCheckSniff.php
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento2\Sniffs\PHP;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;
/**
* Detects misusing of IS_IDENTICAL operators.
*/
class ReturnValueCheckSniff implements Sniff
{
/**
* String representation of error.
*
* @var string
*/
protected $errorMessage = 'Identical operator === is not used for testing the return value of %s function';
/**
* Error violation code.
*
* @var string
*/
protected $errorCode = 'ImproperValueTesting';
/**
* Searched functions.
*
* @var array
*/
protected $functions = [
'strpos',
'stripos',
'array_search'
];
/**
* All tokens from current file.
*
* @var array
*/
protected $tokens = [];
/**
* PHP_CodeSniffer file.
*
* @var File
*/
protected $file;
/**
* Left limit for search of identical operators.
*
* @var int
*/
protected $leftLimit;
/**
* Right limit for search of identical operators.
*
* @var int
*/
protected $rightLimit;
/**
* List of tokens which declares left bound of current scope.
*
* @var array
*/
protected $leftRangeTokens = [
T_IS_IDENTICAL,
T_IS_NOT_IDENTICAL,
T_OPEN_PARENTHESIS,
T_BOOLEAN_AND,
T_BOOLEAN_OR,
];
/**
* List of tokens which declares right bound of current scope.
*
* @var array
*/
protected $rightRangeTokens = [
T_IS_IDENTICAL,
T_IS_NOT_IDENTICAL,
T_CLOSE_PARENTHESIS,
T_BOOLEAN_AND,
T_BOOLEAN_OR,
];
/**
* List of tokens which declares identical operators.
*
* @var array
*/
protected $identical = [
T_IS_IDENTICAL,
T_IS_NOT_IDENTICAL,
];
/**
* @inheritdoc
*/
public function register()
{
return [T_IF, T_ELSEIF];
}
/**
* @inheritdoc
*/
public function process(File $phpcsFile, $stackPtr)
{
$this->tokens = $phpcsFile->getTokens();
$this->file = $phpcsFile;
$this->leftLimit = $open = $this->tokens[$stackPtr]['parenthesis_opener'];
$this->rightLimit = $close = $this->tokens[$stackPtr]['parenthesis_closer'];
for ($i = ($open + 1); $i < $close; $i++) {
if (($this->tokens[$i]['code'] === T_STRING && in_array($this->tokens[$i]['content'], $this->functions))
&& (!$this->findIdentical($i - 1, $this->findFunctionParenthesisCloser($i) + 1))
) {
$foundFunctionName = $this->tokens[$i]['content'];
$phpcsFile->addError($this->errorMessage, $i, $this->errorCode, [$foundFunctionName]);
}
}
}
/**
* Recursively finds identical operators in current scope.
*
* @param int $leftCurrentPosition
* @param int $rightCurrentPosition
* @return bool
*/
protected function findIdentical($leftCurrentPosition, $rightCurrentPosition)
{
$leftBound = $this->file->findPrevious($this->leftRangeTokens, $leftCurrentPosition, $this->leftLimit - 1);
$rightBound = $this->file->findNext($this->rightRangeTokens, $rightCurrentPosition, $this->rightLimit + 1);
$leftToken = $this->tokens[$leftBound];
$rightToken = $this->tokens[$rightBound];
if ($leftToken['code'] === T_OPEN_PARENTHESIS && $rightToken['code'] === T_CLOSE_PARENTHESIS) {
return $this->findIdentical($leftBound - 1, $rightBound + 1);
} else {
return (
in_array($leftToken['code'], $this->identical) || in_array($rightToken['code'], $this->identical)
) ?: false;
}
}
/**
* Finds the position of close parenthesis of detected function.
*
* @param int $currentPosition
* @return mixed
*/
protected function findFunctionParenthesisCloser($currentPosition)
{
$nextOpenParenthesis = $this->file->findNext(T_OPEN_PARENTHESIS, $currentPosition, $this->rightLimit);
return $nextOpenParenthesis ? $this->tokens[$nextOpenParenthesis]['parenthesis_closer'] : false;
}
}