Skip to content

Commit c91ed28

Browse files
authored
Refactor methods to have nullable return types (#95)
* Change findContaining methods to nullable return * Change findFunctionCallArguments to array return * Change isNextThingAnAssign to nullable return * Rename isNextThingAnAssign to getNextAssignPointer * Change findFunctionPrototype to nullable return * Change findVariableScope to nullable return * Remove 'file' default for getScopeKey
1 parent 1b1b2b5 commit c91ed28

File tree

2 files changed

+59
-62
lines changed

2 files changed

+59
-62
lines changed

VariableAnalysis/Lib/Helpers.php

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,38 @@
55
use PHP_CodeSniffer\Files\File;
66

77
class Helpers {
8+
/**
9+
* @param int|bool $value
10+
*
11+
* @return ?int
12+
*/
13+
public static function getIntOrNull($value) {
14+
return is_int($value) ? $value: null;
15+
}
16+
817
/**
918
* @param File $phpcsFile
1019
* @param int $stackPtr
1120
*
12-
* @return int|bool
21+
* @return ?int
1322
*/
1423
public static function findContainingOpeningSquareBracket(File $phpcsFile, $stackPtr) {
1524
$previousStatementPtr = self::getPreviousStatementPtr($phpcsFile, $stackPtr);
16-
return $phpcsFile->findPrevious(T_OPEN_SHORT_ARRAY, $stackPtr - 1, $previousStatementPtr);
25+
return self::getIntOrNull($phpcsFile->findPrevious(T_OPEN_SHORT_ARRAY, $stackPtr - 1, $previousStatementPtr));
1726
}
1827

1928
/**
2029
* @param File $phpcsFile
2130
* @param int $stackPtr
2231
*
23-
* @return int|bool
32+
* @return ?int
2433
*/
2534
public static function findContainingClosingSquareBracket(File $phpcsFile, $stackPtr) {
26-
$endOfStatementPtr = $phpcsFile->findNext([T_SEMICOLON], $stackPtr + 1);
27-
if (is_bool($endOfStatementPtr)) {
28-
return false;
35+
$endOfStatementPtr = self::getIntOrNull($phpcsFile->findNext([T_SEMICOLON], $stackPtr + 1));
36+
if (! $endOfStatementPtr) {
37+
return null;
2938
}
30-
return $phpcsFile->findNext(T_CLOSE_SHORT_ARRAY, $stackPtr + 1, $endOfStatementPtr);
39+
return self::getIntOrNull($phpcsFile->findNext(T_CLOSE_SHORT_ARRAY, $stackPtr + 1, $endOfStatementPtr));
3140
}
3241

3342
/**
@@ -45,25 +54,25 @@ public static function getPreviousStatementPtr(File $phpcsFile, $stackPtr) {
4554
* @param File $phpcsFile
4655
* @param int $stackPtr
4756
*
48-
* @return int|bool
57+
* @return ?int
4958
*/
5059
public static function findContainingOpeningBracket(File $phpcsFile, $stackPtr) {
5160
$tokens = $phpcsFile->getTokens();
5261
if (isset($tokens[$stackPtr]['nested_parenthesis'])) {
5362
$openPtrs = array_keys($tokens[$stackPtr]['nested_parenthesis']);
5463
return (int)end($openPtrs);
5564
}
56-
return false;
65+
return null;
5766
}
5867

5968
/**
6069
* @param File $phpcsFile
6170
* @param int $stackPtr
6271
*
63-
* @return int|bool
72+
* @return ?int
6473
*/
6574
public static function findParenthesisOwner(File $phpcsFile, $stackPtr) {
66-
return $phpcsFile->findPrevious(T_WHITESPACE, $stackPtr - 1, null, true);
75+
return self::getIntOrNull($phpcsFile->findPrevious(T_WHITESPACE, $stackPtr - 1, null, true));
6776
}
6877

6978
/**
@@ -123,22 +132,22 @@ public static function areConditionsWithinFunctionBeforeClass(array $conditions)
123132
* @param File $phpcsFile
124133
* @param int $openPtr
125134
*
126-
* @return int|bool
135+
* @return ?int
127136
*/
128137
public static function findPreviousFunctionPtr(File $phpcsFile, $openPtr) {
129138
// Function names are T_STRING, and return-by-reference is T_BITWISE_AND,
130139
// so we look backwards from the opening bracket for the first thing that
131140
// isn't a function name, reference sigil or whitespace and check if it's a
132141
// function keyword.
133142
$functionPtrTypes = [T_STRING, T_WHITESPACE, T_BITWISE_AND];
134-
return $phpcsFile->findPrevious($functionPtrTypes, $openPtr - 1, null, true, null, true);
143+
return self::getIntOrNull($phpcsFile->findPrevious($functionPtrTypes, $openPtr - 1, null, true, null, true));
135144
}
136145

137146
/**
138147
* @param File $phpcsFile
139148
* @param int $stackPtr
140149
*
141-
* @return int|bool
150+
* @return ?int
142151
*/
143152
public static function findFunctionCall(File $phpcsFile, $stackPtr) {
144153
$tokens = $phpcsFile->getTokens();
@@ -147,18 +156,18 @@ public static function findFunctionCall(File $phpcsFile, $stackPtr) {
147156
if (is_int($openPtr)) {
148157
// First non-whitespace thing and see if it's a T_STRING function name
149158
$functionPtr = $phpcsFile->findPrevious(T_WHITESPACE, $openPtr - 1, null, true, null, true);
150-
if ($tokens[$functionPtr]['code'] === T_STRING) {
159+
if (is_int($functionPtr) && $tokens[$functionPtr]['code'] === T_STRING) {
151160
return $functionPtr;
152161
}
153162
}
154-
return false;
163+
return null;
155164
}
156165

157166
/**
158167
* @param File $phpcsFile
159168
* @param int $stackPtr
160169
*
161-
* @return array[]|false
170+
* @return array[]
162171
*/
163172
public static function findFunctionCallArguments(File $phpcsFile, $stackPtr) {
164173
$tokens = $phpcsFile->getTokens();
@@ -167,19 +176,19 @@ public static function findFunctionCallArguments(File $phpcsFile, $stackPtr) {
167176
if (($tokens[$stackPtr]['code'] !== T_STRING) && ($tokens[$stackPtr]['code'] !== T_ARRAY)) {
168177
// Assume $stackPtr is something within the brackets, find our function call
169178
$stackPtr = Helpers::findFunctionCall($phpcsFile, $stackPtr);
170-
if ($stackPtr === false) {
171-
return false;
179+
if ($stackPtr === null) {
180+
return [];
172181
}
173182
}
174183

175184
// $stackPtr is the function name, find our brackets after it
176185
$openPtr = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, null, true, null, true);
177186
if (($openPtr === false) || ($tokens[$openPtr]['code'] !== T_OPEN_PARENTHESIS)) {
178-
return false;
187+
return [];
179188
}
180189

181190
if (!isset($tokens[$openPtr]['parenthesis_closer'])) {
182-
return false;
191+
return [];
183192
}
184193
$closePtr = $tokens[$openPtr]['parenthesis_closer'];
185194

@@ -220,7 +229,7 @@ public static function findWhereAssignExecuted(File $phpcsFile, $stackPtr) {
220229
$commaPtr = $phpcsFile->findNext(T_COMMA, $stackPtr + 1, null, false, null, true);
221230
$closePtr = false;
222231
$openPtr = Helpers::findContainingOpeningBracket($phpcsFile, $stackPtr);
223-
if ($openPtr !== false) {
232+
if ($openPtr !== null) {
224233
if (isset($tokens[$openPtr]['parenthesis_closer'])) {
225234
$closePtr = $tokens[$openPtr]['parenthesis_closer'];
226235
}
@@ -240,19 +249,17 @@ public static function findWhereAssignExecuted(File $phpcsFile, $stackPtr) {
240249
* @param File $phpcsFile
241250
* @param int $stackPtr
242251
*
243-
* @return int|bool
252+
* @return ?int
244253
*/
245-
public static function isNextThingAnAssign(File $phpcsFile, $stackPtr) {
254+
public static function getNextAssignPointer(File $phpcsFile, $stackPtr) {
246255
$tokens = $phpcsFile->getTokens();
247256

248257
// Is the next non-whitespace an assignment?
249258
$nextPtr = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, null, true, null, true);
250-
if ($nextPtr !== false) {
251-
if ($tokens[$nextPtr]['code'] === T_EQUAL) {
252-
return $nextPtr;
253-
}
259+
if (is_int($nextPtr) && $tokens[$nextPtr]['code'] === T_EQUAL) {
260+
return $nextPtr;
254261
}
255-
return false;
262+
return null;
256263
}
257264

258265
/**
@@ -269,27 +276,27 @@ public static function normalizeVarName($varName) {
269276
* @param File $phpcsFile
270277
* @param int $stackPtr
271278
*
272-
* @return int|bool
279+
* @return ?int
273280
*/
274281
public static function findFunctionPrototype(File $phpcsFile, $stackPtr) {
275282
$tokens = $phpcsFile->getTokens();
276283

277284
$openPtr = Helpers::findContainingOpeningBracket($phpcsFile, $stackPtr);
278285
if (! is_int($openPtr)) {
279-
return false;
286+
return null;
280287
}
281288
$functionPtr = Helpers::findPreviousFunctionPtr($phpcsFile, $openPtr);
282-
if (($functionPtr !== false) && ($tokens[$functionPtr]['code'] === T_FUNCTION)) {
289+
if (($functionPtr !== null) && ($tokens[$functionPtr]['code'] === T_FUNCTION)) {
283290
return $functionPtr;
284291
}
285-
return false;
292+
return null;
286293
}
287294

288295
/**
289296
* @param File $phpcsFile
290297
* @param int $stackPtr
291298
*
292-
* @return int|false
299+
* @return ?int
293300
*/
294301
public static function findVariableScope(File $phpcsFile, $stackPtr) {
295302
$tokens = $phpcsFile->getTokens();
@@ -314,7 +321,7 @@ public static function findVariableScope(File $phpcsFile, $stackPtr) {
314321

315322
if ($in_class) {
316323
// Member var of a class, we don't care.
317-
return false;
324+
return null;
318325
}
319326

320327
// File scope, hmm, lets use first token of file?

VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -195,14 +195,11 @@ protected function isGetDefinedVars(File $phpcsFile, $stackPtr) {
195195
}
196196

197197
/**
198-
* @param int|bool $currScope
198+
* @param int $currScope
199199
*
200200
* @return string
201201
*/
202202
protected function getScopeKey($currScope) {
203-
if ($currScope === false) {
204-
$currScope = 'file';
205-
}
206203
return ($this->currentFile ? $this->currentFile->getFilename() : 'unknown file') . ':' . $currScope;
207204
}
208205

@@ -442,7 +439,7 @@ protected function markVariableReadAndWarnIfUndefined($phpcsFile, $varName, $sta
442439
*/
443440
protected function markAllVariablesRead(File $phpcsFile, $stackPtr) {
444441
$currScope = Helpers::findVariableScope($phpcsFile, $stackPtr);
445-
if (! $currScope) {
442+
if ($currScope === null) {
446443
return;
447444
}
448445
$scopeInfo = $this->getOrCreateScopeInfo($currScope);
@@ -469,7 +466,7 @@ protected function checkForFunctionPrototype(File $phpcsFile, $stackPtr, $varNam
469466
// T_FUNCTION, but AbstractVariableSniff and AbstractScopeSniff define everything
470467
// we need to do that as private or final, so we have to do it this hackish way.
471468
$openPtr = Helpers::findContainingOpeningBracket($phpcsFile, $stackPtr);
472-
if (is_bool($openPtr)) {
469+
if (! is_int($openPtr)) {
473470
return false;
474471
}
475472

@@ -487,7 +484,7 @@ protected function checkForFunctionPrototype(File $phpcsFile, $stackPtr, $varNam
487484
$varInfo->passByReference = true;
488485
}
489486
// Are we optional with a default?
490-
if (Helpers::isNextThingAnAssign($phpcsFile, $stackPtr) !== false) {
487+
if (Helpers::getNextAssignPointer($phpcsFile, $stackPtr) !== null) {
491488
$this->markVariableAssignment($varName, $stackPtr, $functionPtr);
492489
}
493490
return true;
@@ -574,7 +571,7 @@ protected function checkForCatchBlock(File $phpcsFile, $stackPtr, $varName, $cur
574571

575572
// Are we a catch block parameter?
576573
$openPtr = Helpers::findContainingOpeningBracket($phpcsFile, $stackPtr);
577-
if ($openPtr === false) {
574+
if ($openPtr === null) {
578575
return false;
579576
}
580577

@@ -736,7 +733,7 @@ protected function checkForStaticOutsideClass(File $phpcsFile, $stackPtr, $varNa
736733
*/
737734
protected function checkForAssignment(File $phpcsFile, $stackPtr, $varName, $currScope) {
738735
// Is the next non-whitespace an assignment?
739-
$assignPtr = Helpers::isNextThingAnAssign($phpcsFile, $stackPtr);
736+
$assignPtr = Helpers::getNextAssignPointer($phpcsFile, $stackPtr);
740737
if (! is_int($assignPtr)) {
741738
return false;
742739
}
@@ -787,7 +784,7 @@ protected function checkForVariableVariable(File $phpcsFile, $stackPtr, $varName
787784
protected function checkForListShorthandAssignment(File $phpcsFile, $stackPtr, $varName, $currScope) {
788785
// OK, are we within a [ ... ] construct?
789786
$openPtr = Helpers::findContainingOpeningSquareBracket($phpcsFile, $stackPtr);
790-
if ($openPtr === false) {
787+
if (! is_int($openPtr)) {
791788
return false;
792789
}
793790

@@ -796,7 +793,7 @@ protected function checkForListShorthandAssignment(File $phpcsFile, $stackPtr, $
796793
if (! is_int($closePtr)) {
797794
return false;
798795
}
799-
$assignPtr = Helpers::isNextThingAnAssign($phpcsFile, $closePtr);
796+
$assignPtr = Helpers::getNextAssignPointer($phpcsFile, $closePtr);
800797
if (! is_int($assignPtr)) {
801798
return false;
802799
}
@@ -820,7 +817,7 @@ protected function checkForListAssignment(File $phpcsFile, $stackPtr, $varName,
820817

821818
// OK, are we within a list (...) construct?
822819
$openPtr = Helpers::findContainingOpeningBracket($phpcsFile, $stackPtr);
823-
if ($openPtr === false) {
820+
if ($openPtr === null) {
824821
return false;
825822
}
826823

@@ -831,7 +828,7 @@ protected function checkForListAssignment(File $phpcsFile, $stackPtr, $varName,
831828

832829
// OK, we're a list (...) construct... are we being assigned to?
833830
$closePtr = $tokens[$openPtr]['parenthesis_closer'];
834-
$assignPtr = Helpers::isNextThingAnAssign($phpcsFile, $closePtr);
831+
$assignPtr = Helpers::getNextAssignPointer($phpcsFile, $closePtr);
835832
if (! is_int($assignPtr)) {
836833
return false;
837834
}
@@ -927,7 +924,7 @@ protected function checkForStaticDeclaration(File $phpcsFile, $stackPtr, $varNam
927924

928925
// It's a static declaration.
929926
$this->markVariableDeclaration($varName, 'static', null, $stackPtr, $currScope);
930-
if (Helpers::isNextThingAnAssign($phpcsFile, $stackPtr) !== false) {
927+
if (Helpers::getNextAssignPointer($phpcsFile, $stackPtr) !== null) {
931928
$this->markVariableAssignment($varName, $stackPtr, $currScope);
932929
}
933930
return true;
@@ -1000,7 +997,7 @@ protected function checkForPassByReferenceFunctionCall(File $phpcsFile, $stackPt
1000997

1001998
// Are we pass-by-reference to known pass-by-reference function?
1002999
$functionPtr = Helpers::findFunctionCall($phpcsFile, $stackPtr);
1003-
if ($functionPtr === false || ! isset($tokens[$functionPtr])) {
1000+
if ($functionPtr === null || ! isset($tokens[$functionPtr])) {
10041001
return false;
10051002
}
10061003

@@ -1012,9 +1009,6 @@ protected function checkForPassByReferenceFunctionCall(File $phpcsFile, $stackPt
10121009
}
10131010

10141011
$argPtrs = Helpers::findFunctionCallArguments($phpcsFile, $stackPtr);
1015-
if ($argPtrs === false) {
1016-
return false;
1017-
}
10181012

10191013
// We're within a function call arguments list, find which arg we are.
10201014
$argPos = false;
@@ -1094,7 +1088,7 @@ protected function processVariable(File $phpcsFile, $stackPtr) {
10941088
$varName = Helpers::normalizeVarName($token['content']);
10951089
Helpers::debug('examining token ' . $varName);
10961090
$currScope = Helpers::findVariableScope($phpcsFile, $stackPtr);
1097-
if ($currScope === false) {
1091+
if ($currScope === null) {
10981092
Helpers::debug('no scope found');
10991093
return;
11001094
}
@@ -1239,7 +1233,7 @@ protected function processVariableInString(File $phpcsFile, $stackPtr) {
12391233
}
12401234

12411235
$currScope = Helpers::findVariableScope($phpcsFile, $stackPtr);
1242-
if (! $currScope) {
1236+
if ($currScope === null) {
12431237
return;
12441238
}
12451239
foreach ($matches[1] as $varName) {
@@ -1287,9 +1281,7 @@ protected function processCompactArguments(File $phpcsFile, $stackPtr, $argument
12871281
if ($argument_first_token['code'] === T_ARRAY) {
12881282
// It's an array argument, recurse.
12891283
$array_arguments = Helpers::findFunctionCallArguments($phpcsFile, $argumentPtrs[0]);
1290-
if ($array_arguments !== false) {
1291-
$this->processCompactArguments($phpcsFile, $stackPtr, $array_arguments, $currScope);
1292-
}
1284+
$this->processCompactArguments($phpcsFile, $stackPtr, $array_arguments, $currScope);
12931285
continue;
12941286
}
12951287
if (count($argumentPtrs) > 1) {
@@ -1327,14 +1319,12 @@ protected function processCompactArguments(File $phpcsFile, $stackPtr, $argument
13271319
*/
13281320
protected function processCompact(File $phpcsFile, $stackPtr) {
13291321
$currScope = Helpers::findVariableScope($phpcsFile, $stackPtr);
1330-
if (! $currScope) {
1322+
if ($currScope === null) {
13311323
return;
13321324
}
13331325

13341326
$arguments = Helpers::findFunctionCallArguments($phpcsFile, $stackPtr);
1335-
if ($arguments !== false) {
1336-
$this->processCompactArguments($phpcsFile, $stackPtr, $arguments, $currScope);
1337-
}
1327+
$this->processCompactArguments($phpcsFile, $stackPtr, $arguments, $currScope);
13381328
}
13391329

13401330
/**

0 commit comments

Comments
 (0)