-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathChangedIntToBoolParamTypeSniff.php
123 lines (110 loc) · 3.68 KB
/
ChangedIntToBoolParamTypeSniff.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
<?php
/**
* PHPCompatibility, an external standard for PHP_CodeSniffer.
*
* @package PHPCompatibility
* @copyright 2012-2020 PHPCompatibility Contributors
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3
* @link https://github.com/PHPCompatibility/PHPCompatibility
*/
namespace Magento2\Sniffs\PHPCompatibility;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Util\Tokens;
use PHPCompatibility\AbstractFunctionCallParameterSniff;
use PHPCSUtils\BackCompat\BCTokens;
/**
* Detect calls to functions where the expected type of a parameter has been changed from integer to boolean.
*
* Throws an error when a hard-coded numeric value is passed.
*
* PHP version 8.0+
*
* @since 10.0.0
*/
class ChangedIntToBoolParamTypeSniff extends AbstractFunctionCallParameterSniff
{
/**
* Functions to check for.
*
* @since 10.0.0
*
* @var array
*/
protected $targetFunctions = [
'ob_implicit_flush' => [
1 => [
'name' => 'flag',
'since' => '8.0',
],
],
'sem_get' => [
4 => [
'name' => 'auto_release',
'since' => '8.0',
],
],
];
/**
* Do a version check to determine if this sniff needs to run at all.
*
* Checks against the first PHP version listed in the above array.
*
* @since 10.0.0
*
* @return bool
*/
protected function bowOutEarly()
{
return ($this->supportsAbove('8.0') === false);
}
/**
* Process the parameters of a matched function.
*
* @since 10.0.0
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in the stack.
* @param string $functionName The token content (function name) which was matched.
* @param array $parameters Array with information about the parameters.
*
* @return int|void Integer stack pointer to skip forward or void to continue
* normal file processing.
*/
public function processParameters(File $phpcsFile, $stackPtr, $functionName, $parameters)
{
static $search;
if (isset($search) === false) {
$search = [
\T_LNUMBER => \T_LNUMBER,
\T_DNUMBER => \T_DNUMBER,
];
$search += BCTokens::arithmeticTokens();
$search += Tokens::$emptyTokens;
}
$functionLC = \strtolower($functionName);
$functionInfo = $this->targetFunctions[$functionLC];
foreach ($functionInfo as $offset => $paramInfo) {
if (isset($parameters[$offset]) === false) {
continue;
}
if ($this->supportsAbove($paramInfo['since']) === false) {
continue;
}
$target = $parameters[$offset];
$hasNonNumeric = $phpcsFile->findNext($search, $target['start'], ($target['end'] + 1), true);
if ($hasNonNumeric !== false) {
// Not a purely numerical value. Ignore.
continue;
}
$error = 'The $%s parameter of %s() expects a boolean value instead of an integer since PHP %s. Found: %s';
$code = $this->stringToErrorCode($functionName . '_' . $paramInfo['name']) . 'NumericFound';
$data = [
$paramInfo['name'],
$functionLC,
$paramInfo['since'],
$target['raw'],
];
$phpcsFile->addError($error, $target['start'], $code, $data);
}
}
}