Skip to content

Commit dc1a497

Browse files
authored
Merge pull request #929 from PHPCSStandards/feature/generic-forbiddenfunctions-improve-comment-tolerance
Generic/ForbiddenFunctions: improve comment tolerance
2 parents ef8ac39 + ff1f9d5 commit dc1a497

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
lines changed

src/Standards/Generic/Sniffs/PHP/ForbiddenFunctionsSniff.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
use PHP_CodeSniffer\Files\File;
1616
use PHP_CodeSniffer\Sniffs\Sniff;
17+
use PHP_CodeSniffer\Util\Tokens;
1718

1819
class ForbiddenFunctionsSniff implements Sniff
1920
{
@@ -135,12 +136,12 @@ public function process(File $phpcsFile, $stackPtr)
135136
T_IMPLEMENTS => true,
136137
];
137138

138-
$prevToken = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
139+
$prevToken = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
139140

140141
// If function call is directly preceded by a NS_SEPARATOR it points to the
141142
// global namespace, so we should still catch it.
142143
if ($tokens[$prevToken]['code'] === T_NS_SEPARATOR) {
143-
$prevToken = $phpcsFile->findPrevious(T_WHITESPACE, ($prevToken - 1), null, true);
144+
$prevToken = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($prevToken - 1), null, true);
144145
if ($tokens[$prevToken]['code'] === T_STRING) {
145146
// Not in the global namespace.
146147
return;
@@ -152,7 +153,7 @@ public function process(File $phpcsFile, $stackPtr)
152153
return;
153154
}
154155

155-
$nextToken = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
156+
$nextToken = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
156157
if (isset($ignore[$tokens[$nextToken]['code']]) === true) {
157158
// Not a call to a PHP function.
158159
return;

src/Standards/Generic/Tests/PHP/ForbiddenFunctionsUnitTest.inc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class RightSideVisTest {
4848
}
4949

5050
namespace Something\sizeof;
51-
$var = new Sizeof();
51+
$var = new /*comment*/ Sizeof();
5252
class SizeOf implements Something {}
5353

5454
function mymodule_form_callback(SizeOf $sizeof) {
@@ -58,3 +58,5 @@ $size = $class?->sizeof($array);
5858

5959
#[SizeOf(10)]
6060
function doSomething() {}
61+
62+
$size = sizeof /*comment*/ ($array);

src/Standards/Generic/Tests/PHP/ForbiddenFunctionsUnitTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ final class ForbiddenFunctionsUnitTest extends AbstractSniffUnitTest
3131
public function getErrorList()
3232
{
3333
$errors = [
34-
2 => 1,
35-
4 => 1,
36-
6 => 1,
34+
2 => 1,
35+
4 => 1,
36+
6 => 1,
37+
62 => 1,
3738
];
3839

3940
return $errors;

0 commit comments

Comments
 (0)