Skip to content

Commit 389b559

Browse files
committed
Detect class in @throws statement.
Add test.
1 parent 0f5577b commit 389b559

3 files changed

Lines changed: 34 additions & 1 deletion

File tree

WPForms/Sniffs/PHP/UseStatementSniff.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ private function findInParamsDescription( $phpcsFile, $entityName, $element ) {
131131
return false;
132132
}
133133

134+
if ( $this->findInThrows( $phpcsFile, $entityName, $nextElement ) ) {
135+
return true;
136+
}
137+
134138
if ( $tokens[ $nextElement ]['content'] !== '@param' ) {
135139
return $this->findInParamsDescription( $phpcsFile, $entityName, $nextElement );
136140
}
@@ -147,4 +151,31 @@ private function findInParamsDescription( $phpcsFile, $entityName, $element ) {
147151

148152
return $this->findInParamsDescription( $phpcsFile, $entityName, $nextElement );
149153
}
154+
155+
/**
156+
* Find function/objects/class in the PHPDoc @throws.
157+
*
158+
* @since {VERSION}
159+
*
160+
* @param File $phpcsFile The PHP_CodeSniffer file where the token was found.
161+
* @param string $entityName Function/objects/class name.
162+
* @param int $stackPtr Current search position.
163+
*
164+
* @return bool
165+
*/
166+
private function findInThrows( $phpcsFile, $entityName, $stackPtr ) {
167+
168+
$tokens = $phpcsFile->getTokens();
169+
170+
if ( $tokens[ $stackPtr ]['content'] === '@throws' ) {
171+
$closePtr = $phpcsFile->findNext( T_DOC_COMMENT_CLOSE_TAG, $stackPtr + 1 );
172+
$commentPtr = $phpcsFile->findNext( T_DOC_COMMENT_STRING, $stackPtr + 1, $closePtr );
173+
174+
if ( $commentPtr && $entityName === explode( ' ', $tokens[ $commentPtr ]['content'] )[0] ) {
175+
return true;
176+
}
177+
}
178+
179+
return false;
180+
}
150181
}

WPForms/Tests/TestedFiles/PHP/UseStatement.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Some\Name\Space\Type2;
1010
use Some\Name\Space\Type3;
1111
use Some\Name\Space\Type4;
12+
use Some\Name\Space\Type5;
1213
use Unused\Name\Space\Example3;
1314
use Unused\Name\Space\Example4;
1415

@@ -45,6 +46,7 @@ public function test( $type ) {}
4546

4647
/**
4748
* @return Type4
49+
* @throws Type5 Type5.
4850
*/
4951
public function test2() {}
5052
}

WPForms/Tests/Tests/PHP/UseStatementTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ public function testProcess() {
2121

2222
$phpcsFile = $this->process( new UseStatementSniff() );
2323

24-
$this->fileHasErrors( $phpcsFile, 'UnusedUseStatement', [ 12, 13, 16 ] );
24+
$this->fileHasErrors( $phpcsFile, 'UnusedUseStatement', [ 13, 14, 17 ] );
2525
}
2626
}

0 commit comments

Comments
 (0)