From a9ad1352a9c2e0ecb3e2bc3fae57f6c03f4be431 Mon Sep 17 00:00:00 2001 From: tfrommen Date: Tue, 27 Oct 2020 18:59:22 +0100 Subject: [PATCH 1/2] Add new Isset sniff --- HM/Sniffs/PHP/IssetSniff.php | 29 +++++++++++++++++++++++++++++ tests/FixtureTests.php | 1 + tests/fixtures/fail/isset.php | 7 +++++++ tests/fixtures/fail/isset.php.json | 14 ++++++++++++++ tests/fixtures/pass/isset.php | 6 ++++++ 5 files changed, 57 insertions(+) create mode 100644 HM/Sniffs/PHP/IssetSniff.php create mode 100644 tests/fixtures/fail/isset.php create mode 100644 tests/fixtures/fail/isset.php.json create mode 100644 tests/fixtures/pass/isset.php diff --git a/HM/Sniffs/PHP/IssetSniff.php b/HM/Sniffs/PHP/IssetSniff.php new file mode 100644 index 00000000..9ae862ab --- /dev/null +++ b/HM/Sniffs/PHP/IssetSniff.php @@ -0,0 +1,29 @@ +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->addError( 'Only one argument is allowed per ISSET call', $stackPtr, 'MultipleArguments' ); + } + } +} diff --git a/tests/FixtureTests.php b/tests/FixtureTests.php index bad88454..35379776 100644 --- a/tests/FixtureTests.php +++ b/tests/FixtureTests.php @@ -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', diff --git a/tests/fixtures/fail/isset.php b/tests/fixtures/fail/isset.php new file mode 100644 index 00000000..2b4be9be --- /dev/null +++ b/tests/fixtures/fail/isset.php @@ -0,0 +1,7 @@ + Date: Wed, 28 Oct 2020 13:08:57 +0100 Subject: [PATCH 2/2] Change to warning --- HM/Sniffs/PHP/IssetSniff.php | 2 +- tests/fixtures/fail/isset.php.json | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/HM/Sniffs/PHP/IssetSniff.php b/HM/Sniffs/PHP/IssetSniff.php index 9ae862ab..9dfb3b39 100644 --- a/HM/Sniffs/PHP/IssetSniff.php +++ b/HM/Sniffs/PHP/IssetSniff.php @@ -23,7 +23,7 @@ public function process( File $phpcsFile, $stackPtr ) { $comma_token = $phpcsFile->findNext( T_COMMA, $open_parenthesis_token + 1 ); if ( $comma_token !== false && $comma_token < $tokens[ $open_parenthesis_token ]['parenthesis_closer'] ) { - $phpcsFile->addError( 'Only one argument is allowed per ISSET call', $stackPtr, 'MultipleArguments' ); + $phpcsFile->addWarning( 'Only one argument should be used per ISSET call', $stackPtr, 'MultipleArguments' ); } } } diff --git a/tests/fixtures/fail/isset.php.json b/tests/fixtures/fail/isset.php.json index fec47188..41821361 100644 --- a/tests/fixtures/fail/isset.php.json +++ b/tests/fixtures/fail/isset.php.json @@ -2,13 +2,13 @@ "3": [ { "source": "HM.PHP.Isset.MultipleArguments", - "type": "error" + "type": "warning" } ], "4": [ { "source": "HM.PHP.Isset.MultipleArguments", - "type": "error" + "type": "warning" } ] } \ No newline at end of file