Skip to content
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

Misc/TypeJuggle: add unit tests + minor fix #76

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions Security/Sniffs/Misc/TypeJuggleSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']));
}
}

Expand Down
21 changes: 21 additions & 0 deletions Security/Tests/Misc/TypeJuggleUnitTest.1.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/*
* Paranoia mode = 1
*/

// Test for 'equals' conditionals.
if ( true == $true ) { // Warning.
echo 'True';
} elseif ( false === $true ) {
echo 'False';
}

// Test for 'not equals' conditionals.
if ( true != $true ) { // Warning.
echo 'True';
} elseif ( true <> $true ) { // Warning.
echo 'False';
} elseif ( false !== $true ) { // Ok.
echo 'False';
}
13 changes: 13 additions & 0 deletions Security/Tests/Misc/TypeJuggleUnitTest.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

/*
* Paranoia mode = 0.
*/

if ( true == $true ) {
echo 'True';
} elseif ( true != $true ) {
echo 'True';
} elseif ( true <> $true ) {
echo 'False';
}
52 changes: 52 additions & 0 deletions Security/Tests/Misc/TypeJuggleUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace PHPCS_SecurityAudit\Security\Tests\Misc;

use PHPCS_SecurityAudit\Security\Tests\AbstractSecurityTestCase;

/**
* Unit test class for the TypeJuggle sniff.
*
* @covers \PHPCS_SecurityAudit\Security\Sniffs\Misc\TypeJuggleSniff
*/
class TypeJuggleUnitTest extends AbstractSecurityTestCase
{

/**
* Returns the lines where errors should occur.
*
* The key of the array should represent the line number and the value
* should represent the number of errors that should occur on that line.
*
* @return array<int, int>
*/
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<int, int>
*/
public function getWarningList($testFile = '')
{
switch ($testFile) {
case 'TypeJuggleUnitTest.1.inc':
return [
8 => 1,
15 => 1,
17 => 1,
];

default:
return [];
}
}
}