Skip to content

Generic/ForbiddenFunctions: improve comment tolerance #929

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/Standards/Generic/Sniffs/PHP/ForbiddenFunctionsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;

class ForbiddenFunctionsSniff implements Sniff
{
Expand Down Expand Up @@ -135,12 +136,12 @@ public function process(File $phpcsFile, $stackPtr)
T_IMPLEMENTS => true,
];

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

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

$nextToken = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
$nextToken = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
if (isset($ignore[$tokens[$nextToken]['code']]) === true) {
// Not a call to a PHP function.
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class RightSideVisTest {
}

namespace Something\sizeof;
$var = new Sizeof();
$var = new /*comment*/ Sizeof();
class SizeOf implements Something {}

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

#[SizeOf(10)]
function doSomething() {}

$size = sizeof /*comment*/ ($array);
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ final class ForbiddenFunctionsUnitTest extends AbstractSniffUnitTest
public function getErrorList()
{
$errors = [
2 => 1,
4 => 1,
6 => 1,
2 => 1,
4 => 1,
6 => 1,
62 => 1,
];

return $errors;
Expand Down