diff --git a/Security/Sniffs/Misc/TypeJuggleSniff.php b/Security/Sniffs/Misc/TypeJuggleSniff.php index db77eb7..bea687d 100644 --- a/Security/Sniffs/Misc/TypeJuggleSniff.php +++ b/Security/Sniffs/Misc/TypeJuggleSniff.php @@ -26,10 +26,10 @@ public function register() { * @return void */ public function process(File $phpcsFile, $stackPtr) { - $tokens = $phpcsFile->getTokens(); if (\PHP_CodeSniffer\Config::getConfigData('ParanoiaMode')) { - $warning = 'You are using the comparison operator "'. $tokens[$stackPtr]['content'] .'" that converts type and may cause unintended results.'; - $phpcsFile->addWarning($warning, $stackPtr, 'TypeJuggle'); + $tokens = $phpcsFile->getTokens(); + $warning = 'You are using the comparison operator "%s" that converts type and may cause unintended results.'; + $phpcsFile->addWarning($warning, $stackPtr, 'TypeJuggle', array($tokens[$stackPtr]['content'])); } } diff --git a/Security/Tests/Misc/TypeJuggleUnitTest.1.inc b/Security/Tests/Misc/TypeJuggleUnitTest.1.inc new file mode 100644 index 0000000..db55499 --- /dev/null +++ b/Security/Tests/Misc/TypeJuggleUnitTest.1.inc @@ -0,0 +1,21 @@ + $true ) { // Warning. + echo 'False'; +} elseif ( false !== $true ) { // Ok. + echo 'False'; +} diff --git a/Security/Tests/Misc/TypeJuggleUnitTest.inc b/Security/Tests/Misc/TypeJuggleUnitTest.inc new file mode 100644 index 0000000..935bd56 --- /dev/null +++ b/Security/Tests/Misc/TypeJuggleUnitTest.inc @@ -0,0 +1,13 @@ + $true ) { + echo 'False'; +} diff --git a/Security/Tests/Misc/TypeJuggleUnitTest.php b/Security/Tests/Misc/TypeJuggleUnitTest.php new file mode 100644 index 0000000..cfe13cc --- /dev/null +++ b/Security/Tests/Misc/TypeJuggleUnitTest.php @@ -0,0 +1,52 @@ + + */ + public function getErrorList() + { + return []; + } + + /** + * Returns the lines where warnings should occur. + * + * The key of the array should represent the line number and the value + * should represent the number of warnings that should occur on that line. + * + * @param string $testFile The name of the file being tested. + * + * @return array + */ + public function getWarningList($testFile = '') + { + switch ($testFile) { + case 'TypeJuggleUnitTest.1.inc': + return [ + 8 => 1, + 15 => 1, + 17 => 1, + ]; + + default: + return []; + } + } +}