Skip to content

Commit b1d20fe

Browse files
committed
Fix PHPStan
1 parent 6cbb95e commit b1d20fe

File tree

5 files changed

+27
-4
lines changed

5 files changed

+27
-4
lines changed

PhpCollective/Sniffs/Classes/MethodArgumentDefaultValueSniff.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,17 @@ protected function getLastNonDefaultArgumentIndex(File $phpcsFile, int $startInd
8888
$token = $tokens[$i];
8989

9090
if ($this->isGivenKind(T_EQUAL, $token)) {
91-
$i = $phpcsFile->findPrevious(T_VARIABLE, $i - 1) ?: null;
92-
if (!$i) {
91+
$result = $phpcsFile->findPrevious(T_VARIABLE, $i - 1);
92+
if ($result === false) {
9393
continue;
9494
}
95+
$i = $result;
9596

96-
$i = $phpcsFile->findPrevious(Tokens::$emptyTokens, $i, $startIndex - 1, true) ?: null;
97+
$result = $phpcsFile->findPrevious(Tokens::$emptyTokens, $i, $startIndex - 1, true);
98+
if ($result === false) {
99+
break;
100+
}
101+
$i = $result;
97102

98103
continue;
99104
}

PhpCollective/Sniffs/Commenting/DocBlockReturnNullableTypeSniff.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,9 @@ protected function getDocBlockReturnTypeToken(File $phpcsFile, int $stackPointer
235235

236236
$docBlockStartIndex = DocCommentHelper::findDocCommentOpenPointer($phpcsFile, $stackPointer);
237237
$docBlockEndIndex = $this->findRelatedDocBlock($phpcsFile, $stackPointer);
238+
if (!$docBlockEndIndex) {
239+
return [];
240+
}
238241

239242
for ($i = $docBlockEndIndex; $i >= $docBlockStartIndex; $i--) {
240243
if ($tokens[$i]['content'] !== '@return') {

PhpCollective/Sniffs/Namespaces/UseStatementSniff.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1044,6 +1044,9 @@ protected function getUseStatements(File $phpcsFile): array
10441044
}
10451045

10461046
$useStatementStartIndex = $phpcsFile->findNext(Tokens::$emptyTokens, $index + 1, null, true);
1047+
if ($useStatementStartIndex === false) {
1048+
continue;
1049+
}
10471050

10481051
// Ignore function () use ($foo) {}
10491052
if ($tokens[$useStatementStartIndex]['content'] === '(') {
@@ -1052,6 +1055,9 @@ protected function getUseStatements(File $phpcsFile): array
10521055

10531056
$semicolonIndex = $phpcsFile->findNext(T_SEMICOLON, $useStatementStartIndex + 1);
10541057
$useStatementEndIndex = $phpcsFile->findPrevious(Tokens::$emptyTokens, $semicolonIndex - 1, null, true);
1058+
if ($useStatementEndIndex === false) {
1059+
continue;
1060+
}
10551061

10561062
$statement = '';
10571063
for ($i = $useStatementStartIndex; $i <= $useStatementEndIndex; $i++) {
@@ -1239,7 +1245,7 @@ protected function parse(File $phpcsFile, int $startIndex, int $endIndex): array
12391245
}
12401246

12411247
$classIndex = $phpcsFile->findNext(Tokens::$emptyTokens, $startIndex + 1, null, true);
1242-
if (empty($tokens[$classIndex])) {
1248+
if ($classIndex === false || empty($tokens[$classIndex])) {
12431249
throw new RuntimeException('Should not happen');
12441250
}
12451251

PhpCollective/Traits/BasicsTrait.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ protected function getNamespaceStatement(File $phpcsFile): array
5454
$namespace = '';
5555
$namespaceStartIndex = $phpcsFile->findNext(Tokens::$emptyTokens, $namespaceIndex + 1, null, true);
5656
$namespaceEndIndex = $phpcsFile->findPrevious(Tokens::$emptyTokens, $endIndex - 1, null, true);
57+
if ($namespaceStartIndex === false || $namespaceEndIndex === false) {
58+
return [];
59+
}
5760
for ($i = $namespaceStartIndex; $i <= $namespaceEndIndex; $i++) {
5861
$namespace .= $tokens[$i]['content'];
5962
}

PhpCollective/Traits/UseStatementsTrait.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ protected function getUseStatements(File $phpcsFile): array
2828
}
2929

3030
$useStatementStartIndex = $phpcsFile->findNext(Tokens::$emptyTokens, $index + 1, null, true);
31+
if ($useStatementStartIndex === false) {
32+
continue;
33+
}
3134

3235
// Ignore function () use ($foo) {}
3336
if ($tokens[$useStatementStartIndex]['content'] === '(') {
@@ -36,6 +39,9 @@ protected function getUseStatements(File $phpcsFile): array
3639

3740
$semicolonIndex = $phpcsFile->findNext(T_SEMICOLON, $useStatementStartIndex + 1);
3841
$useStatementEndIndex = $phpcsFile->findPrevious(Tokens::$emptyTokens, $semicolonIndex - 1, null, true);
42+
if ($useStatementEndIndex === false) {
43+
continue;
44+
}
3945

4046
$statement = '';
4147
for ($i = $useStatementStartIndex; $i <= $useStatementEndIndex; $i++) {

0 commit comments

Comments
 (0)