Skip to content

Commit

Permalink
Merge pull request #236 from humanmade/isset-sniff
Browse files Browse the repository at this point in the history
#142 - Add new Isset sniff
  • Loading branch information
tfrommen authored Feb 3, 2021
2 parents 6efe4b8 + b9f3cb3 commit 6ad1fc9
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 0 deletions.
29 changes: 29 additions & 0 deletions HM/Sniffs/PHP/IssetSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
namespace HM\Sniffs\PHP;

use PHP_CodeSniffer\Exceptions\RuntimeException;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;

/**
* Sniff to check for isset() usage.
*/
class IssetSniff implements Sniff {
public function register() {
return array( T_ISSET );
}

public function process( File $phpcsFile, $stackPtr ) {
$tokens = $phpcsFile->getTokens();

$open_parenthesis_token = $phpcsFile->findNext( T_OPEN_PARENTHESIS, $stackPtr + 1 );
if ( $open_parenthesis_token === false ) {
throw new RuntimeException( '$stackPtr was not a valid T_ISSET' );
}

$comma_token = $phpcsFile->findNext( T_COMMA, $open_parenthesis_token + 1 );
if ( $comma_token !== false && $comma_token < $tokens[ $open_parenthesis_token ]['parenthesis_closer'] ) {
$phpcsFile->addWarning( 'Only one argument should be used per ISSET call', $stackPtr, 'MultipleArguments' );
}
}
}
1 change: 1 addition & 0 deletions tests/FixtureTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public function setUp() {
'HM.Namespaces.NoLeadingSlashOnUse',
'HM.Performance.SlowMetaQuery',
'HM.Performance.SlowOrderBy',
'HM.PHP.Isset',
'HM.Security.EscapeOutput',
'HM.Security.NonceVerification',
'HM.Security.ValidatedSanitizedInput',
Expand Down
7 changes: 7 additions & 0 deletions tests/fixtures/fail/isset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

isset( $a, $b );
isset(
$a,
$b
);
14 changes: 14 additions & 0 deletions tests/fixtures/fail/isset.php.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"3": [
{
"source": "HM.PHP.Isset.MultipleArguments",
"type": "warning"
}
],
"4": [
{
"source": "HM.PHP.Isset.MultipleArguments",
"type": "warning"
}
]
}
6 changes: 6 additions & 0 deletions tests/fixtures/pass/isset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

isset( $a );
isset(
$a
);

0 comments on commit 6ad1fc9

Please sign in to comment.