Skip to content

Commit 3d7233d

Browse files
committed
More TokenHelper methods
1 parent 36e29f3 commit 3d7233d

28 files changed

+207
-122
lines changed

SlevomatCodingStandard/Helpers/CatchHelper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ public static function findCatchedTypesInCatch(\PHP_CodeSniffer_File $phpcsFile,
1717
$tokens = $phpcsFile->getTokens();
1818
$catchedTypes = [];
1919
do {
20-
$nameStartPointer = $phpcsFile->findNext(array_merge([T_BITWISE_OR], TokenHelper::$nameTokenCodes), $nameEndPointer + 1, $catchToken['parenthesis_closer']);
21-
if ($nameStartPointer === false) {
20+
$nameStartPointer = TokenHelper::findNext($phpcsFile, array_merge([T_BITWISE_OR], TokenHelper::$nameTokenCodes), $nameEndPointer + 1, $catchToken['parenthesis_closer']);
21+
if ($nameStartPointer === null) {
2222
break;
2323
}
2424

SlevomatCodingStandard/Helpers/ClassHelper.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static function getFullyQualifiedName(\PHP_CodeSniffer_File $codeSnifferF
1515
public static function getName(\PHP_CodeSniffer_File $codeSnifferFile, int $classPointer): string
1616
{
1717
$tokens = $codeSnifferFile->getTokens();
18-
return $tokens[$codeSnifferFile->findNext(T_STRING, $classPointer + 1, $tokens[$classPointer]['scope_opener'])]['content'];
18+
return $tokens[TokenHelper::findNext($codeSnifferFile, T_STRING, $classPointer + 1, $tokens[$classPointer]['scope_opener'])]['content'];
1919
}
2020

2121
/**
@@ -37,12 +37,12 @@ function (int $classPointer) use ($codeSnifferFile): string {
3737
private static function getAllClassPointers(\PHP_CodeSniffer_File $codeSnifferFile, int &$previousClassPointer): \Generator
3838
{
3939
do {
40-
$nextClassPointer = $codeSnifferFile->findNext(TokenHelper::$typeKeywordTokenCodes, $previousClassPointer + 1);
41-
if ($nextClassPointer !== false) {
40+
$nextClassPointer = TokenHelper::findNext($codeSnifferFile, TokenHelper::$typeKeywordTokenCodes, $previousClassPointer + 1);
41+
if ($nextClassPointer !== null) {
4242
$previousClassPointer = $nextClassPointer;
4343
yield $nextClassPointer;
4444
}
45-
} while ($nextClassPointer !== false);
45+
} while ($nextClassPointer !== null);
4646
}
4747

4848
}

SlevomatCodingStandard/Helpers/DocCommentHelper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ public static function hasDocCommentDescription(\PHP_CodeSniffer_File $codeSniff
1818
}
1919

2020
$tokens = $codeSnifferFile->getTokens();
21-
$found = $codeSnifferFile->findNext([T_DOC_COMMENT_WHITESPACE, T_DOC_COMMENT_STAR], $docCommentOpenToken + 1, $tokens[$docCommentOpenToken]['comment_closer'] - 1, true);
21+
$found = TokenHelper::findNextExcluding($codeSnifferFile, [T_DOC_COMMENT_WHITESPACE, T_DOC_COMMENT_STAR], $docCommentOpenToken + 1, $tokens[$docCommentOpenToken]['comment_closer'] - 1);
2222

23-
return $found !== false && $tokens[$found]['code'] === T_DOC_COMMENT_STRING;
23+
return $found !== null && $tokens[$found]['code'] === T_DOC_COMMENT_STRING;
2424
}
2525

2626
/**

SlevomatCodingStandard/Helpers/FunctionHelper.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class FunctionHelper
88
public static function getName(\PHP_CodeSniffer_File $codeSnifferFile, int $functionPointer): string
99
{
1010
$tokens = $codeSnifferFile->getTokens();
11-
return $tokens[$codeSnifferFile->findNext(T_STRING, $functionPointer + 1, $tokens[$functionPointer]['parenthesis_opener'])]['content'];
11+
return $tokens[TokenHelper::findNext($codeSnifferFile, T_STRING, $functionPointer + 1, $tokens[$functionPointer]['parenthesis_opener'])]['content'];
1212
}
1313

1414
public static function getFullyQualifiedName(\PHP_CodeSniffer_File $codeSnifferFile, int $functionPointer): string
@@ -164,29 +164,29 @@ public static function findReturnTypeHint(\PHP_CodeSniffer_File $codeSnifferFile
164164
$isAbstract = self::isAbstract($codeSnifferFile, $functionPointer);
165165

166166
$colonToken = $isAbstract
167-
? $codeSnifferFile->findNext(T_COLON, $tokens[$functionPointer]['parenthesis_closer'] + 1, null, false, null, true)
168-
: $codeSnifferFile->findNext(T_COLON, $tokens[$functionPointer]['parenthesis_closer'] + 1, $tokens[$functionPointer]['scope_opener'] - 1);
167+
? TokenHelper::findNextLocal($codeSnifferFile, T_COLON, $tokens[$functionPointer]['parenthesis_closer'] + 1)
168+
: TokenHelper::findNext($codeSnifferFile, T_COLON, $tokens[$functionPointer]['parenthesis_closer'] + 1, $tokens[$functionPointer]['scope_opener'] - 1);
169169

170-
if ($colonToken === false) {
170+
if ($colonToken === null) {
171171
return null;
172172
}
173173

174174
$abstractExcludeTokens = array_merge(TokenHelper::$ineffectiveTokenCodes, [T_SEMICOLON]);
175175

176176
$nullableToken = $isAbstract
177-
? $codeSnifferFile->findNext($abstractExcludeTokens, $colonToken + 1, null, true, null, true)
178-
: $codeSnifferFile->findNext(TokenHelper::$ineffectiveTokenCodes, $colonToken + 1, $tokens[$functionPointer]['scope_opener'] - 1, true);
177+
? TokenHelper::findNextLocalExcluding($codeSnifferFile, $abstractExcludeTokens, $colonToken + 1)
178+
: TokenHelper::findNextExcluding($codeSnifferFile, TokenHelper::$ineffectiveTokenCodes, $colonToken + 1, $tokens[$functionPointer]['scope_opener'] - 1);
179179

180-
$nullable = $nullableToken !== false && $tokens[$nullableToken]['code'] === T_NULLABLE;
180+
$nullable = $nullableToken !== null && $tokens[$nullableToken]['code'] === T_NULLABLE;
181181

182182
$typeHint = '';
183183
$nextToken = $nullable ? $nullableToken : $colonToken;
184184
do {
185185
$nextToken = $isAbstract
186-
? $codeSnifferFile->findNext($abstractExcludeTokens, $nextToken + 1, null, true, null, true)
187-
: $codeSnifferFile->findNext(TokenHelper::$ineffectiveTokenCodes, $nextToken + 1, $tokens[$functionPointer]['scope_opener'] - 1, true);
186+
? TokenHelper::findNextLocalExcluding($codeSnifferFile, $abstractExcludeTokens, $nextToken + 1)
187+
: TokenHelper::findNextExcluding($codeSnifferFile, TokenHelper::$ineffectiveTokenCodes, $nextToken + 1, $tokens[$functionPointer]['scope_opener'] - 1);
188188

189-
$isTypeHint = $nextToken !== false;
189+
$isTypeHint = $nextToken !== null;
190190
if ($isTypeHint) {
191191
$typeHint .= $tokens[$nextToken]['content'];
192192
}

SlevomatCodingStandard/Helpers/NamespaceHelper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ public static function getNameParts(string $name): array
4343
*/
4444
public static function findCurrentNamespaceName(\PHP_CodeSniffer_File $phpcsFile, int $anyPointer)
4545
{
46-
$namespacePointer = $phpcsFile->findPrevious(T_NAMESPACE, $anyPointer);
47-
if ($namespacePointer === false) {
46+
$namespacePointer = TokenHelper::findPrevious($phpcsFile, T_NAMESPACE, $anyPointer);
47+
if ($namespacePointer === null) {
4848
return null;
4949
}
5050

SlevomatCodingStandard/Helpers/ReferencedNameHelper.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ private static function createAllReferencedNames(\PHP_CodeSniffer_File $phpcsFil
6161
$types = [];
6262

6363
while (true) {
64-
$nameStartPointer = $phpcsFile->findNext($searchTypes, $beginSearchAtPointer);
65-
if ($nameStartPointer === false) {
64+
$nameStartPointer = TokenHelper::findNext($phpcsFile, $searchTypes, $beginSearchAtPointer);
65+
if ($nameStartPointer === null) {
6666
break;
6767
}
6868

@@ -169,8 +169,8 @@ private static function isReferencedName(\PHP_CodeSniffer_File $phpcsFile, int $
169169
];
170170

171171
if ($previousToken['code'] === T_USE) {
172-
$classPointer = $phpcsFile->findPrevious([T_CLASS, T_TRAIT], $startPointer - 1);
173-
if ($classPointer !== false) {
172+
$classPointer = TokenHelper::findPrevious($phpcsFile, [T_CLASS, T_TRAIT], $startPointer - 1);
173+
if ($classPointer !== null) {
174174
$classToken = $tokens[$classPointer];
175175
return $startPointer > $classToken['scope_opener'] && $startPointer < $classToken['scope_closer'];
176176
}

SlevomatCodingStandard/Helpers/TokenHelper.php

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,39 @@ class TokenHelper
4141
T_CALLABLE,
4242
];
4343

44+
/**
45+
* @param \PHP_CodeSniffer_File $phpcsFile
46+
* @param int|int[] $types
47+
* @param int $startPointer
48+
* @param int|null $endPointer
49+
* @return int|null
50+
*/
51+
public static function findNext(\PHP_CodeSniffer_File $phpcsFile, $types, int $startPointer, int $endPointer = null)
52+
{
53+
$token = $phpcsFile->findNext($types, $startPointer, $endPointer, false);
54+
if ($token === false) {
55+
return null;
56+
}
57+
return $token;
58+
}
59+
60+
/**
61+
* @param \PHP_CodeSniffer_File $phpcsFile
62+
* @param int|int[] $types
63+
* @param string $content
64+
* @param int $startPointer
65+
* @param int|null $endPointer
66+
* @return int|null
67+
*/
68+
public static function findNextContent(\PHP_CodeSniffer_File $phpcsFile, $types, string $content, int $startPointer, int $endPointer = null)
69+
{
70+
$token = $phpcsFile->findNext($types, $startPointer, $endPointer, false, $content);
71+
if ($token === false) {
72+
return null;
73+
}
74+
return $token;
75+
}
76+
4477
/**
4578
* @param \PHP_CodeSniffer_File $phpcsFile
4679
* @param int $startPointer search starts at this token, inclusive
@@ -68,6 +101,38 @@ public static function findNextExcluding(\PHP_CodeSniffer_File $phpcsFile, $type
68101
return $token;
69102
}
70103

104+
/**
105+
* @param \PHP_CodeSniffer_File $phpcsFile
106+
* @param int|int[] $types
107+
* @param int $startPointer
108+
* @param int|null $endPointer
109+
* @return int|null
110+
*/
111+
public static function findNextLocal(\PHP_CodeSniffer_File $phpcsFile, $types, int $startPointer, int $endPointer = null)
112+
{
113+
$token = $phpcsFile->findNext($types, $startPointer, $endPointer, false, null, true);
114+
if ($token === false) {
115+
return null;
116+
}
117+
return $token;
118+
}
119+
120+
/**
121+
* @param \PHP_CodeSniffer_File $phpcsFile
122+
* @param int|int[] $types
123+
* @param int $startPointer
124+
* @param int|null $endPointer
125+
* @return int|null
126+
*/
127+
public static function findNextLocalExcluding(\PHP_CodeSniffer_File $phpcsFile, $types, int $startPointer, int $endPointer = null)
128+
{
129+
$token = $phpcsFile->findNext($types, $startPointer, $endPointer, true, null, true);
130+
if ($token === false) {
131+
return null;
132+
}
133+
return $token;
134+
}
135+
71136
/**
72137
* @param \PHP_CodeSniffer_File $phpcsFile
73138
* @param int $startPointer search starts at this token, inclusive
@@ -79,6 +144,22 @@ public static function findNextAnyToken(\PHP_CodeSniffer_File $phpcsFile, int $s
79144
return self::findNextExcluding($phpcsFile, [], $startPointer, $endPointer);
80145
}
81146

147+
/**
148+
* @param \PHP_CodeSniffer_File $phpcsFile
149+
* @param int[]|int $types
150+
* @param int $startPointer search starts at this token, inclusive
151+
* @param int|null $endPointer search ends at this token, exclusive
152+
* @return int|null
153+
*/
154+
public static function findPrevious(\PHP_CodeSniffer_File $phpcsFile, $types, int $startPointer, int $endPointer = null)
155+
{
156+
$token = $phpcsFile->findPrevious($types, $startPointer, $endPointer, false);
157+
if ($token === false) {
158+
return null;
159+
}
160+
return $token;
161+
}
162+
82163
/**
83164
* @param \PHP_CodeSniffer_File $phpcsFile
84165
* @param int $startPointer search starts at this token, inclusive
@@ -113,8 +194,8 @@ public static function findPreviousExcluding(\PHP_CodeSniffer_File $phpcsFile, $
113194
*/
114195
public static function findFirstTokenOnNextLine(\PHP_CodeSniffer_File $phpcsFile, int $pointer)
115196
{
116-
$newLinePointer = $phpcsFile->findNext(T_WHITESPACE, $pointer, null, false, $phpcsFile->eolChar);
117-
if ($newLinePointer === false) {
197+
$newLinePointer = self::findNextContent($phpcsFile, T_WHITESPACE, $phpcsFile->eolChar, $pointer);
198+
if ($newLinePointer === null) {
118199
return null;
119200
}
120201
$tokens = $phpcsFile->getTokens();

SlevomatCodingStandard/Helpers/TypeHintHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public static function getFullyQualifiedTypeHint(\PHP_CodeSniffer_File $phpcsFil
6363
return self::convertLongSimpleTypeHintToShort($typeHint);
6464
}
6565

66-
$useStatements = UseStatementHelper::getUseStatements($phpcsFile, $phpcsFile->findPrevious(T_OPEN_TAG, $pointer));
66+
$useStatements = UseStatementHelper::getUseStatements($phpcsFile, TokenHelper::findPrevious($phpcsFile, T_OPEN_TAG, $pointer));
6767
return NamespaceHelper::resolveName($phpcsFile, $typeHint, $useStatements, $pointer);
6868
}
6969

SlevomatCodingStandard/Helpers/UseStatementHelper.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ public static function isAnonymousFunctionUse(\PHP_CodeSniffer_File $phpcsFile,
1616

1717
public static function isTraitUse(\PHP_CodeSniffer_File $phpcsFile, int $usePointer): bool
1818
{
19-
$typePointer = $phpcsFile->findPrevious(array_merge(TokenHelper::$typeKeywordTokenCodes, [T_ANON_CLASS]), $usePointer);
20-
if ($typePointer !== false) {
19+
$typePointer = TokenHelper::findPrevious($phpcsFile, array_merge(TokenHelper::$typeKeywordTokenCodes, [T_ANON_CLASS]), $usePointer);
20+
if ($typePointer !== null) {
2121
$tokens = $phpcsFile->getTokens();
2222
$typeToken = $tokens[$typePointer];
2323
$openerPointer = $typeToken['scope_opener'];
@@ -31,11 +31,11 @@ public static function isTraitUse(\PHP_CodeSniffer_File $phpcsFile, int $usePoin
3131

3232
public static function getNameAsReferencedInClassFromUse(\PHP_CodeSniffer_File $phpcsFile, int $usePointer): string
3333
{
34-
$endPointer = $phpcsFile->findNext([T_SEMICOLON, T_COMMA], $usePointer + 1);
35-
$asPointer = $phpcsFile->findNext(T_AS, $usePointer + 1, $endPointer);
36-
if ($asPointer !== false) {
34+
$endPointer = TokenHelper::findNext($phpcsFile, [T_SEMICOLON, T_COMMA], $usePointer + 1);
35+
$asPointer = TokenHelper::findNext($phpcsFile, T_AS, $usePointer + 1, $endPointer);
36+
if ($asPointer !== null) {
3737
$tokens = $phpcsFile->getTokens();
38-
return $tokens[$phpcsFile->findNext(T_STRING, $asPointer + 1)]['content'];
38+
return $tokens[TokenHelper::findNext($phpcsFile, T_STRING, $asPointer + 1)]['content'];
3939
}
4040
$name = self::getFullyQualifiedTypeNameFromUse($phpcsFile, $usePointer);
4141

@@ -46,7 +46,7 @@ public static function getFullyQualifiedTypeNameFromUse(\PHP_CodeSniffer_File $p
4646
{
4747
$tokens = $phpcsFile->getTokens();
4848

49-
$nameEndPointer = $phpcsFile->findNext([T_SEMICOLON, T_AS, T_COMMA], $usePointer + 1) - 1;
49+
$nameEndPointer = TokenHelper::findNext($phpcsFile, [T_SEMICOLON, T_AS, T_COMMA], $usePointer + 1) - 1;
5050
if (in_array($tokens[$nameEndPointer]['code'], TokenHelper::$ineffectiveTokenCodes, true)) {
5151
$nameEndPointer = TokenHelper::findPreviousEffective($phpcsFile, $nameEndPointer);
5252
}
@@ -103,8 +103,8 @@ private static function getUseStatementPointers(\PHP_CodeSniffer_File $phpcsFile
103103
$pointers = [];
104104
while (true) {
105105
$typesToFind = array_merge([T_USE], TokenHelper::$typeKeywordTokenCodes);
106-
$pointer = $phpcsFile->findNext($typesToFind, $pointer);
107-
if ($pointer === false) {
106+
$pointer = TokenHelper::findNext($phpcsFile, $typesToFind, $pointer);
107+
if ($pointer === null) {
108108
break;
109109
}
110110

SlevomatCodingStandard/Sniffs/Classes/UnusedPrivateElementsSniff.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public function process(\PHP_CodeSniffer_File $phpcsFile, $classPointer)
9494
$writeOnlyProperties = [];
9595
$findUsagesStartTokenPointer = $classToken['scope_opener'] + 1;
9696

97-
while (($propertyAccessTokenPointer = $phpcsFile->findNext([T_VARIABLE, T_SELF, T_STATIC, T_DOUBLE_QUOTED_STRING], $findUsagesStartTokenPointer, $classToken['scope_closer'])) !== false) {
97+
while (($propertyAccessTokenPointer = TokenHelper::findNext($phpcsFile, [T_VARIABLE, T_SELF, T_STATIC, T_DOUBLE_QUOTED_STRING], $findUsagesStartTokenPointer, $classToken['scope_closer'])) !== null) {
9898
$propertyAccessToken = $tokens[$propertyAccessTokenPointer];
9999
if ($propertyAccessToken['code'] === T_DOUBLE_QUOTED_STRING) {
100100
if (preg_match_all('~(?<!\\\\)\$this->(.+?\b)(?!\()~', $propertyAccessToken['content'], $matches, PREG_PATTERN_ORDER)) {
@@ -251,7 +251,7 @@ private function getProperties(\PHP_CodeSniffer_File $phpcsFile, array $tokens,
251251
{
252252
$reportedProperties = [];
253253
$findPropertiesStartTokenPointer = $classToken['scope_opener'] + 1;
254-
while (($propertyTokenPointer = $phpcsFile->findNext(T_VARIABLE, $findPropertiesStartTokenPointer, $classToken['scope_closer'])) !== false) {
254+
while (($propertyTokenPointer = TokenHelper::findNext($phpcsFile, T_VARIABLE, $findPropertiesStartTokenPointer, $classToken['scope_closer'])) !== null) {
255255
$visibilityModifiedTokenPointer = TokenHelper::findPreviousEffective($phpcsFile, $propertyTokenPointer - 1);
256256
$visibilityModifiedToken = $tokens[$visibilityModifiedTokenPointer];
257257
if ($visibilityModifiedToken['code'] !== T_PRIVATE) {
@@ -303,7 +303,7 @@ private function getMethods(\PHP_CodeSniffer_File $phpcsFile, array $tokens, arr
303303
{
304304
$reportedMethods = [];
305305
$findMethodsStartTokenPointer = $classToken['scope_opener'] + 1;
306-
while (($methodTokenPointer = $phpcsFile->findNext(T_FUNCTION, $findMethodsStartTokenPointer, $classToken['scope_closer'])) !== false) {
306+
while (($methodTokenPointer = TokenHelper::findNext($phpcsFile, T_FUNCTION, $findMethodsStartTokenPointer, $classToken['scope_closer'])) !== null) {
307307
$visibilityModifier = $this->findVisibilityModifier($phpcsFile, $tokens, $methodTokenPointer);
308308
if ($visibilityModifier === null || $visibilityModifier !== T_PRIVATE) {
309309
$findMethodsStartTokenPointer = $methodTokenPointer + 1;
@@ -331,7 +331,7 @@ private function getConstants(\PHP_CodeSniffer_File $phpcsFile, array $tokens, a
331331
{
332332
$reportedConstants = [];
333333
$findConstantsStartTokenPointer = $classToken['scope_opener'] + 1;
334-
while (($constantTokenPointer = $phpcsFile->findNext(T_CONST, $findConstantsStartTokenPointer, $classToken['scope_closer'])) !== false) {
334+
while (($constantTokenPointer = TokenHelper::findNext($phpcsFile, T_CONST, $findConstantsStartTokenPointer, $classToken['scope_closer'])) !== null) {
335335
$visibilityModifier = $this->findVisibilityModifier($phpcsFile, $tokens, $constantTokenPointer);
336336
if ($visibilityModifier === null || $visibilityModifier !== T_PRIVATE) {
337337
$findConstantsStartTokenPointer = $constantTokenPointer + 1;

0 commit comments

Comments
 (0)