Skip to content

Commit 1529566

Browse files
committed
Squiz/FunctionCommentThrowTag: fix sniff to work with the PHP 8 identifier tokens
Includes adding some extra tests to ensure all identifier name types are covered.
1 parent 2ccfae2 commit 1529566

File tree

2 files changed

+31
-27
lines changed

2 files changed

+31
-27
lines changed

src/Standards/Squiz/Sniffs/Commenting/FunctionCommentThrowTagSniff.php

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -106,44 +106,30 @@ public function process(File $phpcsFile, $stackPtr)
106106

107107
$nextToken = $phpcsFile->findNext(Tokens::$emptyTokens, ($currPos + 1), null, true);
108108
if ($tokens[$nextToken]['code'] === T_NEW
109-
|| $tokens[$nextToken]['code'] === T_NS_SEPARATOR
110-
|| $tokens[$nextToken]['code'] === T_STRING
109+
|| isset(Tokens::$nameTokens[$tokens[$nextToken]['code']]) === true
111110
) {
112111
if ($tokens[$nextToken]['code'] === T_NEW) {
113112
$currException = $phpcsFile->findNext(
114-
[
115-
T_NS_SEPARATOR,
116-
T_STRING,
117-
],
118-
$currPos,
113+
Tokens::$emptyTokens,
114+
($nextToken + 1),
119115
$stackPtrEnd,
120-
false,
121-
null,
122116
true
123117
);
124118
} else {
125119
$currException = $nextToken;
126120
}
127121

128-
if ($currException !== false) {
129-
$endException = $phpcsFile->findNext(
130-
[
131-
T_NS_SEPARATOR,
132-
T_STRING,
133-
],
134-
($currException + 1),
135-
$stackPtrEnd,
136-
true,
137-
null,
138-
true
139-
);
140-
141-
if ($endException === false) {
142-
$thrownExceptions[] = $tokens[$currException]['content'];
122+
if ($currException !== false
123+
&& isset(Tokens::$nameTokens[$tokens[$currException]['code']]) === true
124+
) {
125+
if ($tokens[$currException]['code'] === T_NAME_RELATIVE) {
126+
// Strip the `namespace\` prefix off the exception name
127+
// to prevent confusing the name comparison.
128+
$thrownExceptions[] = substr($tokens[$currException]['content'], 10);
143129
} else {
144-
$thrownExceptions[] = $phpcsFile->getTokensAsString($currException, ($endException - $currException));
130+
$thrownExceptions[] = $tokens[$currException]['content'];
145131
}
146-
}//end if
132+
}
147133
} else if ($tokens[$nextToken]['code'] === T_VARIABLE) {
148134
// Find the nearest catch block in this scope and, if the caught var
149135
// matches our re-thrown var, use the exception types being caught as

src/Standards/Squiz/Tests/Commenting/FunctionCommentThrowTagUnitTest.inc

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ class NamespacedException {
281281
public function fooBar2() {
282282
throw new \Foo\Bar\FooBarException();
283283
}
284-
284+
285285
/**
286286
* @throws FooBarException
287287
*/
@@ -531,3 +531,21 @@ $anon = new class {
531531
public function ImproveCommentTolerance() {
532532
throw /*comment*/ new /*comment*/ Right_Exception('Error');
533533
}
534+
535+
namespace Namespaced\Names {
536+
class NamespacedNames {
537+
/**
538+
* @throws Namespaced\Names\Exception
539+
*/
540+
public function foo() {
541+
throw new namespace\Exception();
542+
}
543+
544+
/**
545+
* @throws Namespaced\Names\Bar\FooBarException
546+
*/
547+
public function fooBar2() {
548+
throw new Bar\FooBarException();
549+
}
550+
}
551+
}

0 commit comments

Comments
 (0)