Skip to content

Commit 5af4388

Browse files
committed
Fixed bug #790 : Incorrect missing @throws error in methods that use closures
1 parent 88ca35e commit 5af4388

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,16 @@ protected function processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, $sta
8484
return;
8585
}
8686

87-
// Find the position where the current function scope ends.
88-
$currScopeEnd = 0;
89-
if (isset($tokens[$currScope]['scope_closer']) === true) {
90-
$currScopeEnd = $tokens[$currScope]['scope_closer'];
91-
}
87+
$currScopeEnd = $tokens[$currScope]['scope_closer'];
9288

9389
// Find all the exception type token within the current scope.
9490
$throwTokens = array();
9591
$currPos = $stackPtr;
96-
if ($currScopeEnd !== 0) {
97-
while ($currPos < $currScopeEnd && $currPos !== false) {
92+
$foundThrows = false;
93+
while ($currPos < $currScopeEnd && $currPos !== false) {
94+
if ($phpcsFile->hasCondition($currPos, T_CLOSURE) === false) {
95+
$foundThrows = true;
96+
9897
/*
9998
If we can't find a NEW, we are probably throwing
10099
a variable, so we ignore it, but they still need to
@@ -136,10 +135,14 @@ protected function processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, $sta
136135
}
137136
}//end if
138137
}//end if
138+
}//end if
139+
140+
$currPos = $phpcsFile->findNext(T_THROW, ($currPos + 1), $currScopeEnd);
141+
}//end while
139142

140-
$currPos = $phpcsFile->findNext(T_THROW, ($currPos + 1), $currScopeEnd);
141-
}//end while
142-
}//end if
143+
if ($foundThrows === false) {
144+
return;
145+
}
143146

144147
// Only need one @throws tag for each type of exception thrown.
145148
$throwTokens = array_unique($throwTokens);

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,4 +275,14 @@ class Test
275275
protected function foo()
276276
{
277277
}
278+
279+
/**
280+
* @return Closure
281+
*/
282+
public function getStuff()
283+
{
284+
return function () {
285+
throw new RuntimeException("bam!");
286+
};
287+
}
278288
}

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
3939
- Version control reports now show which errors are fixable if you are showing sources
4040
- Added a new sniff to enforce a single space after a NOT operator (request #1051)
4141
-- Include in a ruleset using the code Generic.Formatting.SpaceAfterNot
42+
- Fixed bug #790 : Incorrect missing @throws error in methods that use closures
4243
- Fixed bug #908 : PSR2 standard is not checking that closing brace is on line following the body
4344
- Fixed bug #945 : Incorrect indent behavior using deep-nested function and arrays
4445
- Fixed bug #961 : Two anonymous functions passed as function/method arguments cause indentation false positive

0 commit comments

Comments
 (0)