Skip to content

Commit 73dcaac

Browse files
committed
Added Squiz DisallowBooleanStatementSniff to ban boolean operators outside of control structure conditions
1 parent cd24025 commit 73dcaac

File tree

4 files changed

+179
-0
lines changed

4 files changed

+179
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
/**
3+
* Squiz_Sniffs_PHP_DisallowBooleanStatementSniff.
4+
*
5+
* PHP version 5
6+
*
7+
* @category PHP
8+
* @package PHP_CodeSniffer
9+
* @author Greg Sherwood <[email protected]>
10+
* @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600)
11+
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
12+
* @link http://pear.php.net/package/PHP_CodeSniffer
13+
*/
14+
15+
/**
16+
* Squiz_Sniffs_PHP_DisallowBooleanStatementSniff.
17+
*
18+
* Ensures that boolean operators are only used inside control structure conditions.
19+
*
20+
* @category PHP
21+
* @package PHP_CodeSniffer
22+
* @author Greg Sherwood <[email protected]>
23+
* @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600)
24+
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
25+
* @version Release: @package_version@
26+
* @link http://pear.php.net/package/PHP_CodeSniffer
27+
*/
28+
class Squiz_Sniffs_PHP_DisallowBooleanStatementSniff implements PHP_CodeSniffer_Sniff
29+
{
30+
31+
32+
/**
33+
* Returns an array of tokens this test wants to listen for.
34+
*
35+
* @return array
36+
*/
37+
public function register()
38+
{
39+
return PHP_CodeSniffer_Tokens::$booleanOperators;
40+
41+
}//end register()
42+
43+
44+
/**
45+
* Processes this test, when one of its tokens is encountered.
46+
*
47+
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
48+
* @param int $stackPtr The position of the current token
49+
* in the stack passed in $tokens.
50+
*
51+
* @return void
52+
*/
53+
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
54+
{
55+
$tokens = $phpcsFile->getTokens();
56+
if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) {
57+
$foundOwner = false;
58+
foreach ($tokens[$stackPtr]['nested_parenthesis'] as $open => $close) {
59+
if (isset($tokens[$open]['parenthesis_owner']) === true) {
60+
// Any owner means we are not just a simple statement.
61+
return;
62+
}
63+
}
64+
}
65+
66+
$error = 'Boolean operators are not allowed outside of control structure conditions';
67+
$phpcsFile->addError($error, $stackPtr, 'Found');
68+
69+
}//end process()
70+
71+
72+
}//end class
73+
74+
?>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
trySomething() || trySomethingElse();
4+
if (trySomething() === FALSE) {
5+
trySomethingElse();
6+
}
7+
8+
$success || fail();
9+
if ($success === FALSE) {
10+
fail();
11+
}
12+
13+
$foo = ($bar || $foo);
14+
15+
doSomething() || die();
16+
17+
if ($something || somethingElse()) {
18+
while ($foo && $bar) {
19+
}
20+
21+
do {
22+
// Code here.
23+
}
24+
while ($foo && $bar);
25+
}
26+
27+
?>
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
/**
3+
* Unit test class for the DisallowBooleanStatement sniff.
4+
*
5+
* PHP version 5
6+
*
7+
* @category PHP
8+
* @package PHP_CodeSniffer
9+
* @author Greg Sherwood <[email protected]>
10+
* @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600)
11+
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
12+
* @link http://pear.php.net/package/PHP_CodeSniffer
13+
*/
14+
15+
/**
16+
* Unit test class for the DisallowBooleanStatement sniff.
17+
*
18+
* A sniff unit test checks a .inc file for expected violations of a single
19+
* coding standard. Expected errors and warnings are stored in this class.
20+
*
21+
* @category PHP
22+
* @package PHP_CodeSniffer
23+
* @author Greg Sherwood <[email protected]>
24+
* @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600)
25+
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
26+
* @version Release: @package_version@
27+
* @link http://pear.php.net/package/PHP_CodeSniffer
28+
*/
29+
class Squiz_Tests_PHP_DisallowBooleanStatementUnitTest extends AbstractSniffUnitTest
30+
{
31+
32+
33+
/**
34+
* Returns the lines where errors should occur.
35+
*
36+
* The key of the array should represent the line number and the value
37+
* should represent the number of errors that should occur on that line.
38+
*
39+
* @return array(int => int)
40+
*/
41+
public function getErrorList()
42+
{
43+
return array(
44+
3 => 1,
45+
8 => 1,
46+
13 => 1,
47+
15 => 1,
48+
);
49+
50+
}//end getErrorList()
51+
52+
53+
/**
54+
* Returns the lines where warnings should occur.
55+
*
56+
* The key of the array should represent the line number and the value
57+
* should represent the number of warnings that should occur on that line.
58+
*
59+
* @return array(int => int)
60+
*/
61+
public function getWarningList()
62+
{
63+
return array();
64+
65+
}//end getWarningList()
66+
67+
68+
}//end class
69+
70+
?>

package.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
3838
- Added support for the PHP 5.4 callable type hint
3939
- Fixed problem where some file content could be ignored when checking STDIN
4040
- Version information is now printed when installed via composer or run from a Git clone (request #20050)
41+
- Added Squiz DisallowBooleanStatementSniff to ban boolean operators outside of control structure conditions
4142
- PEAR MultiLineConditionSniff now has a setting to specify how many spaces code should be indented
4243
-- Default remains at 4; override the 'indent' setting in a ruleset.xml file to change
4344
-- Thanks to Szabolcs Sulik for the patch
@@ -1515,6 +1516,9 @@ http://pear.php.net/dtd/package-2.0.xsd">
15151516
<file baseinstalldir="PHP" name="CommentedOutCodeSniff.php" role="php">
15161517
<tasks:replace from="@package_version@" to="version" type="package-info" />
15171518
</file>
1519+
<file baseinstalldir="PHP" name="DisallowBooleanStatementSniff.php" role="php">
1520+
<tasks:replace from="@package_version@" to="version" type="package-info" />
1521+
</file>
15181522
<file baseinstalldir="PHP" name="DisallowComparisonAssignmentSniff.php" role="php">
15191523
<tasks:replace from="@package_version@" to="version" type="package-info" />
15201524
</file>
@@ -1934,6 +1938,10 @@ http://pear.php.net/dtd/package-2.0.xsd">
19341938
<file baseinstalldir="PHP" name="CommentedOutCodeUnitTest.php" role="test">
19351939
<tasks:replace from="@package_version@" to="version" type="package-info" />
19361940
</file>
1941+
<file baseinstalldir="PHP" name="DisallowBooleanStatementUnitTest.inc" role="test" />
1942+
<file baseinstalldir="PHP" name="DisallowBooleanStatementUnitTest.php" role="test">
1943+
<tasks:replace from="@package_version@" to="version" type="package-info" />
1944+
</file>
19371945
<file baseinstalldir="PHP" name="DisallowComparisonAssignmentUnitTest.inc" role="test" />
19381946
<file baseinstalldir="PHP" name="DisallowComparisonAssignmentUnitTest.php" role="test">
19391947
<tasks:replace from="@package_version@" to="version" type="package-info" />

0 commit comments

Comments
 (0)