-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathRemovedOptionalBeforeRequiredParamSniff.php
146 lines (127 loc) · 4.68 KB
/
RemovedOptionalBeforeRequiredParamSniff.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
<?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\Sniff;
use PHPCSUtils\Tokens\Collections;
use PHPCSUtils\Utils\FunctionDeclarations;
/**
* Declaring a required function parameter after an optional parameter is deprecated since PHP 8.0.
*
* > Declaring a required parameter after an optional one is deprecated. As an
* > exception, declaring a parameter of the form "Type $param = null" before
* > a required one continues to be allowed, because this pattern was sometimes
* > used to achieve nullable types in older PHP versions.
*
* PHP version 8.0
*
* @link https://github.com/php/php-src/blob/69888c3ff1f2301ead8e37b23ff8481d475e29d2/UPGRADING#L145-L151
*
* @since 10.0.0
*/
class RemovedOptionalBeforeRequiredParamSniff extends Sniff
{
/**
* Tokens allowed in the default value.
*
* This property will be enriched in the register() method.
*
* @since 10.0.0
*
* @var array
*/
private $allowedInDefault = [
\T_NULL => \T_NULL,
];
/**
* Returns an array of tokens this test wants to listen for.
*
* @since 10.0.0
*
* @return array
*/
public function register()
{
$this->allowedInDefault += Tokens::$emptyTokens;
return Collections::functionDeclarationTokens();
}
/**
* Processes this test, when one of its tokens is encountered.
*
* @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 passed in $tokens.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
if ($this->supportsAbove('8.0') === false) {
return;
}
// Get all parameters from the function signature.
$parameters = FunctionDeclarations::getParameters($phpcsFile, $stackPtr);
if (empty($parameters)) {
return;
}
$error = 'Declaring a required parameter after an optional one is deprecated since PHP 8.0. Parameter %s is optional, while parameter %s is required.';
$paramCount = \count($parameters);
$lastKey = ($paramCount - 1);
$firstOptional = null;
foreach ($parameters as $key => $param) {
/*
* Ignore variadic parameters, which are optional by nature.
* These always have to be declared last and this has been this way since their introduction.
*/
if ($param['variable_length'] === true) {
continue;
}
// Handle optional parameters.
if (isset($param['default']) === true) {
if ($key === $lastKey) {
// This is the last parameter and it's optional, no further checking needed.
break;
}
if (isset($firstOptional) === false) {
// Check if it's typed and has a null default value, in which case we can ignore it.
if ($param['type_hint'] !== '') {
$hasNull = $phpcsFile->findNext(\T_NULL, $param['default_token'], $param['comma_token']);
$hasNonNull = $phpcsFile->findNext(
$this->allowedInDefault,
$param['default_token'],
$param['comma_token'],
true
);
if ($hasNull !== false && $hasNonNull === false) {
continue;
}
}
// Non-null default value. This is an optional param we need to take into account.
$firstOptional = $param['name'];
}
continue;
}
// Found a required parameter.
if (isset($firstOptional) === false) {
// No optional params found yet.
continue;
}
// Found a required parameter with an optional param before it.
$data = [
$firstOptional,
$param['name'],
];
$phpcsFile->addWarning($error, $param['token'], 'Deprecated', $data);
}
}
}